博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ map容器-58-map容器大小/交换/插入/删除
阅读量:4301 次
发布时间:2019-05-27

本文共 3192 字,大约阅读时间需要 10 分钟。

接着学习map容器的常见API操作,依然是大部分前面学习过的,名称相同的API的基本调用测试。本篇学习map容器大小相关API,然后学习容器交换,容器元素插入和删除,清空等操作。

1.map容器大小操作

统计map容器大小的函数原型

#include 
#include
#include
using namespace std;void test01(){ // map容器的构造 map
m; // map容器添加元素 m.insert(pair
("Tom", 18)); m.insert(pair
("Anthony", 23)); m.insert(pair
("Bob", 24)); m.insert(pair
("Sunny", 19)); // 统计容器大小 cout << "size of Map: " << m.size() << endl; // 容器是否为空 cout << "is empty? " << m.empty() << endl;}int main(){ test01(); system("pause"); return 0;}

运行结果

数字0表示非空,数字1表示容器为空

 

2.map容器交换

函数原型还是swap(map)

#include 
#include
#include
using namespace std;void printMap(map
& m){ for(map
::iterator it = m.begin(); it != m.end(); it++) { cout << "Key= " << (*it).first << " ,Value= " << (*it).second << endl; }}void test01(){ // map容器的构造 map
m; // map容器添加元素 m.insert(pair
("Tom", 18)); m.insert(pair
("Anthony", 23)); m.insert(pair
("Bob", 24)); m.insert(pair
("Sunny", 19)); map
m1; m1.insert(pair
("aaaa", 243254)); m1.insert(pair
("bbbb", 10000)); cout << "=======m=========== " << endl; printMap(m); cout << "========m1========== " << endl; printMap(m1); cout << "================== " << endl; // 容器交换 m.swap(m1); cout << "=======m=========== " << endl; printMap(m); cout << "========m1========== " << endl; printMap(m1); }int main(){ test01(); system("pause"); return 0;}

运行结果

 

3.map容器插入和删除

相关插入和删除函数原型

插入这里不重复,就是前面一直在使用的insert()函数。

#include 
#include
#include
using namespace std;void printMap(map
& m){ for(map
::iterator it = m.begin(); it != m.end(); it++) { cout << "Key= " << (*it).first << " ,Value= " << (*it).second << endl; }}void test01(){ // map容器的构造 map
m; // map容器添加元素 m.insert(pair
("Tom", 18)); m.insert(pair
("Anthony", 23)); m.insert(pair
("Bob", 24)); m.insert(pair
("Sunny", 19)); printMap(m); //删除迭代器所指的元素 map
::iterator pos = m.begin(); m.erase(pos); cout << "after erase pos=======================" << endl; printMap(m); //删除容器中值为Sunny m.erase("Sunny"); cout << "after erase key=======================" << endl; printMap(m); // 清空容器 m.clear(); cout << "is Empty? " <
<< endl; }int main(){ test01(); system("pause"); return 0;}

测试结果

 

4.map容器查找和统计

接着来看看查找和统计的API

#include 
#include
#include
using namespace std;void printMap(map
& m){ for(map
::iterator it = m.begin(); it != m.end(); it++) { cout << "Key= " << (*it).first << " ,Value= " << (*it).second << endl; }}void test01(){ // map容器的构造 map
m; // map容器添加元素 m.insert(pair
("Tom", 18)); m.insert(pair
("Anthony", 23)); m.insert(pair
("Bob", 24)); m.insert(pair
("Sunny", 19)); printMap(m); // 查找 map
::iterator pos = m.find("Anthony"); if(pos != m.end()) { cout << "find result: " << (*pos).first << " = " << (*pos).second << endl; } else { cout << "not find"<< endl; } // 统计,要不是返回结果0就是1 cout << "count of Sunny:"<< m.count("Sunny") << endl;}int main(){ test01(); system("pause"); return 0;}

测试结果

转载地址:http://texws.baihongyu.com/

你可能感兴趣的文章
通知机制 (Notifications)
查看>>
10 Things You Need To Know About Cocoa Auto Layout
查看>>
C指针声明解读之左右法则
查看>>
一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
查看>>
iOS 如何放大按钮点击热区
查看>>
ios设备唯一标识获取策略
查看>>
获取推送通知的DeviceToken
查看>>
Could not find a storyboard named 'Main' in bundle NSBundle
查看>>
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Swift code into Object-C 出现 ***-swift have not found this file 的问题
查看>>
为什么你的App介绍写得像一坨翔?
查看>>
RTImageAssets插件--@3x可自动生成@2x图片
查看>>
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
点击button实现Storyboard中TabBar Controller的tab切换
查看>>