As far as I am aware it is supposed to copy elements from one container to another, ensuring that only one instance of any element occurs in the destination container.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This program (for an exercise in Stroustup's The C++ Programming Language) compiles fine, but crashes when it gets to unique copy.
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> words;
vector<string> uniquewords;
string input;
do
{
cin >> input;
if (input != "quit")
{
words.push_back(input);
}
else
{
break;
}
}while(input != "quit");
cout << endl << endl;
unique_copy(words.begin(), words.end(), uniquewords.begin());
for (int loop=0; loop<uniquewords.size(); loop++)
{
cout << uniquewords[loop] << endl;
}
cout << endl << endl;
system("PAUSE");
return 0;
}
Your problem is with cin
Learn more about it.
BlakJak :]
Indeed?
As far as I can see cin is doing it's job.
words contains a valid vector of words.
What is unique_copy doing???
BlakJak :]
What is it doing?
Crashing: Unhandled acception - access violation
What is it supposed to do?
As far as I am aware it is supposed to copy elements from one container to another, ensuring that only one instance of any element occurs in the destination container.
I've got it.
Should have been:
unique_copy(words.begin(), words.end(), back_inserter(uniquewords));
instead of:
unique_copy(words.begin(), words.end(), uniquewords.begin());
This version would only have worked if there had already been something in uniquewords to overwrite.