Bruce Eckel's Thinking in C++, 2nd Ed | Contents | Prev | Next |
//: C20:Intset.cpp // Simple use of STL set #include <set> #include <iostream> using namespace std; int main() { set<int> intset; for(int i = 0; i < 25; i++) for(int j = 0; j < 10; j++) // Try to insert multiple copies: intset.insert(j); // Print to output: copy(intset.begin(), intset.end(), ostream_iterator<int>(cout, "\n")); } ///:~
//: C20:WordSet.cpp #include <string> #include <fstream> #include <iostream> #include <set> #include "../require.h" using namespace std; int main(int argc, char* argv[]) { requireArgs(argc, 1); ifstream source(argv[1]); assure(source, argv[1]); string word; set<string> words; while(source >> word) words.insert(word); copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n")); cout << "Number of unique words:" << words.size() << endl; } ///:~