您的位置:首页 > 编程语言 > C语言/C++

C++ STL学习笔记九 map映照容器

2015-10-12 20:16 453 查看
/*

*

********************************************

* map映照容器的基础说明:

********************************************

*

* map映照容器:容器的数据结构采用红黑树进行管理,插入的元素键值不允许重复

* map的所有元素都是pair:第一元素为键值(key),不能修改;第二元素为实值(value),可被修改

*

* map和list一样,使用insert或erase后,操作前的所有迭代器,在操作完成后依然有效

* []:不仅可用键值的数组方式访问元素的映照数据,还可用来添加map容器的元素 //main中示例

*

* Sorted Associative Container Pair Associative Container Unique Associative Container

*

* 使用map必须使用宏语句#include <map>

*

**************************************************************************************

*

* 创建map对象:

* 1.map<char,int,greater<char> > a; //元素键值类型为char,映照数据类型为int,键值的比较函数对象为greater<char>

* 2.map(const key_compare& comp) //指定一个比较函数对象comp来创建map对象

* 3.map(const map&); //map<int,char*> b(a); //此时使用默认的键值比较函数less<int>

* 4.map(first,last);

* 5.map(first,last,const key_compare& comp);

*

* //Example:

* pair<const int ,char> p1(1,'a');

* pair<const int ,char> p2(2,'b');

* pair<const int ,char> p3(3,'c');

* pair<const int ,char> p4(4,'d');

* pair<const int ,char> pairArray[]={p1,p2,p3,p4};

* map<const int,char> m4(pairArray,pairArray+5);

* map<const int,char> m3(m4);

* map<const int,char,greater<const int> > m5(pairArray,pairArray+5,greater<const int>());

*

**************************************************************************************

*

* 元素的插入

* //typedef pair<const key,T> value_type;

* pair<iterator,bool> insert(const value_type& v);

* iterator insert(iterator pos,const value_type& v);

* void insert(first,last);

*

**************************************************************************************

*

* 元素的删除

* void erase(iterator pos);

* size_type erase(const key_type& k); //删除等于键值k的元素

* void erase(first,last); //删除[first,last)区间的元素

* void clear();

*

**************************************************************************************

*

* 访问与搜索

*

* iterator begin();iterator end(); //企图通过迭代器改变元素是不被允许的

* reverse_iterator rbegin();reverse_iterator rend();

*

* iterator find(const key_type& k) const;

* pair<iterator,iterator> equal_range(const key_type& k) const;//返回的pair对象,

* //first为lower_bound(k);大于等于k的第一个元素位置

* //second为upper_bound();大于k的第一个元素位置

*

* 其它常用函数

* bool empty() const;

* size_type size() const;

* void swap();

*

* iterator lower_bound();iterator upper_bound();pair<iterator,iterator> equal_range();//上界、下届、确定区间

*

*

*

********************************************

** cumirror ** tongjinooo@163.com ** **

********************************************

*

*/

#include <map>

#include <string>

#include <iostream>

// 基本操作与set类似,牢记map中所有元素都是pair

// 对于自定义类,初学者会觉得比较函数如何构造很麻烦,这个可以参照前面的书写示例

// 但若设置键值为int或char类型,无须构造比较函数

struct student{

char* name;

int age;

char* city;

char* phone;

};

struct strCmp{

bool operator () (const char* a,const char* b) const{

return strcmp(a,b)<0;

}

};

struct strCmpBig{

bool operator () (const char* a,const char* b) const{

return strcmp(a,b)>0;

}

};

int main(){

using namespace std;

// 使用[]操作符

map<string,int> animal;

animal[string("fish")]=12;

animal[string("dog")]=10;

animal[string("cat")]=5;

cout<<animal["cat"]<<endl;

// string类有默认的比较函数,故下面的语句可以执行,若换为char*类型,则执行会出现问题

// 迭代器i指向pair类型

for(map<string,int>::iterator i=animal.begin();i!=animal.end();i++){

cout<<"The number of "<<i->first<<" : "<<i->second<<endl;

}

student s[]={

{"童进",23,"武汉","XXX"},

{"老大",23,"武汉","XXX"},

{"饺子",23,"武汉","XXX"}

};

pair<int,student> p1(4,s[0]);

pair<int,student> p2(2,s[1]);

pair<int,student> p3(3,s[2]);

map<int,student> a;

a.insert(p1);

a.insert(p2);

a.insert(p3);

// 按照键值2、3、4进行排列输出

for(map<int,student>::iterator j=a.begin();j!=a.end();j++){

cout<<"The name: "<<j->second.name<<" "<<"age: "<<j->second.age<<" "

<<"city: "<<j->second.city<<" "<<"phone: "<<j->second.phone<<endl;

}

// 思考,这是按照键值进行排列,若我想变换,按照name或者年龄进行重新排列,那么又该如何实现呢?

cout<<"新的排列"<<endl;

pair<const char*,student> q1(s[0].name,s[0]);

pair<const char*,student> q2(s[1].name,s[1]);

pair<const char*,student> q3(s[2].name,s[2]);

student testStu={"AB",23,"武汉","XXX"};

// 为何要采用函数对象的形式,而不只能是函数,这个与C++内部实现机制有关

map<const char*,student,strCmp> b;

b.insert(q1);

b.insert(q2);

b.insert(q3);

// insert函数的测试,观察其放回迭代器的值,可改变名字看看,插入位置视实际情况定

// 返回插入元素的迭代器

pair<const char*,student> q4(testStu.name,testStu);

map<const char*,student,strCmp>::iterator test=b.insert(b.begin()++,q4);

cout<<test->second.name<<endl;

for(map<const char*,student,strCmp>::iterator k=b.begin();k!=b.end();k++){

cout<<"The name: "<<k->second.name<<" "<<"age: "<<k->second.age<<" "

<<"city: "<<k->second.city<<" "<<"phone: "<<k->second.phone<<endl;

}

// 拷贝的时候也可以进行函数对象的设置,那如果函数对象变换了,能实现顺序的变化吗?

// 目前还没实现,不知道具体可行吗?以后再进行测试吧

// 个人观点:不可行。函数对象比较的是key,若重新排列,key不能变换,

// 那么所谓的重新排列,岂不仅仅是反序排列,而反序输出map已经具有了这样的函数了。

cout<<"拷贝时实现重新排列"<<endl; //并未实现

map<const char*,student,strCmp> c(b);

for(map<const char*,student,strCmp/*strCmpBig*/>::iterator m=c.begin();m!=c.end();m++){

cout<<"The name: "<<m->second.name<<" "<<"age: "<<m->second.age<<" "

<<"city: "<<m->second.city<<" "<<"phone: "<<m->second.phone<<endl;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: