您的位置:首页 > 其它

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;
}

结果截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: