STL 关联容器
来源:互联网 发布:卢松松博客php版 编辑:程序博客网 时间:2024/05/20 23:30
创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,second成员初始化为v2
make_pair(v1, v2)以v1和v2值创建一个新的pair对象,其元素类型分别是v1和v2的类型p1 < p2字典次序:如果p1.first<p2.first或者!(p2.first < p1.first)&& p1.second<p2.second,则返回truep1 == p2如果两个pair对象的first和second成员依次相等,则这两个对象相等。p.first返回p中名为first的(公有)数据成员p.second返回p中名为second的(公有)数据成员map类型
const map<K, V>::key_type类型,而second元素
则为map<K, V>::mapped_type类型
例如:
#include<iostream>#include<map>using namespace std;int main(){map<char,int> mymap;map<char,int>::iterator it;pair<map<char,int>::iterator,bool> ret1;pair<map<char,int>::iterator,map<char,int>::iterator> ret;//插入元素mymap['a']=10;mymap['b']=20;mymap['c']=30;mymap['d']=40;ret1=mymap.insert(pair<char,int>('c',500));if (ret1.second==false){cout << "element 'c' already existed";cout << " with a value of " << ret1.first->second << endl;}//寻找、删除it=mymap.find('d');mymap.erase (it);//边界ret = mymap.equal_range('b');cout << "lower bound points to: ";cout << ret.first->first << " => " << ret.first->second << endl;cout << "upper bound points to: ";cout << ret.second->first << " => " << ret.second->second << endl;// 输出容器元素cout << "mymap contains:\n";for ( it=mymap.begin() ; it != mymap.end(); it++ )cout << (*it).first << " => " << (*it).second << endl;return 0;}
该函数返回一个pair类型对象,如果发生了插入动作,则返回pair(it, true);否则返回pair(it, false)。其中,it是指向键为e.first那个元素的迭代器。m.insert(beg, end)
beg和end是标记元素范围的迭代器,其中的元素必须为value_type类型的键-值对。对于该范围内的所有元素,如果它的键在m中不存在,则将该键及其关联的值插入到m。返回void类型。
m.insert(iter, e)insert(e),并以iter为起点搜索新元素的位置。返回一个迭代器,指向m中键为e.first的元素。如果不存在,则返回end()值。
set类型
multimap和multiset类型
m.lower_bound(k),而second成员则等价于
m.upper_bound(k)
lower_bound()是binary_search()的特殊形式. 此函数搜索给定的序列[first, end)中val可插入的第一个位置; 或者说, 它返回序列中遇到的第一个不小于val的元素的迭代器, 如果所有元素都小于val则返回“end”. 从函数要求待搜索序列是有序的.
lower_bound()的返回值乃是一个指向val可以安全插入的位置的迭代器. 在比较函数f被指定之前, 默认使用<操作符进行排序.
例如, 下面的代码借助lower_bound()将7插入到一个已排序好的vector中:
#include<iostream> #include <algorithm>#include<vector>using namespace std;int main(){vector<int> nums;nums.push_back(-242);nums.push_back(-1);nums.push_back(0);nums.push_back(5);nums.push_back(8);nums.push_back(8);nums.push_back(11); cout<<"Before nums is: ";for(unsigned int i=0;i<nums.size();i++)cout<<nums[i]<<" ";cout<<endl; vector<int>::iterator result;int new_val=7; result=lower_bound(nums.begin(),nums.end(),new_val); nums.insert(result,new_val); cout << "After, nums is: ";for(i = 0; i < nums.size(); i++ )cout << nums[i] << " ";cout << endl;return 0;}
输出结果为:
Before nums is: -242 -1 0 5 8 8 11
After, nums is: -242 -1 0 5 7 8 8 11
Press any key to continue
upper_bound用法和lower_bound相似。
在没有map和set时候要用到这三个函数要声明算法的头文件
函数equal_range()返回first和last之间等于val的元素区间. 此函数假定first和last区间内的元素可以使用<操作符或者指定的comp执行比较操作.
equal_range()可以被认为是lower_bound和upper_bound的结合, pair中的第一个迭代器由lower_bound返回, 第二个则由upper_bound返回.
例如, 下面的代码使用equal_range()探测一个有序的vector中的可以插入数字8的位置:
#include<iostream> #include <algorithm>#include<vector>using namespace std;int main(){vector<int> nums;nums.push_back( -242 );nums.push_back( -1 );nums.push_back( 0 );nums.push_back( 5 );nums.push_back( 8 );nums.push_back( 8 );nums.push_back( 11 );pair<vector<int>::iterator, vector<int>::iterator> result;int new_val = 8; result = equal_range( nums.begin(), nums.end(), new_val); cout<< "The first place that " << new_val<< " could be inserted is before " << *result.first<< ", and the last place that it could be inserted is before " << *result.second<< endl;return 0;}
输出结果为:
The first place that 8 could be inserted is before 8, and the last place that it could be inserted is before 11
下面是一个map例子
#pragma warning (disable : 4786)#include<iostream>#include<cstdio>#include<string>#include<map>using namespace std;const int N = 100;struct student{string name;int id; string blog;}s[N];int main(){int i,n;map<string,int>m;//用 name 和 结构体数组下标 建立映射m.clear();//清空map,判空用empty()//freopen("data.in","r",stdin);//freopen("my.out","w",stdout);scanf("%d",&n);for(i=1;i<=n;i++){cin>>s[i].name>>s[i].id>>s[i].blog;/*************第一种插入方式************/m.insert(pair<string,int>(s[i].name,i));/*************第二种插入方式************///m.insert(map<string,int>::value_type(s[i].name,i));/*************第三种插入方式************///m[s[i].name]=i;}/*********** map的大小 ************/cout<<"元素个数"<<":"<<" "<<m.size()<<endl;cout<<endl;/************遍历操作1: 前向迭代器*************map<string,int>::iterator p;for(p=m.begin();p!=m.end();p++){int k=p->second;cout<<p->first<<" "<<s[k].id<<" "<<s[k].blog<<endl;}*//************遍历操作2: 反向迭代器*************map<string,int>::reverse_iterator p;for(p=m.rbegin();p!=m.rend();p++){int k=p->second;cout<<p->first<<" "<<s[k].id<<" "<<s[k].blog<<endl;}**********************************************//************遍历操作3: 数组遍历*************/for(int pi=1;pi<=m.size();pi++){cout<<s[pi].name<<" "<<s[pi].id<<" "<<s[pi].blog<<endl;}/************************查找*******************************//********** m.count() **********/if(!m.count("Lch"))cout<<"No answer!"<<endl;else cout<<"Yes,you got it!"<<endl;/******* m.find()函数 ********map<string,int>::iterator p;p=m.find("Lch");if(p!=m.end()){int k=p->second;cout<<p->first<<" "<<s[k].id<<" "<<s[k].blog<<endl;}else cout<<"No answer!!!!"<<endl;*******************************//*** m.lower_bound 和 m.upper_bound ***/map<string,int>::iterator p1,p2;p2=m.lower_bound("Lch");p1=m.upper_bound("Lch");if(p1==p2)cout<<"No answer!"<<endl;else cout<<"Yes,you got it!"<<endl;/*********************** 删除 *******************************//************* erase() **************///按关键字删除//int x=m.erase("Lch");//cout<<x<<endl;//迭代器删除map<string,int>::iterator p;p=m.find("Lch");m.erase(p);//区间删除m.erase(m.begin(),m.end());if(!m.count("Lch"))cout<<"No answer!"<<endl;else cout<<"Yes,you got it!"<<endl;return 0;}
以上资料整理于网络
- STL关联容器
- STL::map关联容器
- STL关联容器概述
- STL 关联容器
- STL关联容器--Set
- STL中关联容器
- STL 之关联容器
- Effective STL --关联容器
- STL关联容器
- STL之关联容器
- STL — 关联容器
- STL-关联式容器
- stl关联容器
- STL关联容器概述
- STL关联容器
- 【STL】关联式容器
- STL容器之关联容器
- STL容器--关联容器map
- windows下练习linux编程的软件-cygwin的用法(超简单)
- GPG(pgp)加解密中文完整教程
- poj 2016 Play on Words--欧拉通路
- poj1401
- HDU 1506 Largest Rectangle in a Histogram
- STL 关联容器
- 开发资源汇总
- poj 1730 Perfect Pth Powers
- ORACLE 10进制转换2进制
- shell的expr命令
- 通过Launcher里的WorkSpace完成桌面的3D转屏效果(有源码)
- 指针和引用的区别 .
- c++学习笔记
- hdu1285拓扑排序