// More STL examples this time including maps. // // Basic operations on maps: // function/operator // ----------------- // insert(pair) // x[index] (reference to the value associated with this key) // size() // empty() // clear() // count(key) (returns a count of the # of keys equal to key, always 0 or 1 // // Use of iterators on maps: // function/operator // ---------------------- // begin() // end() // *iterator (const) // erase(iterator) // insert(iterator, value) // // pair // first--returns first element in the pair // second--returns second element in the pair // // use type unordered_set for a set of objects that do not overload operator < // use type unordered_map for a map whose keys do not overload operator < #include #include #include #include #include #include using namespace std; // This is the overloaded << operator to allow us to put a set onto an // output stream as a bracketed, comma-separted sequence ostream & operator<<(ostream & out, const set & s) { out << "["; if (s.size() > 0) { auto itr = s.begin(); out << *itr; itr++; while (itr != s.end()) { out << ", " << *itr; itr++; } } out << "]"; return out; } // This example involves reading all of the words in a file and counting how // many times each word occurs. We ask the user for a minimum number of words // for printing and show those entries. void example1() { ifstream input("moby.txt"); map count; string word; while (input >> word) { count[word]++; } cout << "minimum count to use? "; int min; cin >> min; for (auto p : count) { if (p.second >= min) { cout << p.first << ": " << p.second << endl; } } cout << endl; } // This function reads a short data file with pairs of lines that have a name // on the first line and a sequence of course abbreviations on the second line. // It figures out who is taking each class, building up a string of student // names with periods to keep them separate. void example2() { ifstream input("stl2.txt"); string name; map data; while (getline(input, name)) { string courses; getline(input, courses); istringstream input2(courses); string course; while (input2 >> course) { data[course] += name + "."; } } for (auto itr = data.begin(); itr != data.end(); itr++) { cout << itr->first << " maps to " << itr->second << endl; } cout << endl; } // This is a variation of example2 where the map keeps a set of names for each // course. void example3() { ifstream input("stl2.txt"); string name; map> data; while (getline(input, name)) { string courses; getline(input, courses); istringstream input2(courses); string course; while (input2 >> course) { data[course].insert(name); } } for (auto itr = data.begin(); itr != data.end(); itr++) { cout << itr->first << " maps to " << itr->second << endl; } cout << endl; // the following code was an example of deleting all map entries where the // key starts with the letter 'c' auto itr = data.begin(); while (itr != data.end()) { if (itr->first[0] == 'c') { itr = data.erase(itr); } else { itr++; } } for (auto itr = data.begin(); itr != data.end(); itr++) { cout << itr->first << " maps to " << itr->second << endl; } cout << endl; } int main() { example1(); example2(); example3(); return 0; }