根据网上资料初步整理
/*容器map*/
///2015/05/22 20:16第一次整理
#include <iostream>
#include <map>
#include <iterator>
#include <cstdio>
using namespace std;
typedef struct StudentInfo
{
int ID;
string strName;
/*重载小于号*/
bool operator <(StudentInfo const & A) const
{
//制定排序策略,按ID排序,如果ID相等,按strName排序
if(ID < A.ID) return true;
if(ID == A.ID) return (strName.compare(A.strName) < 0);//compare函数,当第一个小于第二个返回负数,相等返回0,否则返回正数
return false;
}
}Stu,*PStu;
int main()
{
map<string,int> k;
pair<map<string,int>::iterator,bool> flag;//对组,bool值用来判断insert操作是否成功
map<string,int> ::iterator ite;//前向迭代器
///插入(3种)
flag = k.insert(pair<string,int>("wl",1));//第一种插入方式
if(flag.second == true)//insert成功
cout << "ok" << endl;
else
cout << "no"<< endl;
flag = k.insert(map<string,int>::value_type("wl",2));//第二种插入方式
if(flag.second == true)
cout << "ok" << endl;
else
cout << "no"<< endl;
/*输出结果:
ok
no
*/
//insert操作不插入主键值相同的元素
k["wl"] = 3;//将主键值为"wl"的元素第二键值改为3;
k["o"] = 3;//不存在主键值为“o”的元素,将会自动创建这个元素;相当于插入这个元素;
///遍历(3种)
for(ite = k.begin(); ite != k.end(); ++ite) //记得加括号,前向遍历
{
cout << ite -> first <<" " << ite -> second << endl;
}
map<string,int>::reverse_iterator iter; //反相迭代器
for(iter = k.rbegin(); iter != k.rend(); ++iter)
{
cout << iter -> first <<" " << iter -> second << endl;//反相遍历,和前向迭代器的输出效果刚好相反
}
//迭代器貌似只能自加
//还可以用数组的方法来遍历容器,但是主键值必须连续
///查找(3种)
/*2015.05.22 20:40 第二次整理 完*/
cout << k.count("wl") << endl;//count返回值为1则存在这个元素
cout << k.count("w") << endl;//0代表不存在
ite = k.find("wl");
if(ite != k.end())//如果找到,则返回值为该元素存在的位置的迭代器,否则为end()返回的迭代器
{
cout <<"find:" << ite -> second << endl;//注意没有括号
}
else
{
cout << "don't find!" << endl;
}
map<int,string> l;
map<int,string> ::iterator ite1;
pair<map<int,string>::iterator,bool> flag1;
l[1] = "a";
l[7] = "c";
l[5] = "b";
//调用lower_bound之前必须确定序列为有序序列,
//lower_bound(key) 返回一个 iterator 指向指向键值>= key的第一个元素
//upper_bound(key) 返回一个 iterator,指向键值> key的第一个元素
ite1 = l.lower_bound(5);
cout << ite1 -> first << endl;//输出5
ite1 = l.upper_bound(5);
cout << ite1 -> first << endl;//输出7
//所以若 lower_bound(key) == upper_bound(key)则不存在key元素
pair<map<int,string>::iterator,map<int ,string>::iterator> mapPair;
mapPair = l.equal_range(3);//返回值即为pair<lower_bound(key),upper_bound(key)>
if(mapPair.first == mapPair.second)
{
cout <<"do not find"<< endl;
}
else
{
cout << "find" << endl;
}
///清空、判空
l.clear();
if(l.empty())
cout << "empty!"<<endl;
else
cout << "clear fail"<<endl;
///删除
l[1] = "example";
ite1 = l.find(1);
l.erase(ite1);
l[1] = "example";
int nFlag = l.erase(1);//只有形参为主键值时erase函数才有返回值
cout << nFlag << endl;//1代表删除,否则返回0
l[1] = "example";
l.erase(l.begin(),l.end());//前闭后开
///排序(默认从小到大)
//结构体排序(必须重载小于运算符,不重载insert函数编译不会通过)
int nsize;
map<Stu,int>mapStu;
map<Stu,int>::iterator ite2;
Stu sStu;
sStu.ID = 1;
sStu.strName = "stu_one";
mapStu.insert(pair<Stu,int>(sStu,95));
sStu.ID = 1;
sStu.strName = "stu_two";
mapStu.insert(pair<Stu,int>(sStu,80));
for(ite2 = mapStu.begin();ite2 != mapStu.end();ite2++)
cout << ite2->first.strName << " " << ite2->second << endl;//注意指针操作符和结构体成员运算符的区别
return 0;
}