Then sort the vector (if it's OK to be alphabetical) and use equal_range to get a count of each string. Since the end reference returned by equal_range is your typical one-past-the-end reference, you can use that as the starting place for the next search.
Here's some example code:
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
int main()
{
typedef std::vector<std::string> strVec;
typedef strVec::iterator svIter;
typedef std::pair<svIter,svIter> sviPair;
strVec v;
v.push_back("hello");
v.push_back("world");
v.push_back("hello");
v.push_back("abc");
v.push_back("hello");
v.push_back("world");
v.push_back("hello");
v.push_back("world");
v.push_back("abc");
std::sort(v.begin(),v.end());
sviPair p;
for (svIter current = v.begin(); current != v.end(); current = p.second){
p = equal_range(v.begin(),v.end(),*current);
std::cout << *p.first << " " << static_cast<int>(distance(p.first,p.second)) << std::endl;
}
}
Output:
Code:
abc 2
hello 4
world 3
If the array elements are a homemade class (in the example below, I call it MyClass) with a std::string in it (str), just define operator< for the class like so:
Code:
bool MyClass::operator<(const MyClass & obj2)const{
return (str < obj2.str);
}