stl:standard library
1、包括:(都是小写)
(1)a pair class(pair of anything,int/int,int/char)
(2)容器:vector——可扩展数组;deque——可扩展数组,两端扩展;list——双向链表——sets(集合:没有重复、无序) and maps(映射,key-value)
(3)基本算法:sort,search
2、使用命名空间:using namespace std;
3、stl的三个主要部分:容器、算法、枚举器
4、 三种数据结构:map、vector、list
eg1:使用vector类
#include<iostream>
#inlclude<vector>
using namespace std;
int main(){
vector<int> x;//x是对象,可以放int
for(int a=0;a<1000;a++)
x.push_back(a);//向vector放东西,有1000个整数
vector<int>::iterator p;//模板类,::作用域解析符,vector<int>的iterator
for(p=x.begin();p<x.end();p++)//p表示x的第一个元素,用.end判断是否到结尾
cout<<*p<<" ";//*p给出元素,给出int;*p可以被重载
return 0;
}
stl很大程度上依靠两种东西:模板、重载
eg2:使用list类
#include<iostream>
using namespace std;
#include<list>
#include<string>
int main(){
list<string> s;
s.push_back("hello");
s.push_back("world");
s.push_front("tide");
s.push_front("crimson");
s.push_front("alabama");
list<string>::iterator p;
for(p=s.begin();p!=s.send();p++)//list用!=判断是否到结尾(通用)
cout<<*p<<" ";
cout<<endl;
}```
//
#include<map>
#include<string>
map<string,float> price;
price["snapple"] = 0.75;
price["coke"]=0.5;
string item;
double total=0;
while(cin>>item)
total+=price[item];