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. Beyong Compare 2.4.10刷新剩余日期bat批处理文件

    @echo off % start )(window.close)&&exit REG DELETE "HKCU\Software\Scooter Software\Beyo ...

  2. tarjan算法 习题

    dfs树与tarjan算法 标签(空格分隔): 517coding problem solution dfs树 tarjan Task 1 给出一幅无向图\(G\),在其中给出一个dfs树\(T\), ...

  3. Jmeter(四)Cookie管理器

    上一节中我们用Jmeter通过接口上传了一张图片到人人网,其中请求头中的Cookie是写死的,这个Cookie其实是登录成功后服务器返回给客户端的,客户端接收到这个Cookie后保存下来,在后续向服务 ...

  4. [负数在内存中的存储] 0x80000000 = -2147483648

    https://blog.csdn.net/youyou362/article/details/72667951/ 1. 十进制负数以其补码存储在内存上 例子:-8 在内存中表示为:1111 1111 ...

  5. JavaWeb_使用dom4j解析、生成XML文件

    dom4j 官网 xml解析DOM文档对象模型(树形结构) DOM方式解析:把xml文档加载到内存形成树形结构,可以进行增删改的操作 Learn   使用dom4j解析文件"NewFile. ...

  6. JS框架_(JQuery.js)图片相册掀开切换效果

    百度云盘 传送门 密码:y0dk 图片掀开切换效果: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...

  7. JS框架_(Popup.js)3D对话框窗口插件

    百度云盘 传送门 密码:afdo 3D对话框窗口插件效果: <!doctype html> <html lang="zh"> <head> &l ...

  8. ImageIO类说明

    最近的项目中遇到ImageIO,因此记录下这个类的用法 一.ImageIO: 这个类中的方法都是静态方法,可以用来进行简单的图片IO操作 1.读入的三种方法 public static Buffere ...

  9. 关键字transient是干啥的

    百度百科的解释: Java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持.换句话来说就是,用transient关键字标记的成员变量不参与序列化过程. ...

  10. GC详解

    GC,即就是Java垃圾回收机制.目前主流的JVM(HotSpot)采用的是分代收集算法.与C++不同的是,Java采用的是类似于树形结构的可达性分析法来判断对象是否还存在引用.即:从gcroot开始 ...