STL之顺序容器

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: 顺序容器:   vector:数组   list:链表   deque:双端数组 顺序容器适配器:   stack:堆栈   queue:队列   priority_queue:优先级队列   deque是一个动态数组  deque与vector非常类似;  deque可以在在数组开...

顺序容器:

  vector:数组

  list:链表

  deque:双端数组

顺序容器适配器:

  stack:堆栈

  queue:队列

  priority_queue:优先级队列

  deque是一个动态数组
  deque与vector非常类似;
  deque可以在在数组开头和末尾插入和删除数据;

 1 #include <deque>
 2 #include <algorithm>
 3 
 4 deque<int>::iterator iElemetnLocater;
 5 for(iElementLocater a.begin();
 6     iElementLocater != a.end();
 7     ++iElemetLocater){
 8 //distance函数
 9     size_t nOffert = distance(a.begin(), iElementLocater);
10     cout << "a["<<nOffset<<"]" <<*iElementLocater << endl;
11 
12 }

  list 类模板:

  vector向量只能在末尾插入数据;
  deque可以在开头和末尾拆入;

  顺序容器 STL list类(双向链表)

    list是一个模板类;
    在list开头插入元素;
    在list末尾插入元素;
    在list中间插入元素;  
    删除list中的元素;
    对list中元素进行反转和排序;

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8         std::list<int> a;
 9         std::list<list>::iterator iter;        
10 
11         a.push_front(4);   //头插
12         a.push_front(3);
13         a.push_front(2);
14         a.push_front(1);
15         a.push_back(5);   //尾插
16         
17         iter = a.begin();
18         ++iter;                 
19         a.insert(iter, 10);     //通过迭代器位置的改变来插入数据;
20         //在末尾拆入4个20
21         a.insert(a.end(), 4, 20)        
22 
23 
24         for(std::list<int>::iterator it=a.begin();
25             it!=a.end();it++){
26             std::cout << *it << std::endl;
27         }     
28         return 0;
29 }
30  
 1 #include <vector>
 2 #include <list>
 3 #include <deque>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     vector<string>  s_vec;
10     s_vec.push_back("hello");
11     s_vec.push_back("c++");
12     s_vec.push_back("STL");
13 
14     //利用迭代器,vector初始化list
15     list<string>  slist(s_vec.begin(), s_vec.end());
16     
17     const list<int>::size_type  list_size =64;
18     list<string> slist(list_size, "hello";  
19 
20     list<int>       I_list;
21     
22     vector<int> ivec;   //默认构造函数
23     ivec.push_back(1);
24     ivec.push_back(2);
25     ivec.push_back(3);
26     
27     vector<int> ivec2(ivec);    //使用ivec初始化ivec2;
28     vector<int> ivec2(ivec);    //使用ivec初始化ivec2;
29 
30     char *words[] = {"stately", "plump", "buck"};
31     size_t words_size = sizeof(words)/sizeof(char*);
32 
33     //利用指针 数组初始化list容器
34     list<string> words(words, words+words_size):
35 
36     const list<int>::size_type list_size = 64;
37     list<string> slist(list_size, "hello");  //list里面64个hello;
38     list<int> ilist(list_size);     //ilist默认64个0;
39 
40     vector<Foo> a;      //不进行初始化,不用调用Foo的构造函数;
41     vector<Foo> b(10, 1);   //进行了初始化,而且调用的是带参数的构造函数;
42 
43     vector<vector<string> > lines;  //vector里面存放vector
44     list<vector<string> > list_lines;   //list里面存放vector
45 
46     //list<int>  ilist(ivec);       //Error;
47     return 0;
48 }

 

  

相关文章
|
3月前
|
存储 算法 编译器
8.STL中Vector容器的常见操作(附习题)
8.STL中Vector容器的常见操作(附习题)
|
1月前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
3月前
|
设计模式 存储 C++
【C++/STL】:stack/queue的使用及底层剖析&&双端队列&&容器适配器
【C++/STL】:stack/queue的使用及底层剖析&&双端队列&&容器适配器
54 2
|
3月前
|
C++ 容器
C++ STL:各类容器的特点和优缺点比较
C++ STL:各类容器的特点、优势、劣势比较
|
3月前
|
编译器 C++ 容器
【C++/STL】:list容器的深度剖析及模拟实现
【C++/STL】:list容器的深度剖析及模拟实现
31 2
|
3月前
|
算法 前端开发 Linux
【常用技巧】C++ STL容器操作:6种常用场景算法
STL在Linux C++中使用的非常普遍,掌握并合适的使用各种容器至关重要!
68 10
|
3月前
|
存储 C++ 容器
【C++/STL】:list容器的基本使用
【C++/STL】:list容器的基本使用
26 1
|
3月前
|
存储 算法 C++
【C++/STL】:vector容器的基本使用
【C++/STL】:vector容器的基本使用
28 1
|
2月前
|
存储 算法 C语言
【C++】详解STL的适配器容器之一:优先级队列 priority_queue
【C++】详解STL的适配器容器之一:优先级队列 priority_queue
|
2月前
|
设计模式 存储 缓存
【C++】详解STL容器之一的deque和适配器stack,queue
【C++】详解STL容器之一的deque和适配器stack,queue