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 ...
随机推荐
- jquery click()方法 语法
jquery click()方法 语法 作用:当点击元素时,会发生 click 事件.当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click.click() 方法触发 click ...
- bytes和bytearray总结
The core built-in types for manipulating binary data are bytes and bytearray. They are supported by ...
- android 支持发送空短信
method:A) AP端修改:1.将ComposeMessageActivity.java 中的 isPreparedForSending() 作如下修改(删掉的code也可以注释掉)private ...
- 深入理解二阶段提交协议(DDB对XA悬挂事务的处理分析)(一)
https://sq.163yun.com/blog/article/165554812476866560
- Jmeter -- 入门,基础操作
1. 添加线程组 设置线程组参数(线程数.准备时长.循环次数等): a)线程数:虚拟用户数.一个虚拟用户占用一个进程或线程.设置多少虚拟用户数在这里也就是设置多少个线程数. b)Ramp-Up Per ...
- [CSP-S模拟测试]:你相信引力吗(单调栈)
题目传送门(内部题124) 输入格式 第一行一个整数$n$代表环的长度. 第二行$n$个整数表示每个冰锥的高度. 输出格式 一行一个整数表示有多少对冰锥是危险的. 样例 样例输入1: 51 2 4 5 ...
- python中的实例方法、类方法、静态方法的区别
Python 除了拥有实例方法外,还拥有静态方法和类方法,跟Java相比需要理解这个类方法的含义. class Foo(object): def test(self)://定义了实例方法 print( ...
- 胜利点20191010-6 alpha week 1/2 Scrum立会报告+燃尽图 04
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8749 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...
- 5、kubernetes资源清单之Pod应用190709
一.Pod镜像及端口 获取帮助文档 # kubectl explain pod.spec.containers spec.containers <[]object> pod.spec.co ...
- LeetCode 96. 不同的二叉搜索树(Unique Binary Search Trees )
题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 输出: 解释: 给定 n = , 一共有 种不同结构的二叉搜索树: \ / / / \ \ / / ...