STL--vertor、map
2016-06-08 17:21
295 查看
STL重要的内容vector和map容器,这也是我将要使用的工具。
#include <vector>
#include <algorithm>
#include <utility> // pair
#include <map>
using namespace std;
template <class T>
void print(T a)
{
cout << a;
}
template <class T, class S>
void printPair(pair<T, S> p)
{
cout << p.first << ":" << p.second << endl;
}
int main()
{
vector<int> vec; // 定义int类型数组
for (int i = 0; i < 9; i++)
{
vec.push_back(i); // 向数组存入数据(从后面存储)
}
for_each(vec.begin(), vec.end(), print<int>); // 遍历容器 vec
cout << endl;
std::map<std::string, std::string> m_map; // map 映射
m_map.insert(std::make_pair("Hello","World")); // 向 map 中插入内容
m_map.insert(std::make_pair("端午节", "放假"));
std::for_each(m_map.begin(), m_map.end(), printPair<std::string, std::string>);
// 查找map里面的内容
//cout << m_map["Hello"];
//cout << m_map["端午节"];
system("pause");
return 0;
}
结果截图:
#include <vector>
#include <algorithm>
#include <utility> // pair
#include <map>
using namespace std;
template <class T>
void print(T a)
{
cout << a;
}
template <class T, class S>
void printPair(pair<T, S> p)
{
cout << p.first << ":" << p.second << endl;
}
int main()
{
vector<int> vec; // 定义int类型数组
for (int i = 0; i < 9; i++)
{
vec.push_back(i); // 向数组存入数据(从后面存储)
}
for_each(vec.begin(), vec.end(), print<int>); // 遍历容器 vec
cout << endl;
std::map<std::string, std::string> m_map; // map 映射
m_map.insert(std::make_pair("Hello","World")); // 向 map 中插入内容
m_map.insert(std::make_pair("端午节", "放假"));
std::for_each(m_map.begin(), m_map.end(), printPair<std::string, std::string>);
// 查找map里面的内容
//cout << m_map["Hello"];
//cout << m_map["端午节"];
system("pause");
return 0;
}
结果截图:
相关文章推荐
- Rundeck任务自动化发布系统部署与配置
- Java BIO、NIO、AIO
- C++Primer 1.2章笔记整理
- 第13周动物这样叫2-2
- SQL Server笔记1
- 在dialog中获取Edittext的值
- zaker splash页面实现
- CentOS部署应用常用命令汇总
- 面向对象的思想
- 循环数组——gas station
- 程序员福利:各大平台免费接口
- windows命令
- (串口通信编程) 开源串口调试助手Common (Com Monitor)
- 警察与厨师(2)
- Java设计模式
- 给libcef_dll_wrapper生成VS工程
- Distributed Result Grouping Caveats
- 0x00到0xFF二进制数值中1的的个数
- date
- 红外发送代码分析