C++ 02 Feb 2010 16:38:56

C++ Find Unique Elements Speed

A performance benchmark of which method is faster of finding all unique elements of a vector. The goal is having an ordered container with just the unique elements, where the tested methods are std::sort()+std::unique()+erase(), versus inserting into a separate std::set, versus inserting into std::unordered_set and then into std::set, and finally inserting into boost::unordered_set and then into std::set.

Source for the integer version is speed-find-unique-int.cpp with cycle.h.

The compilers are Microsoft Visual C++ 2010 Express as VC10 with _SECURE_SCL disabled, and GNU g++ 4.3.2.

Continue Reading »

C++ 31 Jan 2010 23:46:43

Snippet: Sort Unique Vector

//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;
}