#include #include #include #include #include #include #include #include #include using namespace std; set s{1,2,3,4,5,8,20}; s set s2{1,20,3,4,5,8,3}; s2 set s3{1,1,1,3,3,2,9,15}; s3 multiset m{1,1,1,3,3,2,9,15}; m multiset m{1,15,1,3,2,2,9,3,1}; m set > s2{1,2,3,4,5,8,20}; s2 // declaring set set st; // declaring iterators auto it = st.begin(); set::iterator it1, it2; // declaring pair for return value of set containing // set iterator and bool pair::iterator, bool> ptr; // using insert() to insert single element // inserting 20 ptr = st.insert(20); // checking if the element was already present or newly inserted if (ptr.second) cout << "The element was newly inserted" << endl; else cout << "The element was already present" << endl; // using insert() to insert single element // inserting 20 again ... ptr = st.insert(20); // checking if the element was already present or newly inserted if (ptr.second) cout << "The element was newly inserted" << endl; else cout << "The element was already present" << endl; // printing set elements after insertion cout << "\nThe set elements after 1st insertion are : "; for (it1 = st.begin(); it1 != st.end(); ++it1) cout << *it1 << " "; // inserting set elements using hint st.insert(it, 24); // printing set elements after insertion cout << "\nThe set elements after 2nd insertion are : "; for (it1 = st.begin(); it1 != st.end(); ++it1) cout << *it1 << " "; // inserting array elements in set // 24 is not inserted again int arr[3] = {25, 24, 26}; st.insert(arr, arr + 3); // printing set elements after insertion cout << "\nThe set elements after 3rd insertion are : "; for (it1 = st.begin(); it1 != st.end(); ++it1) cout << *it1 << " "; cout << endl; // using insert() to insert single element // inserting 20 again ptr = st.insert(20); // checking if the element was already present or newly inserted if (ptr.second) cout << "The element was newly inserted" << endl; else cout << "The element was already present" << endl; // returns 1 if exists, 0 otherwise st.count(20) st.count(30) // returns iterator if found, end() if not found auto it = st.find(20); it // true if found, false otherwise it != st.end() // true if found, false otherwise st.find(30) != st.end() st.erase(20); st multiset m5{1,1,2,3}; m5 m5.erase(1); m5 multiset m6{1,1,2,3}; m6.erase(m6.begin()); m6 // not sorted! std::unordered_set us({1,11,4,2,7}); us // a different order now! us.insert(3); us // same way to check for values us.count(11) // same way to check for values us.count(30) set setOps1{1,4,7,11}; set setOps2{1,5,9,11}; set setUnion; set_union(setOps1.begin(), setOps1.end(), setOps2.begin(), setOps2.end(), std::inserter(setUnion, setUnion.begin())); setUnion set setIntersection; set_intersection(setOps1.begin(), setOps1.end(), setOps2.begin(), setOps2.end(), std::inserter(setIntersection, setIntersection.begin())); setIntersection set setDifference; set_difference(setOps1.begin(), setOps1.end(), setOps2.begin(), setOps2.end(), std::inserter(setDifference, setDifference.begin())); setDifference // not limited to sets vector vOps1{1,4,7,11}; vector vOps2{1,5,9,11}; vector vUnion(10); auto endIt = set_union(vOps1.begin(), vOps1.end(), vOps2.begin(), vOps2.end(), vUnion.begin()); vUnion vector vUnionTrunc(vUnion.begin(), endIt); vUnionTrunc map m1({{"abc", 6}, {"def", 7}, {"ghi", 6}}); m1 m1.insert({"jkl", 8}); m1 m1["mno"] = 11; m1 bool fncomp(char lhs, char rhs) { return lhs > rhs; } struct classcomp { bool operator()(const char &lhs, const char &rhs) const { return lhs > rhs; } }; void printMap(map &m) { auto mapPtr = m.cbegin(); while (mapPtr != m.cend()) { cout << mapPtr->first << ": " << mapPtr->second << endl; mapPtr++; } // End of WHILE } // End of printMap void printMap(map &m) { auto mapPtr = m.cbegin(); while (mapPtr != m.cend()) { cout << mapPtr->first << ": " << mapPtr->second << endl; mapPtr++; } // End of WHILE } // End of printMap void printMap(map &m) { auto mapPtr = m.cbegin(); while (mapPtr != m.cend()) { cout << mapPtr->first << ": " << mapPtr->second << endl; mapPtr++; } // End of WHILE } // End of printMap std::map first; first['a'] = 10; first['a'] = 20; first['b'] = 50; first['c'] = 70; first['d'] = 30; first std::map second(first.begin(), first.end()); second std::map third(second); third // class as Compare std::map fourth; fourth['a'] = 10; fourth['b'] = 50; fourth bool (*fn_pt)(char, char) = fncomp; // function pointer as Compare std::map fifth(fn_pt); fifth['a'] = 10; fifth['b'] = 50; fifth['c'] = 70; fifth['d'] = 30; fifth std::map sixth; sixth["abc"] = 4; sixth["def"] = 5; sixth["ghi"] = 6; sixth sixth["def"] sixth["blah"] sixth.at("blah") sixth.insert({"jkl", 22}); sixth sixth.erase("ghi"); sixth unordered_map um({{"abc", 6}, {"def", 7}, {"ghi", 6}}); um um.at("ghi") um.at("jkl") um["jkl"] // returns an iterator to a pair auto uit = um.find("def"); uit->second multimap mm({{"a", 1}, {"a", 3}, {"b", 5}, {"c", 7}, {"c", 8}}); mm // subscript doesn't work mm["a"] // use equal_range to find all values auto range_its = mm.equal_range("a"); for (auto it = range_its.first; it != range_its.second; ++it) { cout << "value: " << it->second << endl; } std::string paragraph = R"""( It was the best of times it was the worst of times it was the age of wisdom it was the age of foolishness it was the epoch of belief it was the epoch of incredulity it was the season of Light it was the season of Darkness it was the spring of hope it was the winter of despair we had everything before us we had nothing before us we were all going direct to Heaven we were all going direct the other way in short the period was so far like the present period that some of its noisiest authorities insisted on its being received for good or for evil in the superlative degree of comparison only )"""; std::stringstream stream(paragraph); // count the number of times each word occurs in the input std::map word_count; // empty map from string to size_t std::string word; while (stream >> word) { ++word_count[word]; } for (const auto &w : word_count) cout << w.first << " occurs " << w.second << " times" << endl; // same thing for (auto map_it = word_count.cbegin(); map_it != word_count.cend(); map_it++) cout << map_it->first << " occurs " << map_it->second << " times" << endl;