STL--vertor、map

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

结果截图:

STL--vertor、map_第1张图片

你可能感兴趣的:(STL--vertor、map)