Inserts a copy of val and returns the position of the new element and, for sets, whether it succeeded
c.insert(pos, val)
Inserts a copy of val and returns the position of the new element (pos is used as a hint pointing to where the insert should start the search)
c.insert(beg, end)
Inserts a copy of all elements of the range [beg,end) (returns nothing)
c.insert(initlist)
Inserts a copy of all elements in the initializer list initlist (returns nothing; since C++11)
c.emplace(args…)
Inserts a copy of an element initialized with args and returns the position of the new element and, for sets, whether it succeeded (since C++11)
c.emplace_hint(pos, args…)
Inserts a copy of an element initialized with args and returns the position of the new element (pos is used as a hint pointing to where the insert should start the search)
c.erase(val)
Removes all elements equal to val and returns the number of removed elements
c.erase(pos)
Removes the element at iterator position pos and returns the following position (returned nothing before C++11)
c.erase(beg, end)
Removes all elements of the range [beg,end) and returns the following position (returned nothing before C++11)
#include <iostream> #include <set> #include <algorithm> #include <iterator> using namespace std; int main() { // type of the collection: // - no duplicates // - elements are integral values // - descending order set<int, greater<int> > coll1; // insert elements in random order using different member functions // coll1.insert({4,3,5,1,6,2}); int temp[6] = {4,3,5,1,6,2}; coll1.insert(temp, temp+6); coll1.insert(5); // print all elements /*for (int elem : coll1) { cout <<elem<<' '; }*/ for (set<int, greater<int> >::iterator it = coll1.begin(); it != coll1.end(); ++it) { cout << *it << ' '; } cout << endl; // insert 4 again and process return value auto status = coll1.insert(4); if (status.second) { cout << "4 inserted as element " << distance(coll1.begin(), status.first) + 1 << endl; }else { cout << "4 already existes" << endl; } // assign elements to another set with ascending order set<int> coll2(coll1.cbegin(), coll1.cend()); // print all elements of the copy using stream iterators copy(coll2.cbegin(), coll2.cend(), ostream_iterator<int>(cout, " ")); cout << endl; // remove all elements up to element with value 3 coll2.erase(coll2.begin(), coll2.find(3)); // remove all elements with value 5 int num(coll2.erase(5)); cout << num << " element(s) removed" << endl; // print all elements copy(coll2.cbegin(), coll2.cend(), ostream_iterator<int>(cout, " ")); cout << endl; system("Pause"); }
#include<iostream> #include<iterator> #include<algorithm> #include<set> usingnamespacestd; // type for runtime sorting criterion class RuntimeCmp { public: enum cmp_mode {normal, reverse}; private: cmp_mode mode; public: // constructor for sorting criterion // - default criterion uses value normal RuntimeCmp (cmp_mode m = normal) : mode(m){} // comparison of elements // - member function for any element type template <typename T> booloperator()(const T& t1, const T& t2)const { return mode == normal ? t1 < t2 : t2 < t1; } // comparison of sorting criteria booloperator== (const RuntimeCmp& rc) const { return mode == rc.mode; } }; // type of a set that uses this sorting criterion typedefset<int, RuntimeCmp> IntSet; intmain() { // create, fill, and print set with normal element order // - uses default sorting criterion int temp[7] = {4,7,5,1,6,2,5}; IntSet coll1(temp, temp+7); cout << "coll1: "; copy(coll1.cbegin(), coll1.cend(), ostream_iterator<int>(cout, " ")); cout << endl; // create sorting criterion with reverse element order RuntimeCmp reverse_order(RuntimeCmp::reverse); // create, fill, and print set with reverse element order IntSet coll2(reverse_order); coll2.insert(temp, temp+7); cout << "coll2: "; copy(coll2.cbegin(), coll2.cend(), ostream_iterator<int>(cout, " ")); cout << endl; // assign elements AND sorting criterion coll1 = coll2; coll1.insert(3); cout << "coll1: "; copy(coll1.cbegin(), coll1.cend(), ostream_iterator<int>(cout, " ")); cout << endl; // just to make sure... if (coll1.value_comp() == coll2.value_comp()) { cout << "coll1 and coll2 have the same sorting criterion" << endl; }else { cout << "coll1 and coll2 have a different sorting criterion" << endl; } system("Pause"); }