map member functions
搜了才发现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的更多相关文章
- C++ 之const Member Functions
Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter lis ...
- C++ Member Functions的各种调用方式
[1]Nonstatic Member Functions(非静态成员函数) C++的设计准则之一就是:nonstatic member function至少必须和一般的nonmember funct ...
- 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 ...
- virtual member functions(单一继承情况)
virtual member functions的实现(就单一继承而言): 1.实现:首先会给有多态的class object身上增加两个members:一个字符串或数字便是class的类型,一个是指 ...
- C++:Special Member Functions
Special Member Functions 区别于定义类的行为的普通成员函数,类内有一类特殊的成员函数,它们负责类的构造.拷贝.移动.销毁. 构造函数 构造函数控制对象的初始化过程,具体来说,就 ...
- 2_成员函数(Member Functions)
成员函数以定从属于类,不能独立存在,这是它与普通函数的重要区别.所以我们在类定义体外定义成员函数的时候,必须在函数名之前冠以类名,如Date::isLeapYear().但如果在类定义体内定义成员函数 ...
- [EffectiveC++]item23:Prefer non-member non-friend functions to member functions
99页 导致较大封装性的是non-member non-friend函数,因为它并不增加“能否访问class内之private成分”的函数数量.
- C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)
4.4 指向Member Function的指针 (Pointer-to-Member Functions) 取一个nonstatic data member的地址,得到的结果是该member在 cl ...
- C++ std::map
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...
随机推荐
- head first 设计模式笔记3-装饰者模式:星巴兹饮料
开放原则:类应该对扩展开放,对修改关闭. - 上篇博客中的观察者模式中,通过加入新的观察者,我们可以在任何时候扩展主题(Subject),而且不需向主题中添加代码. - 装饰者模式也完全遵循开放原则. ...
- Nowcoder 练习赛26 D xor序列 ( 线性基 )
题目链接 题意 : 中文题.点链接 分析 : 对于给定的 X 和 Y 假设存在一个 Z 使得 X (xor) Z = Y 做一个变形 X (xor) Z (xor) Y = 0 X (xor) Y = ...
- UVA 11181 Possibility Given
#include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<cmath> ...
- Jmeter(一) 安装
一.检查JDK版本 执行cmd > java -version 查看本机JDK版本,JDK版本不能低于1.6 二.软件下载 登录Jmeter官网:https://jmeter.apache.or ...
- happens-before规则和as-if-serial语义
JSR-133使用happens-before的概念来阐述操作之间的内存可见性.在JMM中,如果一个操作执行的结果需要对另一个操作可见, 那么这2个操作之间必须要存在happens-before关系. ...
- BZOJ 5326 [JSOI2017]博弈 (模拟费用流、线段树)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5326 题解 终于成为第8个A掉这题的人--orz tzw神仙早我6小时 本以为这东西常数 ...
- JavaWeb_(SSH论坛)_六、点赞模块
基于SSH框架的小型论坛项目 一.项目入门 传送门 二.框架整合 传送门 三.用户模块 传送门 四.页面显示 传送门 五.帖子模块 传送门 六.点赞模块 传送门 七.辅助模块 传送门 联合主键 创建p ...
- MariaDB 默认是禁止远程访问的 我们改掉它
查询用户账号信息: select User, host from mysql.user; 现在只显示 root账户中的host项是localhost表示该账号只能进行本地登录,我们需要修改权限,输入 ...
- Java密码处理
- SpringMVC配置多个自定义拦截器
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...