1、构造函数
#include <iostream> #include <set> int main () { int ary[] = { 5,3,7,5,2,3,7,5,5,4 }; set<int> s1; set<int, greater<int> > s2; for ( int i=0; i<sizeof(ary)/sizeof(int); i++ ) { s1.insert(ary[i]); s2.insert(ary[i]); } set<int>::iterator It = s1.begin(); cout << "s1 : "; while ( It != s1.end() ) cout << *(It++) << " "; cout << endl; It = s2.begin(); cout << "s2 : "; while ( It != s2.end() ) cout << *(It++) << " "; cout << endl; // 第二种形式的构造 set<int> s3(ary,ary+3); It = s3.begin(); cout << "s3 : "; while ( It != s3.end() ) cout << *(It++) << " "; cout << endl; //拷贝构造 set<int, less > s4(s1); It = s4.begin(); cout << "s4 : "; while ( It != s4.end() ) cout << *(It++) << " "; cout << endl; return 0; } 运行结果: // s1 : 2 3 4 5 7 // s2 : 7 5 4 3 2 // s3 : 3 5 7 // s4 : 2 3 4 5 7
2、begin——返回容器头
#include <iostream> #include <set> using namespace std; int main () { int ary[] = {1,2,3,2,4,5,7,2,6,8}; set<int> s(ary,ary+10); copy(s.begin(),s.end(), ostream_iterator<int>(cout," ")); return 0; } 运行结果: // 1 2 3 4 5 6 7 8
3、clear——容器就像一个盒子,clear便是拿走盒子里面的东西
#include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); s.clear(); cout << "Size of set s = " << s.size() << endl; print(s); return 0; } 运行结果: // 1 2 3 4 5 6 8 // Size of set s = 0
4、count——返回容器中指定元素的个数
#include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); cout << "count of '2' (0 or 1) is "; int n = s.count(2); cout << n << endl; 返回first和last之间等于val的元素区间. 此函数假定first和last区间内的元素可以使用<操作符或者指定的comp执行比较操作. return 0; } 运行结果: // 1 2 3 4 5 6 8 // count of '2' (0 or 1) is 1
5、empty——判断容器内元素是否为空
#include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); cout << "set is " << ((s.empty()) ? "" : "not ") << "empty" << endl; s.clear(); cout << "set is " << ((s.empty()) ? "" : "not ") << "empty" << endl; return 0; } 运行结果: // 1 2 3 4 5 6 8 // set is not empty // set is empty
6、end——容器迭代器最后一个元素的下一个迭代
#include <iostream> #include <set> #include <iomanip> #include <string> using namespace std; template <class T> class Member { public: Member(T l, T f) : last(l), first(f) {} void print() const // const !!! { cout.setf(ios::left); cout << setw(15) << first.c_str() << last << endl; } private: T first, last; // const !!! friend bool operator < (const Member& m1, const Member& m2) { return (m1.last < m2.last) ? true : false; } friend bool operator == (const Member& m1, const Member& m2) { return (m1.last == m2.last) ? true : false; } }; //=============================================== int main () { typedef Member<string> M; typedef set<M, less<M> > S; M m("Frost","Robert"); S s; s.insert(m); s.insert(M("Smith","John")); s.insert(M("Amstrong","Bill")); s.insert(M("Bain","Linda")); S::iterator It = s.begin(); while ( It != s.end() ) (It++)->print(); return 0; } 运行结果: // Bill Amstrong // Linda Bain // Robert Frost // John Smith
<7、equal_range——返回first和last之间等于指定元素的区间. 此函数假定first和last区间内的元素可以使用<操作符或者指定的comp执行比较操作.>
#include <iostream> #include <set> using namespace std; int main () { set<int> c; c.insert(1); c.insert(2); c.insert(4); c.insert(10); c.insert(11); cout << "lower_bound(3): " << *c.lower_bound(3) << endl; cout << "upper_bound(3): " << *c.upper_bound(3) << endl; cout << "equal_range(3): " << *c.equal_range(3).first << " " << *c.equal_range(3).second << endl; cout << endl; cout << "lower_bound(5): " << *c.lower_bound(5) << endl; cout << "upper_bound(5): " << *c.upper_bound(5) << endl; cout << "equal_range(5): " << *c.equal_range(5).first << " " << *c.equal_range(5).second << endl; cin.get(); } 运行结果: // lower_bound(3): 4 // upper_bound(3): 4 // equal_range(3): 4 4 // // lower_bound(5): 10 // upper_bound(5): 10 // equal_range(5): 10 10
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。
哈哈,没想到代码也可以做一篇文章 😀
实在不好意思 这也有些偷懒的缘故 🙂