11.3.2. Adding Elements
The insert
members (Table 11.4 (overleaf)) add one element or a range of elements. Because map
and set
(and the corresponding unordered types) contain unique keys, inserting an element that is already present has no effect:
vector<int> ivec = {2,4,6,8,2,4,6,8}; // ivec has eight elementsset<int> set2; // empty setset2.insert(ivec.cbegin(), ivec.cend()); // set2 has four elementsset2.insert({1,3,5,7,1,3,5,7}); // set2 now has eight elements
The versions of insert
that take a pair of iterators or an initializer list work similarly to the ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.