1、set和multiset基础
set和multiset会根据特定的排序准则,自动将元素进行排序。不同的是后者允许元素重复而前者不允许。
需要包含头文件:
#include <set>
2、创建元素
set<int> s1; //创建空的set对象,元素类型为int, set<const char*, strLess> s2( strLess); //创建空的set对象,元素类型char*,比较函数对象(即排序准则)为自定义strLess set<int> s3(s1); //利用set对象s1,拷贝生成set对象s2 int iArray[] = {13, 32, 19}; set<int> s4(iArray, iArray + 3); //用迭代区间[&first, &last)所指的元素,创建一个set对象 const char* szArray[] = {"hello", "dog", "bird" }; set<const char*, strLess> s5(szArray, szArray + 3, strLess() ); //用迭代区间[&first, &last)所指的元素,及比较函数对象strLess,创建一个set对象
3、插入元素
set<string> set1; //empty set set1.insert("the"); //set1 now has one element set1.insert("and"); //set1 now has two elements set<int> set2; //empty set set2.insert(iset.begin(), iset.end()); //set2 now has 10 elements
例如:
// set::insert (C++98) #include <iostream> #include <set> int main () { std::set<int> myset; std::set<int>::iterator it; std::pair<std::set<int>::iterator,bool> ret; // set some initial values: for (int i=1; i<=5; ++i) myset.insert(i*10); // set: 10 20 30 40 50 ret = myset.insert(20); // no new element inserted if (ret.second==false) it=ret.first; // "it" now points to element 20 myset.insert (it,25); // max efficiency inserting myset.insert (it,24); // max efficiency inserting myset.insert (it,26); // no max efficiency inserting int myints[]= {5,10,15}; // 10 already in set, not inserted myset.insert (myints,myints+3); std::cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ‘ ‘ << *it; std::cout << ‘\n‘; return 0; }
结果:
myset contains: 5 10 15 20 24 25 26 30 40 50
4、删除元素
// erasing from set #include <iostream> #include <set> int main () { std::set<int> myset; std::set<int>::iterator it; // insert some values: for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 it = myset.begin(); ++it; // "it" points now to 20 myset.erase (it); myset.erase (40); it = myset.find (60); myset.erase (it, myset.end()); std::cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ‘ ‘ << *it; std::cout << ‘\n‘; return 0; }
结果:
myset contains: 10 30 50
5、查找元素
iset.find(1); //returns iterator that refers to the element with key==1 iset.find(11); //returns iterator == iset.end() iset.count(1); //returns 1; iset.count(11); //returns 0; //set_it refers to the element with key==1 set<int>::iterator set_it = iset.find(1); *set_it=11; //error: keys in a set are read-only cout<<*set_it<<endl; //ok: can read the key
例如:
// set::find #include <iostream> #include <set> int main () { std::set<int> myset; std::set<int>::iterator it; // set some initial values: for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50 it=myset.find(20); myset.erase (it); myset.erase (myset.find(40)); std::cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ‘ ‘ << *it; std::cout << ‘\n‘; return 0; }
结果:
myset contains: 10 30 50
6、其他
#include <iostream> #include <set> using namespace std; int main() { typedef set<int,greater<int> > IntSet; IntSet s1; s1.insert(4); s1.insert(3); s1.insert(5); s1.insert(1); s1.insert(6); s1.insert(2); s1.insert(5); //the inserted element that has the same value with a element existed is emitted copy(s1.begin(),s1.end(),ostream_iterator<int>(cout," ")); cout << endl << endl; pair<IntSet::iterator,bool> status = s1.insert(4); if(status.second) cout << "4 is inserted as element " << distance(s1.begin(),status.first) + 1 << endl; else cout << "4 already exists in s1" << endl; copy(s1.begin(),s1.end(),ostream_iterator<int>(cout," ")); cout << endl << endl; set<int> s2(s1.begin(),s1.end());//default sort criterion is less< copy(s2.begin(),s2.end(),ostream_iterator<int>(cout," ")); cout << endl << endl; }
上述程序最后新产生一个set:s2,默认排序准则是less。以s1的元素作为初值。
注意:s1和s2有不同的排序准则,所以他们的型别不同,不能直接进行相互赋值或比较。
运行结果:
6 5 4 3 2 1 4 already exist in s1 6 5 4 3 2 1 1 2 3 4 5 6
时间: 2024-10-10 05:18:40