STL

13 STL

13.1 STL

容器 Container

迭代器 Iterator

适配器 Adapter

算法 Algorithm

13.1.1 容器 Container

容纳一些数据的模板类 vector list deque

set map multimap multiset

13.1.2 适配器 Interface

stack queue priority_queue

13.1.3 迭代器 Iterator

广义指针

随机访问迭代器 Random Access Iterator

双向迭代器 Bidirectional Iterator

前向迭代器 Forward Iterator

输入迭代器 Input Iterator

输出迭代器 Output Iterator

13.2 序列式容器

vector list deque·

#include<iostream>
#include<vector>/*引用头文件*/
using namespace std;
int main()
{
   vector<int> obv;/*创建空的容器*/
   vector<int> obv1(10);/*产生特定大小的容器,容器中的元素被创建,但未被初始化,编译器采用默认值为元素初始化,对类对象调用无参构造函数*/
   vector<int> obv2(10,8);/*产生特定大小的容器,容器中的元素被创建并初始化*/
   vector<int> obv3(obv2);/*复制obv2*/
   int sz[5]={1,2,3,4,5};
   vector<int> obv4(sz,sz+5);
   /*对于vector和deque可采用以下方式随即访问,注意防止越界
list不支持以下方式,只能采用迭代器间接访问*/
   for(unsigned int i=0;i<obv4.size();i++)
       cout<<obv4[i]<<" ";
   cout<<endl;
   for(int i=0;i<(int)obv4.size();i++)
       cout<<obv4.at(i)<<" ";
   cout<<endl;
   /*创建迭代器,类似指针,指向第一个元素*/
   vector<int>::iterator iter=obv4.begin();
   /*迭代器间接访问*/
   while(iter!=obv4.end())
       cout<<(*iter++)<<" ";
   cout<<endl;
   return 0;
}
13.2.2 所有容器支持的特征
表达式返回值说明复杂度
ob.begin()迭代器指向容器第一个元素的迭代器固定
ob.end()迭代器指向容器末尾元素的下一个迭代器固定
ob.size()size_type返回元素个数固定
ob.swap(ob2)void交换固定
ob1==ob2boolob1与ob2长度相同,且每个元素都相等时返回真线性
ob1!=ob2bool!(ob1==ob2)线性
13.2.3 序列式容器中元素的插入和删除
  1. push_back(t) pop_back(void) //容器末插入删除元素
  2. push_front(t) pop_front(void) //vector不适用 容器内插入删除元素
  3. front(void) back(void) //返回容器的最前端和最后端元素
  4. insert插入
/*insert 3种用法*/
#include<iostream>
#include<vector>/*引用头文件*/
using namespace std;
void disp(vector<int>&t)
{
   vector<int>::iterator iter=t.begin();
   while(iter!=t.end())
       cout<<(*iter++)<<" ";
   cout<<endl;
}
int main()
{
    const int num=4;
    int sz[num]={6,7,8,9};
  vector<int> obv(sz,sz+num);/*采用数组赋值*/
  vector<int>::iterator pD=obv.end();/*创建迭代器pD*/
   /*1.将元素插入迭代器p之前,返回的迭代器指向被安插的元素*/
    /*迭代器不再指向原来位置,由于系统为容器的扩充或删除等操作造成,在使用此迭代器前,必须对迭代器重新定位*/
  pD=obv.insert(pD,1);/*在尾部插入元素1,返回的迭代器指向新插入的1*/
  disp(obv);
  /*2.将元素插入迭代器p之前,个数为num个,每个值为t,无返回值*/
  obv.insert(obv.end(),2,4);
  disp(obv);
  /*3.在迭代器p之前安插[first,last]之间的所有元素*/
  obv.insert(obv.end(),sz,sz+num);
  disp(obv);
   return 0;
}

  1. erase 删除操作 vector和deque支持下标运算符随机访问,list不能。
#include<iostream>
#include<list>/*引用头文件*/
using namespace std;
void disp(list<int>&t)
{
   list<int>::iterator iter=t.begin();
   while(iter!=t.end())
       cout<<(*iter++)<<" ";
   cout<<endl;
}
int main()
{
    const int num=4;
    int sz[num]={6,7,8,9};
  list<int> obl(sz,sz+num);/*采用数组赋值*/
  disp(obl);
  list<int>::iterator iter=obl.begin();
  iter++;
  /*1.抹去迭代器指向的元素,返回指向所删除元素的下一个元素的迭代器*/
  iter=obl.erase(iter);/*抹掉第2个元素,iter指向第3个元素*/
  disp(obl);
  /*2.抹去[first,end]之间的所有元素,返回指向被删除的一片元素的下一个元素的迭代器*/
  obl.erase(iter,obl.end());
  disp(obl);
   return 0;
}

13.2.4 vector容器

同数组一样,在(push_back\pop_back)尾部添加删除时间固定,在中间或者头部增删(insert\erase)正比元素个数

13.2.5 deque容器

双端队列,经常在头部删减推荐使用。

13.2.6 list容器

双向链表,只能双向遍历

13.3 关联式容器

将值和关键字成对关联

set 仅包含关键字,没有值的概念

map 关键字-值 对

map set不会出现多个相同的关键字

multiset和multimap是map和set的扩展,允许相同关键字存在。

13.3.1 set容器
#include<iostream>
#include<set>/*引用头文件*/
using namespace std;
void disp(set<int>&t)
{/*set不支持随机访问,必须通过迭代器方式对set容器对象中的元素进行访问*/
   set<int>::iterator iter=t.begin();
   while(iter!=t.end())
       cout<<(*iter++)<<" ";
   cout<<endl;
}
int main()
{
    const int num=5;
    int sz[num]={1,3,4,6,5};
    set<int> obs(sz,sz+num);/*默认情况下,将使用less<>模板,从小到大排序*/
    disp(obs);
   return 0;
}
13.3.2 multiset容器
#include<iostream>
#include<set>/*引用头文件*/
using namespace std;
void disp(multiset<int>&t)
{
   multiset<int>::iterator iter=t.begin();
   while(iter!=t.end())
       cout<<(*iter++)<<" ";
   cout<<endl;
}
int main()
{
    const int num=5;
    int sz[num]={1,1,5,4,4};
    /*与set的不同之处在于其允许相同的元素*/
    multiset<int> obs(sz,sz+num);
    disp(obs);
   return 0;
}
13.3.3 map
pair<const keyword,value> t(keydata,valuedata);
(*iter).first (*iter).second
#include<iostream>
#include<map>/*引用头文件*/
#include<string>
using namespace std;
void disp(map<int,string>&t)
{
   map<int,string>::iterator iter=t.begin();
   while(iter!=t.end())
       cout<<(*iter).first<<" "<<(*iter++).second<<endl;/*first,second没有提示*/
   cout<<endl;
}
int main()
{
   typedef  pair<int,string> item;
    const int num=5;
    item t[num]={item(1,"China"),item(2,"Aemaria"),item (3,"Japan"),item(4,"German")};
    map<int,string> obM(t,t+num-1);
    /*使用迭代器构造*/
    cout<<obM.size()<<endl;
    disp(obM);
   return 0;
}
13.3.4 muitimap

与map的关系,类似于set和multiset允许一个关键字对应多个值

13.4 关联式容器支持的成员函数操作

13.4.1 元素的插入

t对于set和multiset是个关键字,对于map和multimap是个pair结构

成员函数适用容器
pair<iterator,bool> ob.insert(t)map/set
iterator ob.insert(t)multimap/multiset
iterator ob.insert(p,t)map/set/multimap/multiset
void ob.insert(i,j)map/set/multimap/multiset
13.4.2 元素的删除
  1. ob.erase(keyword) 删除ob中所有关键字为keyword的元素,并返回删除元素的个数。

所有容器定义的类型(Type代表容器类型)

类型代表意义
Type::value_TypeType容器对象中存储的元素类型T
Type::referenceT&
Type::const_referenceconst T&
Type::iterator指向T的迭代器
Type::const_iterator指向T的const迭代器
Type::difference_type表示两个迭代器间距离的符号整数
TType::size_type无符号整型,表示容器对象的元素个数,也可作为随机访问时的下标

为关联式容器定义的类型

类型代表意义
Type::key_type关键字类型
Type::mapped_type值类型
Type::key_compare关键字比较函数对象类,默认值为less
Type::value_compare对set来说与key_compare相同,对map,为value_typep提供排序功能
  1. void ob.erase§

删除迭代器p指向的元素

  1. void ob.erase[q1,q2)

从ob删除[q1,q2)的元素

  1. void clear(void)//会自动调用释放内存
  2. void pop_back();
13.4.3 元素的查找与访问

关联式容器元素的查找与访问

类型代表意义
iterator ob.lower_bound(k)指向第一个关键字不小于k的元素
iterator ob.upper_bound(k)指向第一个关键字大于k的元素
pairType::value_type,Type::value_type ob.equal_range(k)返回一个pair结构

13.5 迭代器

随机访问迭代器
双向迭代器
前向迭代器
输入迭代器
输出迭代器

13.6 STL Algorithms

在在 numeric或algorithm库中

13.6.1 函数对象

可以以函数方式与()结合使用的任意对象

  • 函数名
  • 指向函数的指针
  • 重载()操作符的类对象

STL定义了一组函数对象,分为Arithmetic、Relational、Logical,在使用这些函数对象前,必须包含相应头文件functional .
Arithmetic:plus、minus 、negate、multiplies、divides、modules
Relational:less、less_equal、greater、greater_equal、equal_to、not_equal_to
Logical:logical_and、logical_or、logical_not

函数对象
Arithmetic
Relational
Logical
plus
minus
negate
less
less_equal
greater
greater_equal
equal_to
not_equal_to
Logical_and
Logical_or
Logical_not
函数对象
生成器 Generator
一元函数 Unary Function
二元函数 Binary Function
一元断言-断言
二元断言
13.6.2 算法分类

STL非修改式序列操作
STL非修改式序列操作
修改式序列操作
排序和相关操作
命名规则1

算法中 == _if == 后缀代表只有满足一定准则才生效,需要一个断言函数,输入一个元素返回bool量指示是否满足准则

count==_if==,on the other hand, accepts a range of iterators and a predicate function, then returns the number of times the predicate evaluates to true in that range

Algorithms containing the word copy (remove_copy, partial_sort_copy, etc.) will perform some task
on a range of data and store the result in the location pointed at by an extra iterator parameter.

With copy functions, you’ll specify all the normal data for the algorithm plus an extra iterator specifying a destination for the result.

If an algorithm ends in==_n== (generate_n, search_n, etc**)**, then it will perform a certain operation n times.

These functions are useful for cases where the number of times you perform an operation is meaningful,rather than the range over which you perform it.

13.6.1 循环
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    string s="123456";
    /*1*/
    cout<<"for循环: ";
    for(int i=0;i<s.size();++i)
        cout<<s[i]<<" ";
    cout<<endl;
    /*2*/
    cout<<"迭代器: ";
    for(string::iterator it=s.begin();it!=s.end();++it)
        cout<<*it<<" ";
    cout<<endl;
    /*3*/
    cout<<"迭代器简化: ";
    for(auto it=s.begin();it!=s.end();++it)
        cout<<*it<<" ";
    cout<<endl;
    /*4*/
    cout<<"C++11特性: ";
    for(auto x:s) cout<<x<<" ";
    cout<<endl;
    return 0;
}
13.6.2 排序

用于vector,set map自动排序

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    string s="145623";
    sort(s.begin(),s.end());/*添加头文件,无论是否为容器都可用,基本类型也是模板的一种,用于*/
    for(auto x:s) cout<<" "<<x;
    cout<<endl;
    return 0;
}
13.6.3 substr
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    /**begin是头迭代器,end是尾迭代器*/
    string s="5418340";
    //s=s.substr(1,3);//取418,取索引为1,往后截断3个
    s=s.substr(1,-1);//索引为1,截断到最后
    cout<<s;
    return 0;
}
13.6.4 sstream处理字符串

istringstream的构造函数原形如下:
istringstream::istringstream(string str);
它的作用是从string对象str中读取字符,stringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。

#include <iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
    string s="Keep coding ...";
    /*从String对象str中读取字符,stringstream可以绑定一行字符串,
以空格为分隔符把该行分隔开*/
    istringstream is(s);
    while(is)
    {
        string substr;
        is>>substr;
        cout<<substr<<endl;
    }
    return 0;
}

将流输入容器中

#include <iostream>
#include<sstream>
#include<vector>
#include<string>

using namespace std;

int main()
{
    string s="Keep coding ...";
    /*从String对象str中读取字符,stringstream可以绑定一行字符串,
以空格为分隔符把该行分隔开*/
    istringstream is(s);
    string buff;
    vector<string> vs;
    while(is>>buff)
        vs.push_back(buff);
    for(auto x:vs) cout<<x<<endl;
    return 0;
}

逆序单词

#include <iostream>
#include<sstream>
#include<stack>

using namespace std;


int main()
{
string s;
getline(cin,s);
stack<string> ss;
stringstream sBuff;
sBuff<<s;
while(sBuff>>s)
    ss.push(s);
while(!ss.empty())
{
    cout<<ss.top()<<" ";
    ss.pop();
}
cout<<endl;
    return 0;
}

字符串转数字

#include <iostream>
#include<sstream>
#include<stack>

using namespace std;


int main()
{
string s="1234";
    /*方法1*/
int i;
stringstream o;
o<<s;
o>>i;
cout<<i<<endl;
    /*方法2*/
int j=stoi(s);
cout<<i<<endl;
    return 0;
}

13.7 适配器

适配器
容器适配器
迭代器适配器
函数适配器
stack - dequeue
queue - dequeue
priority_queue - vector
反向迭代器
插入迭代器
Negator
Binder
成员函数适配器

函数适配器的接口更简单,只是受限比一般容器要多。
迭代器适配器拓展了迭代器的功能。
函数适配器通过转换或者修改其他函数对象使得其功能得到扩展。

13.7.1 容器适配器

在创建适配器时,通过将一个顺序容器指定为适配器的第2个类型参数,可覆盖其关联的基础容器类型。
对于给定的适配器,其关联的容器必须满足一定的约束条件

容器适配器类型关联适配器要求支持操作
stack任意一种(均提供push_back、pop_back、back)压元素入栈(push)、弹元素出栈(pop)、查看栈顶(top)、查看元素数目(size)和测试栈是否为空(empty)
queue提供pop_front(不能建立在vector容器中)队尾压入元素(push)、队首弹出元素(pop)、参看队首(front)和队尾(back)、查看元素数目(size)和测试队列是否为空(empty)[queue头函数]
priority_queue不能建立在List相对于queue增加了top操作[queue头函数]
13.7.2 迭代器适配器

包含在iterator

  1. 反向迭代器
  2. 插入迭代器适配器
插入适配器
back_insert_iterator
front_insert_iterator
insert_iterator

back_insert_iterator(obv);
front_insert_iterator(obv);//不能基于vector创建front_insert_iterator对象
insert_iterator(obv,p);//将元素插入到其迭代器参数p指定位置的前面。
通过插入迭代器适配器可以将复制算法转化成插入算法

13.7.3 函数适配器
  1. Binder
    bind1st(函数对象,指定值);//将指定值绑定在函数对象的第一个参数上。
    bind2nd(函数对象,指定值);//将指定值绑定在函数对象的第二个参数上。
  2. 否定器
    逆转函数对象的真伪值,要求函数对象是断言的
    not1:用于逆转一元断言
    not2:用于逆转二元断言
  3. 成员函数适配器
    容器存储元素是某个类的对象,使用成员函数适配器来调用对象的函数。
    men_fun_ref:容器存放的是对象实体。
    men_fun:容器存放的是对象指针。

  1. cs106L ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值