模板与STL小结--vector、list、map等各类容器

一、什么是STL

  STL是standard template library,标准模版库
  是HP实验室开发的一系列软件的统称,从根本上来说, 它是一些容器和算法的集合,它是世界上很多聪明的程序员多年的杰作
  STL是标准化的组件,不用重新开发,可以直接使用,它是C++的一部分,不需要额外安装

二、STL中有什么

  • 1、容器
    存储类对象的盒子
    线性容器:vector、list
    容器适配器:queue、stack、double queue
    关系型容器:set、map
  • 2、算法
    find sort
    #include <iostream>
    #include <string.h>
    #include <vector>
    #include <algorithm>
    using namespace std; 
    class Student 
    { 
    char name[20]; 
    char sex; 
    public: 
    short age; 
    Student(const char* name="haha",const char sex='f',short age =1):sex(sex),age(age) 
    { 
    strcpy(this->name,name); 
    } 
    friend ostream& operator << (ostream& os,Student& stu) 
    { 
    os << stu.name << " " << stu.sex << " " << stu.age; 
    return os; 
    } 
    bool operator < (const Student& stu)const 
    { 
    return age > stu.age; 
    } 
    bool operator == (const Student& stu)const 
    { 
    return age == stu.age && sex == stu.sex && 0 == strcmp(name,stu.name); 
    } 
    }; 
    bool stu_cmp(const Student& stu1,const Student& stu2) 
    { 
    return stu1.age > stu2.age; 
    } 
    int main() 
    { 
    /int arr[10] = {3,2,8,6,1,9,4,0,5,7}; 
    sort(arr,arr+10); 
    for(int i=0; i<10; i++) 
    { 
    cout << arr[i] << " "; 
    }/ 
    vector arr(10); 
    for(int i=0; i<10; i++) 
    { 
    arr[i].age += i; 
    cout << arr[i] << endl; 
    } 
    /sort(arr.begin(),arr.end(),stu_cmp); 
    for(int i=0; i<10; i++) 
    { 
    cout << arr[i] << endl; 
    }/ 
    Student stu("haha",'f',10); 
    vector::iterator it; 
    it = find(arr.begin(),arr.end(),stu); 
    cout << *it << " " << stu << endl; 
    }
  • 3、迭代器
    迭代器是一个类,它实现于容器模版中
    它的对象是一个指向容器中的一个元素,它实现的*运算符,给人的感觉它好像是个指针
    从容器中获取到迭代器是一个半开半闭区间,[start,end)

三、vector容器

  • 1、特点:

    • a、占用的是连续的内存
    • b、动态的管理内存
    • c、支持随即访问(at,[])
    • d、支持按迭代器进行插入和删除(insert,erase)
      但只有在末尾添加和删除时效率才最高
    • e、支持随即迭代:it=it+4;
  • 2、定义
    vector<类型> a; //创建容器
    vector<类型> a(10);//创建容器并设置容量为10,把元素初始化为0
    vector<类型> a(10,1);//创建容器并设置容量为10,并设置初始值

  • 3、返回值
    v[i]、v.at(i)、v.front()、v.back()这种方式返回的是元素的引用

  • 4、成员函数
    void assign( input_iterator start, input_iterator end);
    void assign( size_type num, const TYPE &val);

    size_type capacity(); //获取容器当前的容量

    iterator erase( iterator loc);
    iterator erase( iterator start, iterator end);
    iterator insert( iterator loc, const TYPE &val);
    void insert( iterator loc, size_type num, const TYPE &val);
    void insert( iterator loc, input_iterator start, input_iterator end);
    //元素被删除或插入之后,之前获取的迭代器就失效,需要重新获取

    void resize( size_type size, TYPE val );
    //改容器的大小,可以调大(构造),可以调小(析构)

  • 5、运算符
    ==、!=、>=、<=、>、<
    比较两个容器中元素的shul、顺序、值是否相等
    容器中存储的对象的==运算符必须要重载

  • 6、排序、查找
    在vector容器中是没有排序和查找成员函数
    在List容器中由于这是链式的存储结构所以不能使用全局的sort函数,必须自己实现
    在使用sort排序时,待排序的对象必须实现出 < 的重载(或者给sort提供比较函数)
    在使用find时,待查找的对象必须实现出 == 的重载

  • 7、自定义类使用容器时需要实现的成员有
    无参构造、拷贝构造
    == <

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class A 
    { 
    int num; 
    public: 
    A(int num=0):num(num) 
    { 
    cout << "我是对象A" << endl; 
    } 
    A(const A& a) 
    { 
    num = a.num; 
    cout << "我是拷贝构造" << endl; 
    } 
    friend ostream& operator << (ostream& os,A& a); 
    }; 
    ostream& operator << (ostream& os,A& a) 
    { 
    return os << a.num; 
    }
    
    int main() 
    { 
    vector a(10); 
    a[1] = 20; 
    a.back() = 10; 
    for(int i=0; i<10; i++) 
    { 
    cout << a[i] << endl; 
    }
    
    vector< A> b(10); 
    b.assign(a.begin(),a.end()-5); 
    for(int i=0; i<10; i++) 
    { 
    cout << b[i] << endl; 
    } 
    }

四、set容器

  集合容器,里面的元素不会重复,它会自动排重。
  使用时要实现它的==运算符。

  multiset 允许有重复的数据。

  pair equal_range( const key_type &key );
  查找值等于key的元素信息,返回两人个迭代器。

  iterator lower_bound( const key_type &key );
  查找大于等于key的第一个元素

  iterator upper_bound( const key_type &key );
  查找大于key的第一个元素

五、队列

  单向队列:
  back、empty、front、pop、push、size
  双向队列:

六、List

  是一种链式存储结构,不能使用算法库中的排序,只能调用自带的排序函数。
  void unique();
  void unique( BinPred pr );
  删除重复的元素。

  void splice( iterator pos, list &lst );
  void splice( iterator pos, list &lst, iterator del );
  void splice( iterator pos, list &lst, iterator start, iterator end );
  从指定的位置开始合并两个链表

  void merge( list &lst );
  void merge( list &lst, Comp compfunction );
  直接合并两人个链表

七、map是一种关联式的容器

  它底层以采用的是红黑树(有序+平衡)进行存储的。
  一个键值(主键)只能对应一个值。 

#include <iostream>
#include <map>
using namespace std; 
int main() 
{ 
map m; 
m.insert(make_pair("a1","hehe")); 
m.insert(make_pair("a2","haha")); 
m.insert(make_pair("a3","xixi")); 
m.insert(make_pair("a4","hehe")); 
cout << m.size() << endl;

map::iterator it = m.find("a2"); 
cout << it->first <<" "<< it->second << endl; 
} 

  multimap 多重映射
  键值可以重复

#include <iostream>
#include <map>
using namespace std; 
int main() 
{ 
multimap m; 
m.insert(make_pair("a1","hehe")); 
m.insert(make_pair("a1","haha")); 
m.insert(make_pair("a2","xixi")); 
m.insert(make_pair("a2","hehe")); 
cout << m.size() << endl;

multimap::iterator it = m.find("a2"); 
while(it != m.end()) 
{ 
cout << it->first <<" "<< it->second << endl; 
it++; 
} 
}

八、priority_queue优先队列

元素在入队后就已经排序好了,最大值在上面。
对元素排序依靠的是 < 运算符。

原文地址:https://www.cnblogs.com/qsz805611492/p/9530129.html