Set
1. 所有元素根据 键值自动排序 而键值就是它的值 2.不能改变数据,所以他的迭代器就是 const
2.插入 只能使用 insert() 3.其他方法 : clear() earse() 用法一样 4.set键值不能重复,自动去重
查找
set<int>::iterator pos = s.find(1); cout << *pos << endl;
find: set<int>::iterator it = s.find(1); 返回值为迭代器 就是一个指针,指向 键的位置, 找不到返回 set.end()
cout : set.cout( 1 ) 查找1 的个数 在set中不是1就是0
lower_bound: set<int>::iterator pos2=s lower_bound(3); 返回第一个大于等于3的迭代器 ,找不到返回 set.end()
upper_bound : 返回第一个大于3的迭代器 ,找不到返回 set.end()
对组
1. pair<set<int>::iterator it, set<int>::iterator it2> ret = s.equal_range(3); 就是两个迭代器
set<int>::iterator lower_bound = ret.first set<int>::iterator upper_bound= ret.second
2. pair<string, int> p(string("wyc"), 15); string name = p.first; int age = p.second;
3.pair<string, int> p2= make_pair ("wyc", 15);.
set排序
插入后不能再排序 , 只有插入前排序
set<int, Mypare> 其中Mypare是一个类 然后重写 operator 方法 插入自定义类型时必须先定义插入规则
Map
根据key 排序, 一个key对一个value , 但是可以有重复的value 。 map<int , int > m;
插入
1. m.insert( pair<int , int> (1,5) ) 2. m.insert( make_pair(1,5) ) 3.m[10] = 5;
遍历
for(map<int,int>::iterator it=m.begin();it!=m.end();it++){
cout << "key=" << it->first << "value=" << it->second << endl; }
删除
1.删除指定迭代器位置的 m.erase(m.begin()); 2.删除一个区间m.erase(m.begin(),m.end()); 3.删除指定key m.erase(1);
查找
map<int, int> iterator pos = m.find( key) 返回迭代器,找不到返回m.end();
m.count() 要么1 要么0
lower_bround upper_bround() 排序用法一致
list set map 不恩随机访问 vector deque 可以
vector 尾部插入 deque 头尾都可以
Lambda表达式.
[ ] ( ){ }
for_each(m.begin(), m.end(), [](int value) { std::cout << value << endl; })