C++ examples for STL:string
Counts the number of unique characters in a string.
#include <string> #include <iostream> template<typename T> int countUnique(const std::basic_string<T>& s) { using std::basic_string; basic_string<T> chars;//from www . ja v a2s . com for (typename basic_string<T>::const_iterator p = s.begin(); p != s.end(); ++p) { if (chars.find(*p) == basic_string<T>::npos) chars += *p; } return(chars.length()); } int main() { std::string s = "this is a test"; std::cout << countUnique(s) << '\n'; }