Example:
Given std::vector<string> v = {"C", "A", "B", "A"},
we seek
vector<size_t> s := {2,0,1,0}.
These integers are assigned based on sorted order of unique values in v: 0-"A", 1-"B", 2-"C"
Possible way to do this is:
vector<string> unique(v.begin(), v.end());
unique.sort();
unique.erase(std::unique(unique.begin(), unique.end()), unique.end());
vector<size_t> s(v.size());
for(size_t i(0); i < v.size(); i++)
{
s[i] = std::lower_bound(unique.begin(), unique.end(), v[i]) - unique.begin();
}
Is there more elegant, compact and, most importantly, efficient method to perform the same routine? I know how to do this with std::map or unordered_map but not in sorted order.
UPDATE:
Obviously asymptotic complexity cannot be improved - lower bound is O(n*logn) (as above). However, different O(n*logn) algo can possibly beat the constant or just be nicer :)