http://www.cplusplus.com

搜了才发现map的成员函数这么多orz,跟着cplusplus按字典序走一遍叭(顺序有微调orz

<1>  map::at (c++11)

// map::at
//Returns a reference to the mapped value of the element identified with key k.
//If k does not match the key of any element in the container, the function throws an out_of_range exception.
#include <iostream>
#include <string>
#include <map> int main ()
{
std::map<std::string,int> mymap = {
{ "alpha", },
{ "beta", },
{ "gamma", } }; mymap.at("alpha") = ;
mymap.at("beta") = ;
mymap.at("gamma") = ; for (auto& x: mymap) {
std::cout << x.first << ": " << x.second << '\n';
} return ;
}
/*output:
alpha: 10
beta: 20
gamma: 30*/

<2> map::begin/end

// map::begin/end
//Returns an iterator referring to the first element in the map container.
//end同理
#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 ;
}
/*output
a => 200
b => 100
c => 300*/

<3>map::cbegin(c++11)

// map::cbegin/cend
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap; mymap['b'] = ;
mymap['a'] = ;
mymap['c'] = ; // print content:
std::cout << "mymap contains:";
for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
std::cout << " [" << (*it).first << ':' << (*it).second << ']';
std::cout << '\n'; return ;
}
/*output
mymap contains: [a:200] [b:100] [c:300]
*/

<4> map::clear

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

//很简单所以就不转例子了orz

<5>map::count

Searches the container for elements with a key equivalent to k and returns the number of matches.
Because all elements in a map container are unique, the function can only return 1 (if the element is found) or zero (otherwise).
Two keys are considered equivalent if the container's comparison object returns false reflexively (i.e., no matter the order in which the keys are passed as arguments).

// map::count
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
char c; mymap ['a']=;
mymap ['c']=;
mymap ['f']=; for (c='a'; c<'h'; c++)
{
std::cout << c;
if (mymap.count(c)>)
std::cout << " is an element of mymap.\n";
else
std::cout << " is not an element of mymap.\n";
} return ;
}

<5>map::crbegin

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

<6>map::crend

Returns a const_reverse_iterator pointing to the theoretical element preceding the first element in the container (which is considered its reverse end).

// map::crbegin/crend
#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:300] [b:100] [a:200]

<7>map::emplace(c++11)

// map::emplace
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap; mymap.emplace('x',);
mymap.emplace('y',);
mymap.emplace('z',); std::cout << "mymap contains:";
for (auto& x: mymap)
std::cout << " [" << x.first << ':' << x.second << ']';
std::cout << '\n'; return ;
}
//output :mymap contains: [x:100] [y:200] [z:100]

<8>map::emplace_hint

带有提示的位置插入?

// map::emplace_hint
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap;
auto it = mymap.end(); it = mymap.emplace_hint(it,'b',);
mymap.emplace_hint(it,'a',);
mymap.emplace_hint(mymap.end(),'c',); std::cout << "mymap contains:";
for (auto& x: mymap)
std::cout << " [" << x.first << ':' << x.second << ']';
std::cout << '\n'; return ;
}
//output:mymap contains: [a:12] [b:10] [c:14]

<9>map::empty

Returns whether the map container is empty (i.e. whether its size is 0).

<10>map::equal_range

<11>map::erase

可以通过iterator,key,range实现删除

// erasing from map
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator it; // insert some values:
mymap['a']=;
mymap['b']=;
mymap['c']=;
mymap['d']=;
mymap['e']=;
mymap['f']=; it=mymap.find('b');
mymap.erase (it); // erasing by iterator mymap.erase ('c'); // erasing by key it=mymap.find ('e');
mymap.erase ( it, mymap.end() ); // erasing by range // show content:
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n'; return ;
}
/*output:
a => 10
d => 40
*/

<12>map::find

//通过key查询,如果找到了返回对应值,没有找到返回末尾位置

// map::find
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator it; mymap['a']=;
mymap['b']=;
mymap['c']=;
mymap['d']=; it = mymap.find('b');
if (it != mymap.end())
mymap.erase (it); // print content:
std::cout << "elements in mymap:" << '\n';
std::cout << "a => " << mymap.find('a')->second << '\n';
std::cout << "c => " << mymap.find('c')->second << '\n';
std::cout << "d => " << mymap.find('d')->second << '\n'; return ;
}
/*output:
elements in mymap:
a => 50
c => 150
d => 200
*/

<13>map::get_allocator

//(这个函数好像用不多?先不写了

<14>map::insert

// map::insert (C++98)
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap; // first insert function version (single parameter):
mymap.insert ( std::pair<char,int>('a',) );
mymap.insert ( std::pair<char,int>('z',) ); std::pair<std::map<char,int>::iterator,bool> ret;
ret = mymap.insert ( std::pair<char,int>('z',) );
if (ret.second==false) {
std::cout << "element 'z' already existed";
std::cout << " with a value of " << ret.first->second << '\n';
} // second insert function version (with hint position):
std::map<char,int>::iterator it = mymap.begin();
mymap.insert (it, std::pair<char,int>('b',)); // max efficiency inserting
mymap.insert (it, std::pair<char,int>('c',)); // no max efficiency inserting // third insert function version (range insertion):
std::map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c')); // showing contents:
std::cout << "mymap contains:\n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n'; std::cout << "anothermap contains:\n";
for (it=anothermap.begin(); it!=anothermap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n'; return ;
}
/*output:
element 'z' already existed with a value of 200
mymap contains:
a => 100
b => 300
c => 400
z => 200
anothermap contains:
a => 100
b => 300
*/

<15>map::lower_bound/upper_bound

// map::lower_bound/upper_bound
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator itlow,itup; mymap['a']=;
mymap['b']=;
mymap['c']=;
mymap['d']=;
mymap['e']=; itlow=mymap.lower_bound ('b'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!) mymap.erase(itlow,itup); // erases [itlow,itup) // print content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n'; return ;
}
/*output:
a => 20
e => 100
*/

累辽,下次有空又不想写题再补坑吧,感觉还是写题把方法用上去比较快乐

不过平常用的稍微多一点的也差不多了orz,佛系更新

map member functions的更多相关文章

  1. C++ 之const Member Functions

    Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter lis ...

  2. C++ Member Functions的各种调用方式

    [1]Nonstatic Member Functions(非静态成员函数) C++的设计准则之一就是:nonstatic member function至少必须和一般的nonmember funct ...

  3. Some interesting facts about static member functions in C++

    Ref http://www.geeksforgeeks.org/some-interesting-facts-about-static-member-functions-in-c/ 1) stati ...

  4. virtual member functions(单一继承情况)

    virtual member functions的实现(就单一继承而言): 1.实现:首先会给有多态的class object身上增加两个members:一个字符串或数字便是class的类型,一个是指 ...

  5. C++:Special Member Functions

    Special Member Functions 区别于定义类的行为的普通成员函数,类内有一类特殊的成员函数,它们负责类的构造.拷贝.移动.销毁. 构造函数 构造函数控制对象的初始化过程,具体来说,就 ...

  6. 2_成员函数(Member Functions)

    成员函数以定从属于类,不能独立存在,这是它与普通函数的重要区别.所以我们在类定义体外定义成员函数的时候,必须在函数名之前冠以类名,如Date::isLeapYear().但如果在类定义体内定义成员函数 ...

  7. [EffectiveC++]item23:Prefer non-member non-friend functions to member functions

    99页 导致较大封装性的是non-member non-friend函数,因为它并不增加“能否访问class内之private成分”的函数数量.

  8. C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)

    4.4 指向Member Function的指针 (Pointer-to-Member Functions) 取一个nonstatic data member的地址,得到的结果是该member在 cl ...

  9. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

随机推荐

  1. CF #355div2 D 宝藏与钥匙 dp 二维数组智商题

    D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...

  2. CodeForces 557C Arthur and Table STL的使用

    题意:一个桌子有n条腿,每条腿有一定的长度l,和砍下的花费w,现在规定,桌子稳的条件是长度最长的腿(可多个)的数量大于长度小于它的桌子腿数量,且不存在比他还长的桌子腿,求让桌子腿稳定的最小的花费 #i ...

  3. 降维算法----PCA原理推导

    1.从几何的角度去理解PCA降维 以平面坐标系为例,点的坐标是怎么来的? 图1                                                             ...

  4. tps吞吐量映射的问题

    tps随着时间增加,吞吐量增加,但到达一定时间,吞吐不变,出现瓶颈,可能是以下原因 1/反应宽带问题 2/连接数释放问题 3/cpu占有率超出问题 4/内存不够问题 5/数据库连接屏蔽 用jmeter ...

  5. 2019年6月Github最新开源java项目

    目录 1.halo,这是一个轻快,简洁,功能强大,使用Java开发的博客系统. 2.jeecg-boot 3.CS-Notes 4.JavaGuide 5.advanced-java 6.mall-l ...

  6. Eclipse中文插件安装教程

    先运行英文版Eclipse 点击Help选择Install New Software 直接点击Add按钮 Name输入: Babel Location输入: http://download.eclip ...

  7. web安全之如何防止CSRF跨站请求伪造(转载)

    https://www.cnblogs.com/blibli/p/7658168.html CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click ...

  8. 问题:unknown error: call function result missing 'value' 解决方法

    问题:unknown error: call function result missing 'value' 页面也没有 填充信息 原因是:安装与chrome和对应的chromedriver版本问题 ...

  9. Pycharm 常用快捷键-Windows版

    常用快捷键: Ctrl + / 行注释(可选中多行) Ctrl + Alt + L 代码格式化 Tab / Shift + Tab 缩进.不缩进当前行(可选中多行) Ctrl + D 复制选定的区域 ...

  10. Prism学习笔记-模块之间通信的几种方式

    在开发大型复杂系统时,我们通常会按功能将系统分成很多模块,这样模块就可以独立的并行开发.测试.部署.修改.使用Prism框架设计表现层时,我们也会遵循这个原则,按功能相关性将界面划分为多个模块,每个模 ...