Answers for "how to assign all elements of vector in c++"

C++
0

how to assign all elements of vector in c++

#include <iostream>
#include <vector>
using namespace std;

// program to illustrate use of std::vector::assign()

int main()
{
	int vec_size, new_val;
	cin >> vec_size >> new_val;
	vector<int> vec(vec_size);

	for (int x : vec)
		cout << x << " ";

	cout << endl;

	// vec.assign(n, val) assigns all n elements of vec new value = val
	vec.assign(vec_size, new_val);
	for (int x : vec)
		cout << x << " ";
}
Posted by: Guest on March-13-2021

Code answers related to "how to assign all elements of vector in c++"

Browse Popular Code Answers by Language