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

 Return an iterator referring to the first element in the map container.

返回一个map容器第一个元素的迭代器引用

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

 

std::map::end

Syntax


    iterator end();
const_iterator end() const;

 

Example


 

#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  return 0;
}

 

 

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

Syntax


 

const_iterator cbegin() const noexcept;

 

Retrun const_iterator to beginning

 Return a const_iterator pointing to the first element in the container.

返回一个指向容器的第一个元素的const 迭代器

Parameters


 

none

 

Example


 

int map_cbegin(){

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

    cout << "mymap contains" << endl;
    for (auto it = mymap.cbegin(); it != mymap.cend(); it++) cout << (*it).first << " : " << it->second << endl;


    return 0;
}

 

Output:

mymap contains
a : 200
b : 100
c : 300

 

std::map::cend

同上

 

std::map::rbegin

Syntax


    reverse_iterator rbegin();
const_reverse_iterator rbegin() cosnt;

 

Return reverse iterator to reverse beginning.

Return a reverse iterator pointing to the last element in the container.

 返回一个指向容器中最后一个元素的取反迭代器

Parameters


 

none

 

Example


 


int map_rbegin(){
map<char, int> mymap;

mymap['a'] = 100;
mymap['b'] = 100;
mymap['c'] = 100;

map<char, int>::reverse_iterator it; // 注意这里是 reverse_iterator 不是 iterator

for (it=mymap.rbegin();it != mymap.rend();it++) cout << it->first << endl;

return 0;
}
 

Output:

c
b
a

 

std::map::rend

同上

 

std::map::crbegin

Syntax


 

const_reverse_iterator crbegin() cosnt except;

 

Return const_reverse_iterator to reverse beginning

Returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning).

返回一个容器中最后一个元素的const_reserver iterator。

 

Example


 

#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  std::cout << "mymap backwards:";
  for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit)
    std::cout << " [" << rit->first << ':' << rit->second << ']';
  std::cout << '\n';

  return 0;
}

 


Output:

mymap backwards: [c:300] [b:100] [a:200]

 

std::map::crend

同上...

 

std::map::clear

Syntax


 

void clear();

Clear content

Removes all elements from the map container(which are destoryed), leaving the container with a size of 0;

从map容器中移除所有元素(这是破坏), 将容易的大小设置为0;

 

Example


 

int map_clear(){
    map<char, int> mymap;
    mymap['x'] = 100;
    mymap['y'] = 200;
    mymap['z'] = 300;
    cout << "mymap contains: " << endl;
    for (map<char, int>::iterator it=mymap.begin(); it!=mymap.end(); it++) cout << it->first << it->second << endl;

    mymap.clear();
    mymap['a'] = 2;
    cout << "mymap contains: " << endl;
    for (map<char, int>::iterator it=mymap.begin(); it!=mymap.end(); it++) cout << it->first << it->second << endl;

    return 0;
}

 

OutPut:

mymap contains: 
x100
y200
z300
mymap contains: 
a2

 

 

std::map::count

Syntax


 

size_type count(const key_type& k) const;

 

Count elements with a specific key

用一个特定的键计数元素

Searches the container for elements with a key equivalent to k and returns the number of matches.

在容器中搜索具有与k相同的键的元素,并返回比较的数量。

 

Parameters


 

k

  Key to search for.

 

Return value


 

if the container contains an element whose key is equivalent to k, or zero otherwise.

如果容器中包含一个元素,其键值等于k,否则就等于零。

Member type size_type is an unsigned integral type

成员类型size_type 是一个unsigned int 类型

 

Example


 

 

int map_count(){
    map<char, int> mymap;

    char c;

    mymap['a'] = 101;
    mymap['c'] = 202;
    mymap['d'] = 202;
    mymap['fg'] = 303;

    for (c='a'; c<'h'; c++){
        cout << mymap.count(c) << ' ';
        cout << c;
        if (mymap.count(c) > 0)
            cout << " is an element of mymap.\n";
        else
            cout << " is not an element of mymap.\n";
    }
    return 0;
}

Output

1 a is an element of mymap.
0 b is not an element of mymap.
1 c is an element of mymap.
1 d is an element of mymap.
0 e is not an element of mymap.
0 f is not an element of mymap.
1 g is an element of mymap.

 

 

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

Syntax


 

template <class... Args>
  pair<iterator,bool> emplace (Args&&... args);

Construct and insert element

构造和插入元素
Inserts a new element in the map if its key is unique. This new element is constructed in place using args as the arguments for the construction of a value_type (which is an object of a pair type).
翻译: 如果它的键是唯一的插入一个新的元素。 这个新元素是用args作为构建valuetype(这是一对类型的对象)的参数而构建的。
The insertion only takes place if no other element in the container has a key equivalent to the one being emplaced (keys in a map container are unique).
只有在容器中没有其他元素的键值与被放置的键值相等时才进行插入(map容器中的键是惟一的)。
If inserted, this effectively increases the container size by one.
如果插入,这将有效地增加容器的大小。
Internally, map containers keep all their elements sorted by their key following the criterion specified by its comparison object. The element is always inserted in its respective position following this ordering.
在内部,map容器按照其比较对象指定的标准,将所有元素按其键排序。在这个顺序之后,元素总是被插入到它各自的位置。
The element is constructed in-place by calling allocator_traits::construct with args forwarded.
元素是通过调用分配特性来构建的::用args转发。
A similar member function exists, insert, which either copies or moves existing objects into the container.

一个相似的成员函数存在,插入,它可以复制或移动现有对象到容器中。

 

Parameters


 

args

  Arguments used to construct a new object of the mapped type for the inserted element.

 

Example


 

int map_emplace(){

    map<char, int> mymap;

    mymap.emplace('x', 100);
    mymap.emplace('y', 200);
    mymap.emplace('z', 100);

    cout << "mymap contains: ";
    for (auto& x: mymap)
        cout << " [ " << x.first << ':' << x.second << " ] ";

    cout << endl;

    return 0;
}

 

Output

mymap contains:  [ x:100 ]  [ y:200 ]  [ z:100 ] 

 

std::map::erase

 Syntax:


#include<map>
void erase(iterator pos);
void erase (iterator start, iterator end);
size_type erase(const key_type& key);

 

 

The erase function() either erases the element at pos, erases the elements between start and end, or erases all elements that have the value of key.

erase函数函数()要么在pos中擦除元素,要么在开始和结束之间擦除元素,要么擦除所有具有键值的元素。

For example

看例子

int map_erase(){
    map<char, int> mymap;
    map<char, int>::iterator it;

    mymap['a'] = 10;
    mymap['b'] = 20;
    mymap['c'] = 30;
    mymap['d'] = 40;
    mymap['e'] = 50;
    mymap['f'] = 60;
    mymap['g'] = 70;

    it = mymap.find('b');
    mymap.erase(it);

    mymap.erase('c');

    it = mymap.find('e');
    mymap.erase(it, mymap.end());

    for (it=mymap.begin(); it!=mymap.end();it++) cout << it->first << " => " << it->second << endl;
    return 0;
}

 

a => 10
d => 40

 

std::map::empty

Syntax


 

#include <map>
bool empty() const;

 

The empty() function returns true if the map has no elements, false otherwise .

 如果map没有元素,则empty()函数返回true;反之则为false。

 

Example


 

int _swap(){
    list<int> first(3, 100);
    list<int> second(5, 200);

    first.swap(second);

    for (list<int>::iterator it = first.begin(); it != first.end(); it++) cout << ' ' << *it; cout << endl;
    for (list<int>::iterator it = first.begin(); it != first.end(); it++) cout << ' ' << *it; cout << endl;


    return 0;
}

 

3
a=>10
c=>20
d=>30
0

 

 

std::map::size

Example


 

int map_size(){
    map<char, int> mymap;

    mymap['a'] = 10;
    mymap['c'] = 20;
    mymap['d'] = 30;

    cout << mymap.size() << endl;
    cout << mymap.max_size() << endl;

    return 0;
}

 

output

3
461168601842738790

 

std::map::max_size

同上

 

std::map::find

Syntax


 

      iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

 

The find() function returns an iterator to key, or an iterator to the end of the map if key is not found.

find()函数将迭代器返回给键,或者如果没有找到键,则返回到map末端的迭代器。

find() runs in logarithmic time

find()以对数时间运行

Example


 

int map_find(){

    map<char, int> mymap;
    map<char, int>::iterator it;

    mymap['a'] = 50;
    mymap['b'] = 100;
    mymap['c'] = 150;
    mymap['d'] = 200;
    mymap['e'] = 250;
    mymap['f'] = 300;

    it = mymap.find('b');
    if(it != mymap.end())
        mymap.erase(it);

    cout << "a=> " << mymap.find('a')->second << endl;
    cout << "c=> " << mymap.find('c')->second << endl;
    cout << "e=> " << mymap.find('d')->second << endl;
    cout << "g=> " << mymap.find('g')->second << endl; // 不存在的key,value为0

    return 0;
}

 

a=> 50
c=> 150
e=> 200
g=> 0

 

posted @ 2018-08-29 12:11  我当道士那儿些年  阅读(301)  评论(0编辑  收藏  举报