C++stl翁恺老师笔记

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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值