STL
参考资料
STL用法总结
STL的基本概念
基本组件
名称 | 解释 |
---|---|
容器(Container) | 容纳包含一组元素的对象 |
迭代器(iterator) | 迭代器是泛化的指针,提供了访问容器中每个元素的方法 |
函数对象(function object) | 行为类似于函数的对象,头文件<functional> |
算法(algorithms) | 广泛用于不同对象和内置的数据类型,头文件<algorithm> |
(1)容器
包含一组元素的对象
a.容器分类
容器模板 | 举例 |
---|---|
顺序容器 | array(数组)、vector(向量)、deque(双端队列)、forward_list(单链表)、list(列表) |
有序关联容器 | set(集合)、multiset(多重集合)、map(映射)、multimap(多重映射) |
无序关联容器 | unordered_set(无序集合)、unordered_multiset(无序多重集合)、unordered_map(无序映射)、unordered_multimap(无序多重映射) |
b.容器的通用功能
功能名称 | 功能解释 |
---|---|
begin()end() | 获得容器首尾迭代器 |
clear() | 将容器清空 |
empty() | 断容器是否为空 |
size() | 得到容器元素个数 |
s1.swap(s2) | 将s1和s2两容器内容交换 |
相关数据类型 | |
S::iterator:指向容器元素的迭代器类型 | |
S::const_iterator:常迭代器类型 |
c.顺序容器
顺序容器的一些基本功能
栈和队列共同支持的操作
操作名称 | 功能解释 |
---|---|
s1 op s2 | op可以是==、!=、<=、>、>= |
s,size() | 返回s中元素的个数 |
s.empty() | 返回s是否为空 |
s,push(t) | 将元素t压入到s |
s.pop() | 将一个元素从s中弹出,栈弹出的是最后被压入的元素,队列是最先被压入的元素 |
栈和对列不同的操作 | |
栈:s.top()返回栈顶元素的引用 | |
队列:s.front()获得对头元素的引用;s.back()获得队尾元素的引用 | |
d.关联容器
特点:每个关联容器都有一个键(key)
可以根据高效地查找元素。
接口:插入(insert)删除(erase)查找(find)定界(lower_bound、upper_bound、equal_range)计数(count)
集合:用来存储一组无重复的元素。集合元素本身是有序的,可以高效查找指定元素也可以方便得到指定元素在容器中所处的区间
尾迭代器,指向集合中的下一个位置
eg :找出集合中数据的最大值最小值,输出中值
# include <iostream>
# include <utility>
# include <iterator>
# include <set>
using namespace std;
int main(){
set<double>s;
while (true)
{
double v;
cin>>v;
if (v==0) break;
pair<set<double>::iterator,bool>r=s.insert(v);
if(!r.second)
cout<<v<<"is duplicated"<<endl;
}
set<double>::iterator iter1 = s.begin();
set<double>::iterator iter2 = s.end();
double medium = (*iter1+*(--iter2))/2;
cout<<"<=medium"<<endl;
cout<<">=medium"<<endl;
copy(s.begin(),s.upper_bound(medium),ostream_iterator<double>(cout," "));
copy(s.lower_bound(medium),s.end(),ostream_iterator<double>(cout," "));
return 0;
}
映射(map)
与集合的对比
映射与集合同属于单重关联容器,它们的主要区别在于集合的元素类型是键本身,而映射的元素是由键和附加数据所构成的二元组。
在集合中按照键查找元素,只能确定元素是否存在;
在映射中查找元素,除了确定存在以外还可以得到相应的附加数据;
eg:有5门课,从中选择3门,输出学分总和
# include <iostream>
# include <utility>
# include <string>
# include <map>
# include <iterator>
using namespace std;
int main(){
map<string,int>courses;
courses.insert(make_pair("A++",3));
courses.insert(make_pair("B++",2));
courses.insert(make_pair("C++",4));
courses.insert(make_pair("D++",4));
courses.insert(make_pair("E++",5));
int n=3;//选课门数
int sum =0;//学分总数
while (n>0)
{
string name;
cin>>name;
map<string,int>::iterator iter = courses.find(name);
if(iter == courses.end()){
cout<<name<<"is not available"<<endl;
}
else
{
sum +=iter->second;
courses.erase(name);
n--;
}
}
cout<<"Totol credit"<<sum<<endl; /* code */
return 0;
}
eg:统计输入字符的数据
# include <iostream>
# include <utility>
# include <string>
# include <map>
# include <iterator>
using namespace std;
int main(){
map<char,int>s;
char c;
do{
cin>>c;
if (isalpha(c))
{
c = tolower(c);//不区分大小写就加一句这样的转换
s[c]++;
}
}while (c!='.');//字符串输出结束标记
for (map<char,int>::iterator iter=s.begin(); iter !=s.end(); ++iter)
{
cout<<iter->first<<" "<<iter->second<<" "<<endl;//输出统计结果
}
return 0;
}
多重集合多重映射
多重集合:允许有重复元素的集合,多重映射允许一个键对应你多个附加数据的映射
eg:上课时间的查询(一门课可以有多个上课时间)
(2)迭代器
a.基本定义:是算法和容器的桥梁
b.迭代器相关的基本操作:记住几个与指针相关的就好
迭代器的辅助函数
advance(p,n)对p执行n次自增操作
distance(first,last)计算两个迭代器first和last的距离,即对first执行多少次“++”操作能够使得first==last
(3)函数对象
a.基本概念
一个行为类似函数的对象,
可以没有参数,也可以带有若干参数
其功能是获取一个值,或者改变操作的状态。
b.STL提供的函数对象
eg.利用STL标准函数对象multiplies实现对元素的连乘操作
# include <iostream>
# include <utility>
# include <numeric>
# include <functional>
using namespace std;
int main(){
int a[]={1,2,3,4,5};
const int N =sizeof(a)/sizeof(int);
cout<<"The result by multipling all elements in A is"<<" "<<
accumulate(a,a+N,1,multiplies<int>())<<endl;
return 0 ;
}
eg将元素由大到小排序
# include <iostream>
# include <algorithm>
# include <vector>
# include <functional>
# include <iterator>
using namespace std;
int main(){
int intArr[]={30,90,60,70,50,20,80};
const int N =sizeof(intArr)/sizeof(int);
vector<int> a(intArr,intArr+N);
cout<<"before sorting:"<<endl;
copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t"));
cout<<endl;
sort(a.begin(),a.end(),greater<int>());
cout<<"after sorting:"<<endl;
copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t"));
}
(4)算法
a.算法特点
本身是一种函数模板,STL算法是通用的,独立于数据类型容器类型;
通过迭代器获得输入数据,通过函数对象进行数据处理,通过迭代器将结果输出;
b.算法分类
算法类型 | 算法说明 | 应用举例 |
---|---|---|
不可变序列算法 | 不直接修改容器内容的算法 | 查找指定容器、比较两个序列是否相等、对元素进行计数 |
可变序列算法 | 修改所操作容器的对象 | 对序列进行杀出、替换、修改等 |
排序和搜索算法 | 常用的排序和搜索算法都有 | |
数值算法 | 利用元素进行数值运算 |