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.6 导入图片资源

    分类:Unity.C#.VS2015 创建日期:2016-04-05 一.简介 Unity支持的图像文件格式非常多,包括TIF.PSD.TCA.JPC.PNG.GlF.BMP.IFF.PICT.DDS ...

  2. C++ Primer笔记 容器和算法(2)

    erase 删除后  返回的是删除元素的后一个迭代器位置 int main() { //怎样正确的删除全部元素 循环 int a[]={1,2,3,4,5,6,7,8,9}; vector<in ...

  3. WebClient请求帮助类

    /// <summary> /// 通过JSON方式发送POST请求 /// 将返回结果按JSON方式解析 /// </summary> public static class ...

  4. HTML5学习笔记(二十三):DOM应用之动态加载脚本

    同步加载和执行JS的情况 在HTML页面的</body>表情之前添加的所有<script>标签,无论是直接嵌入JS代码还是引入外部js代码都是同步执行的,这里的同步执行指的是在 ...

  5. angular的uiRouter服务学习(4)

    本篇接着上一篇angular的uiRouter服务学习(3)继续讲解uiRouter的用法 本篇主要讲解uiRouter的url路由 大多数情况下,状态是和url相关联的: 当url改变,激活对应的状 ...

  6. angular中的表单数据自定义验证

    之前说过了angular是如何给表单的数据进行基本的,常用的验证的:angular学习笔记(二十)-表单验证 但是在实际工作中,这些验证是远远不够的,很多时候我们需要自定义一些验证规则,以及一些异步, ...

  7. (原创)如何使用boost.asio写一个简单的通信程序(一)

    boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ...

  8. (原创)c++11改进我们的模式之改进访问者模式

    本次讲c++11改进我们的模式之改进访问者模式 访问者模式是GOF23个设计模式中比较复杂的模式之一,但是它的功能也很强大,非常适合稳定的继承层次中对象的访问,可以在不修改被访问对象的情况下,动态添加 ...

  9. 每日英语:The best time for coffee is 10:30 a.m.

    For many of us, it is the rocket fuel that gets us going first thing in the morning. But one expert ...

  10. 【Delphi】SPComm注意事项

    Spcomm属性设置 SPCOMM 控件的属性设置很关键的,特别是使用事件驱动时接收大数据块时尤为明显,如果设置不当,接收到的数据可能严重出错. ReadIntervalTimeout:=100 SP ...