1、erase——从容器中删除某个元素或者某个区间
#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);
// erase '2'
s.erase(2);
print(s);
set<int, less<int> >::iterator It;
It = s.find(5);
// erase '5'
s.erase(It);
print(s);
It = s.find(4);
// erase from It to the end of set
s.erase(It,s.end());
print(s);
return 0;
}
运行结果:
// 1 2 3 4 5 6 8
// 1 3 4 5 6 8
// 1 3 4 6 8
// 1 3
2、find——在容器中查找指定元素
#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();
It = s.find(m);
if ( It == s.end() )
cout << "element not found" << endl;
else
{
cout << "element is found : ";
(*It).print();
}
return 0;
}
运行结果:
// Bill Amstrong
// Linda Bain
// Robert Frost
// John Smith
// element is found : Robert Frost
3、insert——向容器中插入一个、多个或者一组元素
#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(10);
print(s);
s.insert(ary,ary+5);
print(s);
set<int, less<int> >::iterator It = s.begin();
s.insert(It,20);
print(s);
return 0;
}
运行结果:
// 10
// 1 2 3 10
// 1 2 3 10 20
4、upper_bound、lower_bound——根据指定关键词key,在容器中搜索小于(upper)或者大于等于(lower)key的元素,并返回搜索结果的迭代器
#include <iostream>
#include <set>
#include <iomanip>
#include <string>
using namespace std;
template <class T>
class Member
{
public:
Member(T l) : last(l), first("") {} // for upper_bound
// and lower_bound
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;
S s;
s.insert(M("Smith","John"));
s.insert(M("Shevchenko","Taras"));
s.insert(M("Amstrong","Bill"));
s.insert(M("Bain","Linda"));
s.insert(M("Pushkin","Alexander"));
s.insert(M("Pasternak","Biris"));
S::iterator It = s.begin();
while ( It != s.end() )
(It++)->print();
cout << endl;
M m1("P");
M m2("Pzz");
S::iterator low = s.lower_bound(m1);
S::iterator upp = s.upper_bound(m2);
It = low;
while ( It != upp )
(It++)->print();
return 0;
}
运行结果:
// Bill Amstrong
// Linda Bain
// Biris Pasternak
// Alexander Pushkin
// Taras Shevchenko
// John Smith
//
// Biris Pasternak
// Alexander Pushkin
5、max_size——容器能申请到的最大容量
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
void print (set<int, less<int> >& s)
{
copy(s.begin(),s.end(),
ostream_iterator<int>(cout," "));
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 << "size of set 's' = "
<< s.size() << endl;
cout << "max_size of 's' = "
<< s.max_size() << endl;
return 0;
}
运行结果:
// 1 2 3 4 5 6 8
// size of set 's' = 7
// max_size of 's' = 4294967295
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。



