C++ 31 Jan 2010 23:46:43

Snippet: Sort Unique Vector

http://tinodidriksen.com/uploads/code/cpp/sort-unique-vector.cpp

#include <vector>
#include <iostream>
#include <algorithm>

int main() {
    std::vector<int> vec1;
    vec1.push_back(1);
    vec1.push_back(2);
    vec1.push_back(3);
    vec1.push_back(1);
    vec1.push_back(2);
    vec1.push_back(3);
    vec1.push_back(1);

    std::sort(vec1.begin(), vec1.end());
    vec1.erase(std::unique(vec1.begin(), vec1.end()), vec1.end());

    std::cout << vec1.size() << std::endl;
    std::cout << vec1[0] << vec1[1] << vec1[2] << std::endl;
}
StumbleUponFacebookDiggDeliciousRedditMySpaceShare

Subscribe to the comments through RSS Feed

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word