map-有序 multimap-可重复 unordered_map-无序
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <algorithm>
using namespace std;
void OutPut(char val)
{
cout << val << ' ';
}
int main(int argc, char const *argv[])
{
vector<char> cVec;
cVec.push_back('Z');
cVec.push_back('D');
cVec.push_back('W');
cVec.push_back('S');
cVec.push_back('J');
cVec.push_back('A');
cVec.push_back('U');
cout << "用C++11 for(:)进行遍历" << endl;
cout << "向量排序前:\n";
for(auto x : cVec){
OutPut(x);
}
// algorithm内的函数模板 底层还是for(begin,!=end)
cout << endl << endl << "用algorithm内的函数模板for_each进行遍历:" << endl;
for_each(cVec.begin(), cVec.end(), [](char val){cout << val << ' ';});
//for_each(cVec.begin(), cVec.end(), OutPut);
// 排序
sort(cVec.begin(), cVec.end());
cout << endl << endl << "向量排序后:\n";
for(auto x : cVec){
OutPut(x);
}
// for_each(cVec.begin(), cVec.end(), OutPut); // algorithm内的函数模板
// map
cout << endl;
cout << endl;
cout << "map示例:" << endl;
map<int, int> imap;
//multimap<int, int> imap;
//unordered_map<int, int> imap;
//imap.insert(unordered_map<int, int>::value_type(100, 1000));
// 三种方式插入
imap.insert(make_pair(1, 10));
imap.insert(make_pair(4, 40));
imap.insert(make_pair(5, 50));
imap.insert(make_pair(9, 90));
imap.insert(map<int, int>::value_type(8, 800));
imap.insert(pair<int,int>(2,20));
// 如果需要更改值的话,用引用for(auto& it : imap)
for(auto it : imap)
{
cout << "Key:" << it.first << " --> Value:" << it.second << "\n";
}
cout << endl;
system("pause");
return 0;
}
map-有序 multimap-可重复 unordered_map-无序的更多相关文章
- stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list
stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...
- STL(六)——map、multimap
STL--map.multimap 文章目录 STL--map.multimap 关联容器与map的介绍 map与set的异同 map与multimap的异同 map类对象的构造 map添加元素 ma ...
- 4.3 map和multimap
使用map multimap必须包含头文件map *:multimap 1)multimap定义 template<class Key,class Pred=less<Key>,cl ...
- 使用multimap创建重复键关联容器
在“使用 <map> 库创建关联容器”一文中,我们讨论了标准库中的 map 关联容器.但那只是 map 容器的一部分.标准库还定义了一个 multimap 容器,它与 map 类似,所不同 ...
- STL的基本使用之关联容器:map和multiMap的基本使用
STL的基本使用之关联容器:map和multiMap的基本使用 简介 map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序.两者不同在于map ...
- STL标准库-容器-map和multimap
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 map与multimap为关联容器,结构如下 map底层实现依然是rb_tree 他的data可以改,但是key不能改,因此ma ...
- 【C++ STL】Map和Multimap
1.结构 Map和multimap将key/value pair(键值/实值 队组)当作元素,进行管理.他们根据key的排序准则将元素排序.multimap允许重复元素,map不允许. 元素要求: k ...
- STL学习笔记— —容器map和multimap
简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...
- C++标准库分析总结(六)——<Map、Multimap、Set、Multiset设计原则>
关联容器我们可以看做是一个小型的数据库,它就是用key找value,编译器底层对于关联容器的实现有两种:红黑树(Red-Block tree)和哈希表(hash table,散列表). 一.红黑树简单 ...
- STL之Map和multimap容器
1.Map和multimap容器 1)map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. 2)map中key值是唯一的.集合中的元素按一 ...
随机推荐
- 重启部署在阿里云上的huginn
背景,因为重新编译安装了gcc,不知怎么服务器上的huginn就停了, 因为之前是安装在docker上的,服务器重启之后需要:1.启动dockerservice docker start2.命令创建h ...
- Python网络爬虫之三种数据解析方式
1. 正则解析 正则例题 import re # string1 = """<div>静夜思 # 窗前明月光 # 疑是地上霜 # 举头望明月 # 低头思故乡 ...
- css加载动画...
加载动画... <p>加载动画...</p> <p> </p> <p> </p> <style><!-- .c ...
- Linux bash基础特性一
命令别名 alias cdnet=”cd /etc/sysconfig/network-scripts” 针对用户的别名: “~/.bashrc” 针对系统的别名:”/etc/bashrc” 重读配置 ...
- react_app 项目开发 (2)_axios_pubsub-js
生产环境打包并运行 yarn run build 会src代码进行打包处理,在内存中生成打包文件 将打包文件保存至内存 yarn global add serve serve -s build 将 b ...
- Qt中在UI文件中新建控件并命名,但在代码中无法识别UI中的控件?
代码中添加FilePathLineEdit控件,显示标准文件选择对话框显示选择的文件路径,但在槽函数中ui->FilePathLineEdit->setText("FilePat ...
- three.js 使用DragControls.js 拖动元素
首先,引入js文件: <script type="text/javascript" src="./path/to/DragControls.js"> ...
- 原生ajax可变参数post
var Ajax = { post:function(){ xmlhttp = new XMLHttpRequest(); var len = arguments.length; var url = ...
- laravel整合workerman做聊天室
测试工具 http://www.blue-zero.com/WebSocket/ 2018年8月6日17:28:24 <?php namespace App\Console\Commands; ...
- init();
<script language="javascript" type="text/javascript">window.onload = funct ...