If you don't mind reordering the vector, then this should do it in O(n*log(n)) time:
std::sort(vector.begin(), vector.end()); vector.erase(std::unique(vector.begin(), vector.end()), vector.end());
To preserve order, you can instead use a vector of pairs (line-number, line *): sort by line, uniquify using a comparator that compares the contents of the line and finally sorts by line number according to the lines:
struct pair {int line, std::string const * string}; struct OrderByLine { bool operator()(pair const & x, pair const & y) { return x.line < y.line; } }; struct OrderByString { bool operator()(pair const & x, pair const & y) { return *x.string < *y.string; } }; struct StringEquals { bool operator()(pair const & x, pair const & y) { return *x.string == *y.string; } }; std::sort(vector.begin(), vector.end(), OrderByString()); vector.erase(std::unique(vector.begin(), vector.end(), StringEquals()), vector.end()); std::sort(vector.begin(), vector.end(), OrderByLine());
Mike seymour
source share