AgPro

导航

STL的vector、map、pair举例

#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;

int main()
{
	vector<int> the_vector;
	vector<int>::iterator the_iterator;
	for( int i=0; i < 10; i++ )
		the_vector.push_back(i);
	int total = 0;
	the_iterator = the_vector.begin();
	while( the_iterator != the_vector.end() ) 
	{
		total += *the_iterator;
		the_iterator++;
	}
	cout << "Total=" << total << endl;

	map<string, int> gMap;
	string strTemp = "help";
	map<string ,int>::iterator it = gMap.find(strTemp);	
	if(it == gMap.end())	//add to map if not exist
		gMap.insert(map<string, int>::value_type(strTemp, 1));	
	else	//increase if exist
		++ (*it).second;
	vector< pair<string,int> > wd(gMap.begin(), gMap.end());
	for(vector< pair<string, int> >::iterator it=wd.begin(); it != wd.end(); ++it)
	{
		cout<< it->first.c_str() 
			<< "-" 
			<< it->second
			<< endl;
	}

	return 0;
}

posted on 2010-06-11 17:01  AgPro  阅读(2034)  评论(0编辑  收藏  举报