Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i copy 'vector' elements in 'set' using the copy algorithm?

Tags:

c++

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));
}
like image 497
Alok Avatar asked Jun 23 '11 03:06

Alok


People also ask

How do I copy a vector array to another?

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.

What is a vector copy?

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.


1 Answers

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());
like image 196
Johnsyweb Avatar answered Oct 17 '22 00:10

Johnsyweb