I am getting the run time error in following code. Please let me know can i copy vector elements in set?
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main()
{
vector<int> v;
set<int> kk;
set<int>::iterator itr;
for (int i = 0; i < 6; i++)
{
v.push_back(i * 2);
}
copy(v.begin(), v.end(), inserter(kk, itr));
}
Begin Initialize a vector v1 with its elements. Declare another vector v2 and copying elements of first vector to second vector using constructor method and they are deeply copied. Print the elements of v1. Print the elements of v2.
Copy enables you to: define a vector of operands, copy the values or bit status of each operand within that vector, write those values or status into a corresponding vector of operands of the same length.
You are not initialising itr
:
set<int>::iterator itr = kk.begin();
Or remove itr
entirely:
copy(v.begin(), v.end(), inserter(kk, kk.begin()));
In this instance, you could simply initialise kk
thus (but if you want to add to kk
follow the line above):
set<int> kk(v.begin(), v.end());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With