参考链接:http://www.cplusplus.com/reference/set/set/find/
顺序容器 | 顺序容器适配器 | 关联容器 |
---|---|---|
vector(支持快速随机访问) | stack(LIFO栈) | set(红黑树,高效的平衡检索二叉树) |
list(支持快速插入/删除) | queue(FIFO队列) | map |
deque(双端队列) | priority_queue(有优先级管理的队列) |
顺序容器提供了快速顺序访问元素的能力。
关联容器支持高效的关键字查找和访问。
// set::begin/end
#include
#include
int main ()
{
int myints[] = {75,23,65,42,13};
std::set<int> myset (myints,myints+5);
std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
//Output:
//myset contains: 13 23 42 65 75
// set::begin/end
#include
#include
int main ()
{
int myints[] = {75,23,65,42,13};
std::set<int> myset (myints,myints+5);
std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
//Output:
//myset contains: 13 23 42 65 75
#include
#include
using namespace std;
int main(){
set<int> mySet;
mySet.insert(5);
mySet.insert(3);
mySet.insert(4);
mySet.insert(4);
cout << "mySet contains:";
for(set<int>::iterator it=mySet.begin(); it!=mySet.end(); ++it){
cout<< ' ' << *it;
}
cout << '\n';
return 0;
}
//Output:
//mySet contains: 3 4 5
#include
#include
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;
}
//Output:
//myset contains: 5 10 15 20 24 25 26 30 40 50
#include
#include
int main ()
{
std::set<int> myset;
myset.insert (100);
myset.insert (200);
myset.insert (300);
std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
myset.clear();
myset.insert (1101);
myset.insert (2202);
std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
//Output//
//myset contains: 100 200 300
//myset contains: 1101 2202
#include
#include
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;
}
//Output
//myset contains: 10 30 50
#include
#include
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;
}
//Output:
//myset contains: 10 30 50
#include
#include
int main ()
{
std::set<int> myints;
std::cout << "0. size: " << myints.size() << '\n';
for (int i=0; i<10; ++i) myints.insert(i);
std::cout << "1. size: " << myints.size() << '\n';
myints.insert (100);
std::cout << "2. size: " << myints.size() << '\n';
myints.erase(5);
std::cout << "3. size: " << myints.size() << '\n';
return 0;
}
//Output:
/*
0. size: 0
1. size: 10
2. size: 11
3. size: 10
*/
#include
#include
main ()
{
int myints[]={12,75,10,32,20,25};
std::set<int> first (myints,myints+3); // 10,12,75
std::set<int> second (myints+3,myints+6); // 20,25,32
first.swap(second);
std::cout << "first contains:";
for (std::set<int>::iterator it=first.begin(); it!=first.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "second contains:";
for (std::set<int>::iterator it=second.begin(); it!=second.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
//Output:
/*
first contains: 20 25 32
second contains: 10 12 75
*/