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. TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

    Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5004   Accepted: 1444 ...

  2. HGOI 20190711 题解

    Problem A 矩阵第K小数 给定一个$n \times m$的矩阵,位置$A_{i,j}  = i\times j$, 给出$Q$个询问,每一次查询矩阵中第$Q_i$小的数是多少. 对于100% ...

  3. Presto部署指南

    1.Presto简介说明 Presto是一个开源的分布式SQL查询引擎,适用于交互式分析查询,数据量支持GB到PB字节. Presto的设计和编写完全是为了解决像Facebook这样规模的商业数据仓库 ...

  4. 在Windows QT下使用ZeroMQ

    zeroMQ作为一个嵌入式消息队列系统,以其轻便灵活的使用方式,极为适合应用程序分布式通讯处理, 或者是一种有效的代替常规saocket通讯的方法. 1)下载地址:http://zeromq.org/ ...

  5. linux查看端口占用情况,python探测端口使用的小程序

    Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 # lsof -i:8000 COMMAND PID USER ...

  6. Oracle三种分页?

    ①select * from (select employee.*, rownum r from employee) where r between 2 and 5; ②select * from ( ...

  7. OkHttp3 使用详解

    一,简介 OkHttp 是一个高效的 HTTP 客户端,具有非常多的优势: 能够高效的执行 http,数据加载速度更快,更省流量 支持 GZIP 压缩,提升速度,节省流量 缓存响应数据,避免了重复的网 ...

  8. [翻译]剖析C#中的异步方法

    翻译自一篇博文,原文:Dissecting the async methods in C# 有些括号里的是译注或我自己的理解. 异步系列 剖析C#中的异步方法 扩展C#中的异步方法 C#中异步方法的性 ...

  9. 微信、QQ第三方登录授权时的问题总结

    一.微信第一个问题:redirect_uri域名与后台配置不一致,错误码:10003 解决方案: 1,首先确定访问的第三方接口地址参数前后顺序是否正确,redirect_uri回调地址是否加了http ...

  10. vue实现百度下拉框

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...