Tutorial by Examples

Three different methods of insertion can used with sets. First, a simple insert of the value. This method returns a pair allowing the caller to check whether the insert really occurred. Second, an insert by giving a hint of where the value will be inserted. The objective is to optimize the inser...
All the insertion methods from sets also apply to multisets. Nevertheless, another possibility exists, which is providing an initializer_list: auto il = { 7, 5, 12 }; std::multiset<int> msut; msut.insert(il);
set and multiset have default compare methods, but in some cases you may need to overload them. Let's imagine we are storing string values in a set, but we know those strings contain only numeric values. By default the sort will be a lexicographical string comparison, so the order won't match the n...
There are several ways to search a given value in std::set or in std::multiset: To get the iterator of the first occurrence of a key, the find() function can be used. It returns end() if the key does not exist. std::set<int> sut; sut.insert(10); sut.insert(15); sut.insert(22); ...
The most obvious method, if you just want to reset your set/multiset to an empty one, is to use clear: std::set<int> sut; sut.insert(10); sut.insert(15); sut.insert(22); sut.insert(3); sut.clear(); //size of sut is 0 Then the erase method can be used.  It offers some poss...

Page 1 of 1