vector :: assign() in C++ STL - GeeksforGeeks (2024)

Improve

vector:: assign() is an STL in C++ which assigns new values to the vector elements by replacing old ones. It can also modify the size of the vector if necessary.

The syntax for assigning constant values:

vectorname.assign(int size, int value)Parameters: size - number of values to be assignedvalue - value to be assigned to the vectorname

Program 1: The program below shows how to assign constant values to a vector

CPP

// CPP program to demonstrate

// how to assign constant values to a vector

#include <bits/stdc++.h>

using namespace std;

int main()

{

vector<int> v;

v.assign(7, 100);

cout << "Size of first: "

<< int(v.size()) << '\n';

cout << "Elements are\n";

for (int i = 0; i < v.size(); i++)

cout << v[i] << endl;

return 0;

}

Output

Size of first: 7Elements are100100100100100100100

The syntax for assigning values from an array or list:

vectorname.assign(arr, arr + size)Parameters: arr - the array which is to be assigned to a vectorsize - number of elements from the beginning which has to be assigned.

Program 2: The program below shows how to assign values from an array or list

CPP

// CPP program to demonstrate

// how to assign values to a vector

// from a list

#include <bits/stdc++.h>

using namespace std;

int main()

{

vector<int> v1;

int a[] = { 1, 2, 3 };

// assign first 2 values

v1.assign(a, a + 2);

cout << "Elements of vector1 are\n";

for (int i = 0; i < v1.size(); i++)

cout << v1[i] << " ";

vector<int> v2;

// assign first 3 values

v2.assign(a, a + 3);

cout << "\nElements of vector2 are\n";

for (int i = 0; i < v2.size(); i++)

cout << v2[i] << " ";

return 0;

}

Output

Elements of vector1 are1 2 Elements of vector2 are1 2 3 

The syntax for modifying values from a vector

vectorname.assign(InputIterator first, InputIterator last) Parameters: first - Input iterator to the initial position range.last - Input iterator to the final position range.

Program 3: The program below shows how to modify the vector

CPP

// CPP program to demonstrate

// how to modify vector size

#include <bits/stdc++.h>

using namespace std;

int main()

{

vector<int> v;

v.assign(7, 100);

cout << "Size of first: " << int(v.size()) << '\n';

cout << "Elements are\n";

for (int i = 0; i < v.size(); i++)

cout << v[i] << endl;

// modify the elements

v.assign(v.begin(), v.begin() + 3);

cout << "\nModified VectorElements are\n";

for (int i = 0; i < v.size(); i++)

cout << v[i] << endl;

return 0;

}

Output

Size of first: 7Elements are100100100100100100100Modified VectorElements are100100100

Time Complexity – Linear O(N)

Syntax for assigning values with initializer list:

vectorname.assign((initializer_list)Parameter: initializer_list

Program 4:The program below shows how to assign a vector with an initializer list.

C++

#include <iostream>

#include <vector>

using namespace std;

int main()

{

vector<int> v;

// Initialize v with an initialization list

v.assign({ 1, 2, 3 });

cout << "The list is:" << endl;

for (auto i = v.begin(); i != v.end(); i++)

{

// Printing 1 2 3 as output

cout << *i << " ";

}

return 0;

}

Output

The list is:1 2 3 


Last Updated : 09 Jun, 2022

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

vector :: assign() in C++ STL - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6232

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.