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", },
{"beta", },
{"gamma", }
};
mymap.at("alpha") = ;
mymap.at("beta") = ;
mymap.at("gamma") = ; for (auto& x:mymap){
cout << x.first << ": " << x.second << endl;
}
return ;
}

Output:

alpha:
beta:
gamma:

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'] = ;
mymap['c'] = ;
mymap['a'] = ; for(map<char, int>::iterator it = mymap.begin(); it != mymap.end(); it++) cout << it->first << ": " << it->second << endl; return ;
}

Output

a:
b:
c:

std::map::end

Syntax


    iterator end();
const_iterator end() const;

Example


#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap; mymap['b'] = ;
mymap['a'] = ;
mymap['c'] = ; // show content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n'; return ;
}

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'] = ;
mymap['a'] = ;
mymap['c'] = ; cout << "mymap contains" << endl;
for (auto it = mymap.cbegin(); it != mymap.cend(); it++) cout << (*it).first << " : " << it->second << endl; return ;
}

Output:

mymap contains
a :
b :
c :

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'] = ;
mymap['a'] = ;
mymap['c'] = ; std::cout << "mymap backwards:";
for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit)
std::cout << " [" << rit->first << ':' << rit->second << ']';
std::cout << '\n'; return ;
}

Output:

mymap backwards: [c:] [b:] [a:]

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'] = ;
mymap['y'] = ;
mymap['z'] = ;
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'] = ;
cout << "mymap contains: " << endl;
for (map<char, int>::iterator it=mymap.begin(); it!=mymap.end(); it++) cout << it->first << it->second << endl; return ;
}

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'] = ;
mymap['c'] = ;
mymap['d'] = ;
mymap['fg'] = ; for (c='a'; c<'h'; c++){
cout << mymap.count(c) << ' ';
cout << c;
if (mymap.count(c) > )
cout << " is an element of mymap.\n";
else
cout << " is not an element of mymap.\n";
}
return ;
}

Output

 a is an element of mymap.
b is not an element of mymap.
c is an element of mymap.
d is an element of mymap.
e is not an element of mymap.
f is not an element of mymap.
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', );
mymap.emplace('y', );
mymap.emplace('z', ); cout << "mymap contains: ";
for (auto& x: mymap)
cout << " [ " << x.first << ':' << x.second << " ] "; cout << endl; return ;
}

Output

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

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(, );
list<int> second(, ); 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 ;
}
a=>
c=>
d=>

std::map::size

Example


int map_size(){
map<char, int> mymap; mymap['a'] = ;
mymap['c'] = ;
mymap['d'] = ; cout << mymap.size() << endl;
cout << mymap.max_size() << endl; return ;
}

output


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'] = ;
mymap['b'] = ;
mymap['c'] = ;
mymap['d'] = ;
mymap['e'] = ;
mymap['f'] = ; 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 ;
}
a=>
c=>
e=>
g=>

C++ STL Maps的更多相关文章

  1. ACE_INET_Addr类 API

    ACE_INET_Addr类,在这个ACE_网络框架中,应该是比较重要的辅助类,该类主要封装了C SOCKET 的地址对象,通过外观封装的模式,把struct sockaddr_in封装在内.方便用户 ...

  2. C++的STL

    今天,看一段代码的时候发现只一句话就做了个排序,是这样的: sort(rotateArray.begin(),rotateArray.end()); 很震惊,后来查了一下sort的用法, sort函数 ...

  3. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  4. Linux环境下stl库使用(map)

    例子1: testMap.cpp #include <string.h> #include <iostream> #include <map> #include & ...

  5. STL迭代器笔记

    STL迭代器简介 标准模板库(The Standard Template Library, STL)定义了五种迭代器.下面的图表画出了这几种: input         output \       ...

  6. [STL][C++]MAP

    参考链接:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html map头文件 #include <map> map添加数据: map&l ...

  7. STL map详细用法和make_pair函数

    今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...

  8. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  9. c++ (P262—P277) STL

    STL的六大组件 容器(Container),是一种数据结构,如list,vector,和deques ,以模板类的方法提供.为了访问容器中的数据,可以使用由容器类输出的迭代器: 迭代器(Iterat ...

随机推荐

  1. 【Unity】3.1 利用内置的3D对象创建三维模型

    分类:Unity.C#.VS2015 创建日期:2016-04-02 一.基本概念 Unity已经内置了一些基本的3D对象,利用这些内置的3D对象就可以直接构建出各种3D模型(当然,复杂的三维模型还需 ...

  2. 数据库存储引擎 show engines 修改引擎

    mysql> show engines; +--------------------+---------+-------------------------------------------- ...

  3. distill 来自google,openai,deepmind,YC research

    https://distill.pub/ https://colah.github.io/

  4. vue.js中请求数据v-for循环使用数据

    1.效果图 2.cart.json { "message":"", "status":"1", "result ...

  5. 转css中文英文换行、禁止换行、显示省略号

    css中文英文换行.禁止换行.显示省略号 原创 2016年08月09日 14:20:01   word-break:break-all;只对英文起作用,以字母作为换行依据 word-wrap:brea ...

  6. Eclipse - Mac Os Default JRE missing

    转:http://stackoverflow.com/questions/1736993/eclipse-mac-os-default-jre-missing 1) Follow Joshua's a ...

  7. Pythonic版二分查找

    [本文出自天外归云的博客园] 前提:升序数组,待查元素在数组中. 二分查找:就是一个递归函数c.待查元素a,当前数组中位数b,如果b=a则返回b的索引,b>a则在b左侧的子数组中调用函数c,否则 ...

  8. RTX——第19章 SVC 中断方式调用用户函数(后期补历程)

    以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解如何采用 SVC 中断方式调用用户函数. 当用户将 RTX 任务设置为工作在非特权级模式 ...

  9. Faiss教程:基础

    Faiss对一些基础算法提供了非常高效的实现:k-means.PCA.PQ编解码. 聚类 假设2维tensor x: ncentroids = 1024 niter = 20 verbose = Tr ...

  10. c语言二分法

    #include <stdio.h> #include <stdlib.h> int Search(int *a,int key) { ,mid; ; while(low< ...