首页 > 代码库 > c++ stl常用
c++ stl常用
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<iterator>
#include<map>
#include<fstream>
#include <string>
#include <hash_map>
using namespace std;
int main()
{
/* //vector have not push_front
vector<string> vstr(10,"str");
vector<string>::iterator itvs=vstr.begin();
vstr.push_back("lll");
vstr.push_back("dddddd");
vstr.pop_back();
for(itvs=vstr.begin(); itvs!=vstr.end(); itvs++)
{
cout<<*itvs<<"***";
}
cout<<vstr.back()<<vstr.size()<<vstr.front()<<endl;
//list
list<string> slist;
slist.push_back("aaa");
slist.push_back("hhh");
slist.push_front("***");
list<string>::iterator it=slist.begin();
slist.insert(it,"front");
for(it=slist.begin(); it!=slist.end(); it++)
{
cout<<*it<<"___";
}
cout<<endl;
slist.sort();
slist.erase(--it);//it 指向最后一个元素的下一个元素
for(it=slist.begin(); it!=slist.end(); it++)
{
cout<<*it<<"___";
}
//map
map<string,int> one;
one["the"]=3;
one.insert(map<string ,int>::value_type("as",2));/////!!!注意格式
map<string ,int>::iterator itmap=one.begin();
cout<<itmap->first<<":";
cout<<itmap->second;
one.erase("the");
ifstream in("in.txt");
string word;
//统计单词个数
while(in>>word){
one[word]+=1;
}
for(itmap=one.begin();itmap!=one.end();itmap++)
{
cout<<itmap->first<<":"<<itmap->second<<endl;
}
//set have not push pop
set<int> a;
a.insert(19);
a.clear();
cout<<a.count(0);//统计指定键的个数
for(int i=0;i<10;i++)
{
a.insert(i);
}
//集合无序,没有sort,映射也没有sort*/
/////////////以下编译不通过
hash_map<int, string> mp;
mp[9527] = "唐伯虎点秋香";
mp[10000] = "百万富翁的生活";
mp[88888] = "白领的工资底线";
if(mp.find(10000) != mp.end())
{
cout<<"lll" <<end;//....
}
}
c++ stl常用