C++ STL Maps

Maps定义 --》 个人理解为python的字典

C++ Maps are sorted associative containers the contian unique key/value pairs. For example, you could create a Map that associates a string with an integer, and

then use that map to associate the number of days in each month with the name of each month.

翻译: C++ Maps是排序的关联容器,其中包含唯一的键/值对。例如,您可以创建一个将字符串与整数相关联的映射,然后使用该地图将每个月的天数与每个月的名称关联起来。

Maps Constuctors & Destructors

Syntax


#include <map>
map();
map( const map &n);
map( iterator start, iterator end);
map(iterator start, iterator end, const key_compare& cmp);
map(const key_compare& com);
^map();

未序...

std::map::at  --> c++11

Syntax


mapped_type& at(const key_type& k);
const mapped_type& at(const key_type& k) const;

Access element

Returns a reference to the mapped value of the element identified with key k.

返回一个区分键k的映射值的引用

If k does not match the key of any element in the container, the function throws an out_of_range exception.

如果k没有匹配容器中任何元素的键, 函数抛出一个out_of_range 的异常

Parametes


A reference to the mapped value of the element with a key value equivalent to k.

一个元素的映射值的引用, 它的键值相当于k

If the map object is const_qualified , the function returns a reference to const mapped_tpe. OtherWise, it returns a reference to mapped_type

怎么翻译都不通, 哈哈哈

Member type mapped_type is the type to the mapped values in the container (see map member types). In map this is an alias of its second template parameter (T).

会员类型mappedtype是容器中映射值的类型(参见map成员类型)。在地图上,这是第二个模板参数(T)的别名。

Example


#include <iostream>
#include <string>
#include <map>

using namespace std;

int _at(){

    std::map<string, int> mymap = {
            {"alpha", 0},
            {"beta", 0},
            {"gamma", 0}
    };
    mymap.at("alpha") = 10;
    mymap.at("beta") = 20;
    mymap.at("gamma") = 30;

    for (auto& x:mymap){
        cout << x.first << ": " << x.second << endl;
    }
    return 0;
}

Output:

alpha: 10
beta: 20
gamma: 30

std::map::begin

Syntax


iterator begin();
const_iterator begin() const;

Return iterator to begining

Example


int map_begin(){
    map<char, int> mymap;
    mymap['b'] = 100;
    mymap['c'] = 200;
    mymap['a'] = 300;

    for(map<char, int>::iterator it = mymap.begin(); it != mymap.end(); it++) cout << it->first << ": " << it->second << endl;


    return 0;
}

Output

a: 300
b: 100
c: 200

猜你喜欢

转载自www.cnblogs.com/renfanzi/p/9543693.html
今日推荐