0

I was unable to make std::copy works. I tried several codes in internet, but I was unable to make it work. I need to use copy, first, to understand why is not working, second, to use it in a constructor of a class(I can't declare a new variable in the constructor).

I have this code:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> myVector;
    vector<int> vectorCopy;

    for (int i = 0; i < 10; i++) {
        myVector.push_back(i);
    }

    cout << vectorCopy.size() << endl; //0

    vectorCopy.reserve(myVector.size());

    cout << vectorCopy.size() << endl; //PROBLEM: again 0

    copy(myVector.begin(), myVector.end(), vectorCopy.begin());

    cout << vectorCopy.size() << endl; //PROBLEM 2: again 0

    for (int j = 0; j < vectorCopy.size(); j++) {
        cout << vectorCopy[j] << endl;//never execute;
    }


    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    return 0;
}

Any ideas? I'm using CLION on MacOS Sierra and C++11.

Thanks!!

Werem
  • 534
  • 2
  • 5
  • 16

1 Answers1

2

You need to pass an output iterator as the third parameter to std::copy(). Usually you'll use something like a std::back_inserter.

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70