How to remove duplicates in a vector (without sorting) C ++

I have a vector that the user enters some lines. I want to keep the order that the user enters, but removes any duplicate words. The only thing I could find on the Internet was sorting and uniqueness, but since I cannot sort the vector, I was stuck. Thanks in advance for any help.

ex user login → hello there dog cat hello cat book

the vector must have → hello there dog cat book

right now everything i have ...

string s; 
vector <string> myVec; 

while (cin >> s){
 myVec.push_back(s); 
}

{code to sort vector}
+4
source share
1 answer

Along with yours, vectoryou can check if this word is already in std::set<std::string>, ignoring it, if so, otherwise paste it into both containers:

while (cin >> s)
    if (mySet.insert(s).second) // newly inserted in set?
        myVec.push_back(s);
+9

All Articles