C++:Map

参考博文:http://blog.csdn.net/iicy266/article/details/11906189?utm_source=tuicool&utm_medium=referral

  Map是c++的一个标准容器,提供了很好一对一的关系,里面的数据都是成对出现的,每一对中的第一个为关键字(key),每个关键字只能在map中出现一次,第二个为该关键字的对应值,通常只能修改实值而不能修改key。通常有下面的几种操作:

一、声明

#include <map>

map<int, string> person;

二、数据插入
  map插入数据,通常有三种方法:
    1、使用insert插入pair数据
    2、使用insert插入value_type数据
    3、插入数组

person.insert(pair<int, string>(1, "詹姆斯"));              // 插入pair数据
person.insert(map<int, string>::value_type(2, "杜兰特"));   // 插入value_type数据
person[3] = "未名湖畔的落叶";                                // 插入数组

  如果两条插入数据的key相同,只有第一个的插入的才能成功,其中可以用pair来判断是否插入成功,如果插入成功的话nsert_pair.second应该是true的,否则为false

pair<map<int, string>::iterator, bool> insert_pair;

insert_pair = person.insert(pair<int, string>(1, "詹姆斯")); // 插入pair数据
if(insert_pair.second = true)                               // 判断是否插入数据成功
    cout << "Insert success" << endl;
else
    cout << "Insert fail" << endl;

三、容量查询

person.empty()      // map是否为空
person.size()       // map大小

四、数据查找
  查找是否包含某个关键字使用用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator

map<int, string>::iterator i;

i = person.find(1);                                         // map数据查找
if(i != person.end())
    cout << "Find, value " << i->second << endl;
else
    cout << "Don't find" << endl;

五、数据删除
  删除某个关键字使用的是erase()方法,传入的参数是要删除的key

person.erase(iterator n);               // 删除关键字为n的条目
person.erase(iterator m, iterator n);   // 删除关键字为m~n范围内的条目
person.clear();                         // 清除整个map

六、容器交换
  map中的swap不是一个容器中的元素交换,而是两个容器所有元素的交换

void swap( map& other );

示例代码:

#include <iostream>
#include <map>

using namespace std;

int main(void)
{
    map<int, string> person;
    pair<map<int, string>::iterator, bool> insert_pair; 

    insert_pair = person.insert(pair<int, string>(1, "詹姆斯"));  // 插入pair数据
    person.insert(map<int, string>::value_type(2, "杜兰特"));     // 插入value_type数据
    person[3] = "未名湖畔的落叶";                                 // 数组模式插入
    person.insert(pair<int, string>(1, "乔治"));

    if(insert_pair.second = true)                                 // 判断是否插入数据成功
        cout << "Insert success" << endl;
    else
        cout << "Insert fail" << endl;

    cout << endl;

    int size = person.size();                                     // map大小
    cout << "person.size: " << size << endl;

    cout << endl;  

    map<int, string>::iterator itor;
    for(itor=person.begin(); itor!=person.end(); ++itor)          // map前向迭代器遍历
        cout << itor->first << " " << itor->second << endl;

    cout << endl;  

    for(int itor=1; itor!=size+1; ++itor )                        // map数组遍历
        cout << itor << " " << person[itor] << endl;

    cout << endl;  

    itor = person.find(1);                                        // map数据查找
    if(itor != person.end())
        cout << "Find success, " << itor->first << " " << itor->second << endl;
    else
        cout << "Cann't find" << endl;

    cout << endl;  

    person.erase(1);                                              // 数据删除,删除成功返回1,否则返回0
    //person.erase(itor);                                           // 与上一条语句作用相同
    for(itor=person.begin(); itor!=person.end(); ++itor)
        cout << itor->first << " " << itor->second << endl;

    person.erase(person.begin(), person.end());                   // 删除所有数据
    cout << "map cout:" << endl;
    for(itor=person.begin(); itor!=person.end(); ++itor)
        cout << itor->first << " " << itor->second << endl;

    return 0;
}
[john@bogon C++]$ ./a.out 
Insert success

person.size: 3

1 詹姆斯
2 杜兰特
3 未名湖畔的落叶

1 詹姆斯
2 杜兰特
3 未名湖畔的落叶

Find success, 1 詹姆斯

2 杜兰特
3 未名湖畔的落叶
map cout:

七、排序
  1、按key排序
    为了实现快速查找,map内部本身就是按序存储的(比如红黑树)。在我们插入key、value时,就会按照key的大小顺序进行存储,例:

#include <iostream>
#include <map>

using namespace std;

int main(void)
{
    map<string, int> person;

    map<string, int>::iterator itor;

    person["詹姆斯"]     = 99;
    person["安德托昆博"]  = 90;
    person["莱昂纳德"]    = 97;
    person["乔治"]       = 92;

    for(itor=person.begin(); itor!=person.end(); ++itor)
        cout << itor->first << " " << itor->second << endl;

    return 0;
}
[john@bogon C++]$ ./a.out 
乔治 92
安德托昆博 90
莱昂纳德 97
詹姆斯 99

  由例程代码可以看出,map本身就是默认按key从小到大排序的,如果想从大到小排序,那么需要在声明的时候加入第三个参数:

#include <iostream>
#include <map>

using namespace std;

int main(void)
{
    map<string, int, greater<string> > person;

    map<string, int>::iterator itor;

    person["詹姆斯"]     = 99;
    person["安德托昆博"]  = 90;                                 
    person["莱昂纳德"]    = 97;
    person["乔治"]       = 92;

    for(itor=person.begin(); itor!=person.end(); ++itor)
        cout << itor->first << " " << itor->second << endl;

    return 0;
}
[john@bogon C++]$ ./a.out 
詹姆斯 99
莱昂纳德 97
安德托昆博 90
乔治 92

  由上面可以看出,声明时第三个参数可以控制排序方式,因此我们可以自己定义一个类,让map按我们自己想要的方式排序,下面就来编写一个按照key长度排序的例子:

#include <iostream>
#include <map>

using namespace std;

struct CmpByKeyLength 
{  
    bool operator() (const string& k1, const string& k2) 
    {  
        return k1.length() < k2.length();  
    }  
};  

int main(void)
{
    map<string, int, CmpByKeyLength> person;

    map<string, int>::iterator itor;

    person["詹姆斯"]     = 99;
    person["安德托昆博"] = 90;
    person["莱昂纳德"]   = 97;
    person["乔治"]       = 92;

    for(itor=person.begin(); itor!=person.end(); ++itor)
        cout << itor->first << " " << itor->second << endl;

    return 0;
}
[john@bogon C++]$ ./a.out 
乔治 92
詹姆斯 99
莱昂纳德 97
安德托昆博 90

  2、按value排序
    在上面,我们借助map提供的参数接口,为它指定相应Compare类,就可以实现对map按Key排序,是在创建map并不断的向其中添加元素的过程中就会完成排序,利用value排序,一般人首先想到sort算法,但是sort有个限制,利用sort算法只能对序列容器进行排序,就是线性的(如vector,list,deque)。map也是一个集合容器,它里面存储的元素是pair,但是它不是线性存储的,所以不能直接利用sort和map结合进行排序,但是我们可以将map中的key和value分别存放在一个pair类型的vector中,然后利用vector的sort函数排序,例:

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<string, int> PAIR;

struct CmpByValue 
{  
    bool operator()(const PAIR& x, const PAIR& y) 
    {  
        return x.second < y.second;  
    }  
};

int main(void)
{
    map<string, int> person;

    person["詹姆斯"]     = 99;
    person["安德托昆博"] = 90;
    person["莱昂纳德"]   = 97;
    person["乔治"]       = 92;

    vector<PAIR> Person(person.begin(), person.end());  
    sort(Person.begin(), Person.end(), CmpByValue());

    for(vector<PAIR>::iterator i=Person.begin(); i!=Person.end(); ++i)
        cout << i->first << " " << i->second << endl;

    return 0;
}
[john@bogon C++]$ ./a.out 
安德托昆博 90
乔治 92
莱昂纳德 97
詹姆斯 99

猜你喜欢

转载自blog.csdn.net/keyue123/article/details/79158594