i have problems with regards using the unique_copy stl function. it seems to not working his job right. i'm using borland, and what it does is just copy the original vector and not the unique string values. i tried to emulate it on devcpp and it produces an error..
here's the code:
the error is `result' cannot appear in a constant-expression and points to the insert_iterator. I'm really confused right now, if i had a vector of strings how would i get a copy with only the unique values?Code:#include <stdlib.h> #include <iostream> #include <algorithm> #include <vector> #include <iostream> #include <string> using namespace std; int main() { //UNIQUE ELEMENTS //Initialize two vectors vector<string> v, result; v.push_back("X"); v.push_back("X"); v.push_back("y"); //Create an insert_iterator for results insert_iterator<vector<string>> ins(result, result.begin()); //Demonstrate includes cout << "The vector: " << endl << " "; for (int i = 0; i < v.size(); i++){ cout << v[i] << " "; } //Find the unique elements unique_copy(v.begin(), v.end(), ins); //Display the results cout << endl << endl << "Has the following unique elements:" << endl << " "; //copy(result.begin(),result.end(), for (int i = 0; i < result.size(); i++){ cout << result[i] << " "; } system("PAUSE"); return 0; }
example
v[0] = "X"
v[1] = "X"
v[2] = "y"
Result:
r[0] = "X"
r[1] = "y"
thanks for your help!![]()



LinkBack URL
About LinkBacks




you're absolutely right! thanks!! i didn't know that the space is important. hmm... what is the space for? is is for the allocator?
I used to be an adventurer like you... then I took an arrow to the knee.