// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<bitset>
#include<string>
#include<math.h>
#include<map>
#include<set>
#define MAX 30
using namespace std;
int main(int argc, _TCHAR* argv[])
{
pair<string,int> p1;//不提供初始化,采用值初始化
pair<string,int> p2("hit",10086);//提供初始化式
p1 = make_pair("hit",10086);//pair也是一种模板类,必须提供两个类型
cout<<p1.first<<endl;//成员是public类型的,可以直接访问
cout<<p1.second<<endl;
p1.first = "hit";
cout<<unitbuf<<p1.first<<nounitbuf;
cout<<endl<<p1.second<<endl;
map<string,int> tel;
tel["hit"] = 10086;//下标操作的时候,返回的是类型为mapped_type的左值,有就改变,没有就加进去
map<string,int>::iterator ite = tel.begin();//对迭代器解引用,返回的是类型为value_type的引用
map<string,int>::iterator end = tel.end();
while(ite!=end){
cout<<ite->first<<endl;
cout<<ite->second<<endl;
++ite;
}
string word;
map<string,int> wordCount;
while(cin>>word){
if(cin.bad()){
throw runtime_error("error");
break;
}
if(cin.fail()){
cout<<"Error string"<<endl;
cin.clear(cin.failbit);
continue;
}
++wordCount[word];
}
pair<map<string,int>::iterator,bool> result =wordCount.insert(make_pair("tang",10086));
if(result.second){
cout<<"OK"<<endl;
cout<<result.first->first<<" "<<result.first->second<<endl;
}
wordCount.insert(tel.begin(),tel.end());
ite = wordCount.begin();
end = wordCount.end();
while(ite!=end){
cout<<ite->first<<endl;
cout<<ite->second<<endl;
++ite;
}
map<string,int>::size_type num = wordCount.erase("hit");
cout<<num<<endl;
map<string,int>::iterator p = wordCount.begin();
wordCount.erase(p);
}
|