- map<int, string> mapStudent;
map<int, string> mapStudent;
- map<int, string> mapStudent;
map<int, string> mapStudent;
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;//pair<int,string>p;p=make_pair(v1,v2);<span style="color: rgb(255, 0, 0); font-family: Arial; font-size: 13px; background-color: rgb(240, 248, 255);"> </span>
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;//pair<int,string>p;p=make_pair(v1,v2);<span style="color: rgb(255, 0, 0); font-family: Arial; font-size: 13px; background-color: rgb(240, 248, 255);"> </span>
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
- make_pair()//返回类型为对应的pair类型
- 无需写出类别,就可以生成一个pair对象
- 例:
- make_pair(1,'@')
- 而不必费力的写成
- pair<int ,char>(1,'@')
make_pair()//返回类型为对应的pair类型
无需写出类别,就可以生成一个pair对象
例:
make_pair(1,'@')
而不必费力的写成
pair<int ,char>(1,'@')
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- mapStudent.insert(map<int, string>::value_type (2, "student_two"));
- mapStudent.insert(map<int, string>::value_type (3, "student_three"));
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
mapStudent.insert(map<int, string>::value_type (2, "student_two"));
mapStudent.insert(map<int, string>::value_type (3, "student_three"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[2] = "student_two";
- mapStudent[3] = "student_three";
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[2] = "student_two";
mapStudent[3] = "student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
- mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- mapStudent.insert(map<int, string>::value_type (1, "student_two"));
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
mapStudent.insert(map<int, string>::value_type (1, "student_two"));
- Pair<map<int, string>::iterator, bool> Insert_Pair;
- Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));
Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- Pair<map<int, string>::iterator, bool> Insert_Pair;
- Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
- If(Insert_Pair.second == true)
- {
- cout<<"Insert Successfully"<<endl;
- }
- Else
- {
- cout<<"Insert Failure"<<endl;
- }
- Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
- If(Insert_Pair.second == true)
- {
- cout<<"Insert Successfully"<<endl;
- }
- Else
- {
- cout<<"Insert Failure"<<endl;
- }
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
If(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
Else
{
cout<<"Insert Failure"<<endl;
}
Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
If(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
Else
{
cout<<"Insert Failure"<<endl;
}
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[1] = "student_two";
- mapStudent[2] = "student_three";
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[1] = "student_two";
mapStudent[2] = "student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
- int nSize = mapStudent.size();
int nSize = mapStudent.size();
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- map<int, string>::reverse_iterator iter;
- for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
map<int, string>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- int nSize = mapStudent.size()
- //此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++)
- //by rainfish
- for(int nIndex = 0; nIndex < nSize; nIndex++)
- {
- cout<<mapStudent[nIndex]<<end;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
int nSize = mapStudent.size()
//此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++)
//by rainfish
for(int nIndex = 0; nIndex < nSize; nIndex++)
{
cout<<mapStudent[nIndex]<<end;
}
}
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- map<int, string>::iterator iter;
- iter = mapStudent.find(1);
- if(iter != mapStudent.end())
- {
- cout<<"Find, the value is "<<iter->second<<endl;
- }
- Else
- {
- cout<<"Do not Find"<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
{
cout<<"Find, the value is "<<iter->second<<endl;
}
Else
{
cout<<"Do not Find"<<endl;
}
}
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[3] = "student_three";
- mapStudent[5] = "student_five";
- map<int, string>::iterator iter;
- iter = mapStudent.lower_bound(2);
- {
- //返回的是下界3的迭代器
- cout<<iter->second<<endl;
- }
- iter = mapStudent.lower_bound(3);
- {
- //返回的是下界3的迭代器
- cout<<iter->second<<endl;
- }
- iter = mapStudent.upper_bound(2);
- {
- //返回的是上界3的迭代器
- cout<<iter->second<<endl;
- }
- iter = mapStudent.upper_bound(3);
- {
- //返回的是上界5的迭代器
- cout<<iter->second<<endl;
- }
- Pair<map<int, string>::iterator, map<int, string>::iterator> mapPair;
- mapPair = mapStudent.equal_range(2);
- if(mapPair.first == mapPair.second)
- {
- cout<<"Do not Find"<<endl;
- }
- Else
- {
- cout<<"Find"<<endl;
- }
- mapPair = mapStudent.equal_range(3);
- if(mapPair.first == mapPair.second)
- {
- cout<<"Do not Find"<<endl;
- }
- Else
- {
- cout<<"Find"<<endl;
- }
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[3] = "student_three";
mapStudent[5] = "student_five";
map<int, string>::iterator iter;
iter = mapStudent.lower_bound(2);
{
//返回的是下界3的迭代器
cout<<iter->second<<endl;
}
iter = mapStudent.lower_bound(3);
{
//返回的是下界3的迭代器
cout<<iter->second<<endl;
}
iter = mapStudent.upper_bound(2);
{
//返回的是上界3的迭代器
cout<<iter->second<<endl;
}
iter = mapStudent.upper_bound(3);
{
//返回的是上界5的迭代器
cout<<iter->second<<endl;
}
Pair<map<int, string>::iterator, map<int, string>::iterator> mapPair;
mapPair = mapStudent.equal_range(2);
if(mapPair.first == mapPair.second)
{
cout<<"Do not Find"<<endl;
}
Else
{
cout<<"Find"<<endl;
}
mapPair = mapStudent.equal_range(3);
if(mapPair.first == mapPair.second)
{
cout<<"Do not Find"<<endl;
}
Else
{
cout<<"Find"<<endl;
}
}
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- //如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好
- //如果要删除1,用迭代器删除
- map<int, string>::iterator iter;
- iter = mapStudent.find(1);
- mapStudent.erase(iter);
- //如果要删除1,用关键字删除
- int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
- //用迭代器,成片的删除
- //一下代码把整个map清空
- mapStudent.earse(mapStudent.begin(), mapStudent.end());
- //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
- //自个加上遍历代码,打印输出吧
- }
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
//如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好
//如果要删除1,用迭代器删除
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);
//如果要删除1,用关键字删除
int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
//用迭代器,成片的删除
//一下代码把整个map清空
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
//自个加上遍历代码,打印输出吧
}
- #include <map>
- #include <string>
- uing namespace std;
- Typedef struct tagStudentInfo
- {
- int nID;
- String strName;
- }StudentInfo, *PStudentInfo; //学生信息
- int main()
- {
- int nSize;
- //用学生信息映射分数
- map<StudentInfo, int>mapStudent;
- map<StudentInfo, int>::iterator iter;
- StudentInfo studentInfo;
- studentInfo.nID = 1;
- studentInfo.strName = "student_one"
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
- studentInfo.nID = 2;
- studentInfo.strName = "student_two";
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
- for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
- cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;
- }
#include <map>
#include <string>
uing namespace std;
Typedef struct tagStudentInfo
{
int nID;
String strName;
}StudentInfo, *PStudentInfo; //学生信息
int main()
{
int nSize;
//用学生信息映射分数
map<StudentInfo, int>mapStudent;
map<StudentInfo, int>::iterator iter;
StudentInfo studentInfo;
studentInfo.nID = 1;
studentInfo.strName = "student_one"
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
studentInfo.nID = 2;
studentInfo.strName = "student_two";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;
}
- Typedef struct tagStudentInfo
- {
- int nID;
- String strName;
- Bool operator < (tagStudentInfo const& _A) const
- {
- //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序
- If(nID < _A.nID) return true;
- If(nID == _A.nID) return strName.compare(_A.strName) < 0;
- Return false;
- }
- }StudentInfo, *PStudentInfo; //学生信息
Typedef struct tagStudentInfo
{
int nID;
String strName;
Bool operator < (tagStudentInfo const& _A) const
{
//这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序
If(nID < _A.nID) return true;
If(nID == _A.nID) return strName.compare(_A.strName) < 0;
Return false;
}
}StudentInfo, *PStudentInfo; //学生信息
- #include <map>
- #include <string>
- using namespace std;
- Typedef struct tagStudentInfo
- {
- int nID;
- String strName;
- }StudentInfo, *PStudentInfo; //学生信息
- class sort
- {
- Public:
- Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
- {
- If(_A.nID < _B.nID) return true;
- If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
- Return false;
- }
- };
- int main()
- {
- //用学生信息映射分数
- map<StudentInfo, int, sort>mapStudent;
- StudentInfo studentInfo;
- studentInfo.nID = 1;
- studentInfo.strName = "student_one";
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
- studentInfo.nID = 2;
- studentInfo.strName = "student_two";
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
- }
#include <map>
#include <string>
using namespace std;
Typedef struct tagStudentInfo
{
int nID;
String strName;
}StudentInfo, *PStudentInfo; //学生信息
class sort
{
Public:
Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
{
If(_A.nID < _B.nID) return true;
If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
Return false;
}
};
int main()
{
//用学生信息映射分数
map<StudentInfo, int, sort>mapStudent;
StudentInfo studentInfo;
studentInfo.nID = 1;
studentInfo.strName = "student_one";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
studentInfo.nID = 2;
studentInfo.strName = "student_two";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
}
map 是一种关联容器, 提供一对一的关联, 关联的形式为: KEY----VALUE 关键字不重复。multimap与map类似,但是允许关键字重复
即:关键字和与之对应的值
关键字起到索引的作用, 在map中查找记录 就是根据关键字查找
关键字 和 值 可以是任意类型
map 也可看做是 关键字映射的集合, 即,map中不可出现重复的关键字,每条映射的关键字都是不同的。
map 是基于红黑树结构的,其查找时间为LOG(N)
如:
map<int, int > //第一个为关键字,第二个为此关键字所对应的值 一个关键字只对应一个值, 是一对一的映射关系
map<CString,int>
map<int, CString>
map<CString,CString>
........
头文件
- #include <map>
- using namespace std; //必须加上
#include <map>
using namespace std; //必须加上
1 插入元素
1) insert函数插入
- map<int,int> idMap;
- idMap.insert(pair<int,int>(1,1));
- idMap.insert(map<int,int>::value_type(2,1));
map<int,int> idMap;
idMap.insert(pair<int,int>(1,1));
idMap.insert(map<int,int>::value_type(2,1));
用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的
判断是否插入成功
- map<int,int> idMap;
- idMap.insert(pair<int,int>(1,1));
- idMap.insert(map<int,int>::value_type(2,1));
- pair<map<int,int>::iterator,bool> InsertPair;
- InsertPair=idMap.insert (pair<int,int>(1,1));
- if (InsertPair.second==true)
- {
- cout<<"insert successfully";
- }else
- cout<<"insert failure";
map<int,int> idMap;
idMap.insert(pair<int,int>(1,1));
idMap.insert(map<int,int>::value_type(2,1));
pair<map<int,int>::iterator,bool> InsertPair;
InsertPair=idMap.insert (pair<int,int>(1,1));
if (InsertPair.second==true)
{
cout<<"insert successfully";
}else
cout<<"insert failure";
2 )数组插入方式
- map<int,int> idMap;
- idMap[1]=2;
map<int,int> idMap;
idMap[1]=2;
用数组方式就不同了,它可以覆盖以前该关键字对应的值
但存在一个性能的问题。该方法会将每个插入值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。而用insert方法则可直接赋值为显示值。
2 判断是否存在
Returns the number of elements in a map whose key matches a parameter-specified key.
size_type count(
const Key& _Key
) const |
1 if the map contains an element whose sort key matches the parameter key; 0 if the map does not contain an element with a matching key.
- int num=idMap.count(1);
- if (num==0)
- {
- cout<<"the key 1 does not exist";
- }else{
- cout<<"exist";
- }
int num=idMap.count(1);
if (num==0)
{
cout<<"the key 1 does not exist";
}else{
cout<<"exist";
}
3 查找关键字
iterator find( const Key& _Key ); const_iterator find( const Key& _Key ) const; |
- map<int,int>::iterator it;
- it=idMap.find(2);
- if (it==idMap.end())
- {
- cout<<"can not find 2";
- }else{
- int first=it->first;
- int second=it->second;
- }
map<int,int>::iterator it;
it=idMap.find(2);
if (it==idMap.end())
{
cout<<"can not find 2";
}else{
int first=it->first;
int second=it->second;
}
扩展:
iterator lower_bound( const Key& _Key ); const_iterator lower_bound( const Key& _Key ) const; |
Returns an iterator to the first element in a map with a key value that is equal to or greater than that of a specified key.
第一个等于或大于Key的元素
iterator upper_bound( const Key& _Key ); const_iterator upper_bound( const Key& _Key ) const; |
Returns an iterator to the first element in a map that with a key having a value that is greater than that of a specified key.
第一个大于Key的元素 pair <const_iterator, const_iterator> equal_range ( const Key& _Key ) const; pair <iterator, iterator> equal_range ( const Key& _Key ); |
A pair of iterators such that the first is the lower_bound of the key and the second is the upper_bound of the key.
Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字
- p2 = m1.equal_range( 4 );
- // If no match is found for the key,
- // both elements of the pair return end( )
- if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
- cout << "The map m1 doesn't have an element "
- << "with a key less than 40." << endl;
- else
- cout << "The element of map m1 with a key >= 40 is: "
- << p2.first -> first << "." << endl;
p2 = m1.equal_range( 4 );
// If no match is found for the key,
// both elements of the pair return end( )
if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
cout << "The map m1 doesn't have an element "
<< "with a key less than 40." << endl;
else
cout << "The element of map m1 with a key >= 40 is: "
<< p2.first -> first << "." << endl;
4 大小,包含多少个元素
Returns the number of elements in the map.
size_type size( ) const; |
- int nSize=idMap.size();
int nSize=idMap.size();
5 遍历
前向迭代器
- map<int,int>::iterator it;
- for (it=idMap.begin ();it!=idMap.end();it++)
- {
- cout<<it->first<<endl;
- cout<<it->second<<endl;
- }
map<int,int>::iterator it;
for (it=idMap.begin ();it!=idMap.end();it++)
{
cout<<it->first<<endl;
cout<<it->second<<endl;
}
反向迭代器
- map<int,int>::reverse_iterator iter;
- for (iter=idMap.rbegin ();iter!=idMap.rend ();iter++)
- {
- cout<<iter->first<<endl;
- cout<<iter->second<<endl;
- }
map<int,int>::reverse_iterator iter;
for (iter=idMap.rbegin ();iter!=idMap.rend ();iter++)
{
cout<<iter->first<<endl;
cout<<iter->second<<endl;
}
6 删除
iterator erase( iterator _Where ); iterator erase( iterator _First, iterator _Last ); size_type erase( const key_type& _Key ); |
- //迭代器删除
- map<int,int>::iterator it;
- it=idMap.find(1);
- idMap.erase(it);
- //关键字删除
- idMap.erase(1);
- //成片删除 或清空
- idMap.erase(idMap.begin (),idMap.end());
- //清空
- idMap.clear ();
//迭代器删除
map<int,int>::iterator it;
it=idMap.find(1);
idMap.erase(it);
//关键字删除
idMap.erase(1);
//成片删除 或清空
idMap.erase(idMap.begin (),idMap.end());
//清空
idMap.clear ();
7 基本函数
- C++ Maps是一种关联式容器,包含“关键字/值”对
- begin() 返回指向map头部的迭代器
- clear() 删除所有元素
- count() 返回指定元素出现的次数
- empty() 如果map为空则返回true
- end() 返回指向map末尾的迭代器
- equal_range() 返回特殊条目的迭代器对
- erase() 删除一个元素
- find() 查找一个元素
- get_allocator() 返回map的配置器
- insert() 插入元素
- key_comp() 返回比较元素key的函数
- lower_bound() 返回键值>=给定元素的第一个位置
- max_size() 返回可以容纳的最大元素个数
- rbegin() 返回一个指向map尾部的逆向迭代器
- rend() 返回一个指向map头部的逆向迭代器
- size() 返回map中元素的个数
- swap() 交换两个map
- upper_bound() 返回键值>给定元素的第一个位置
- value_comp() 返回比较元素value的函数
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
示例:
- map<int,vector<int>> m_DianmingMap;
map<int,vector<int>> m_DianmingMap;
- AddDianmingMap(int nXueqi,int nXuehao)
- {
- //将此学生添加到已点名容器中
- map<int,vector<int>>::iterator it;
- it=m_DianmingMap.find(nXueqi);
- if (it==m_DianmingMap.end ()) //先查找关键字有无此学期ID
- {
- //容器中不存在 则添加
- vector<int> int_Vec;
- int_Vec.push_back(nXuehao);
- m_DianmingMap[nXueqi]=int_Vec;
- }else{//在查找 此学期中 有无此学号ID
- vector<int>::iterator itVec=find(it->second.begin (),it->second.end(),m_nXuehaoID);
- if(itVec==it->second.end())
- {
- //没有此学生则添加
- it->second.push_back(nXuehao);
- }
- }
- return TRUE;
- }