c++ List、Vector、Stack、Queue使用

摘要:
1、 List使用介绍头文件#include<List>List的基本函数List,按顺序存储链接列表中的元素。与向量相比,它允许快速插入和删除,但随机访问很慢Assign()将值分配给列表back()返回最后一个元素begin()返回指向第一个元素的迭代器clear()删除所有元素empty()如果列表为空则返回true()返回迭代器erase()在结尾删除一个

一、List使用

引入头文件#include <list>

List基本函数
Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.
assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素

举例说明:

#include <iostream> 
#include <list> 

using namespace std; 
typedef list<int> INTLIST; 

//从前向后显示list队列的全部元素 
void put_list(INTLIST list, string name) 
{ 
    INTLIST::iterator plist; 
    
    cout << "The contents of " << name << " : "; 
    for(plist = list.begin(); plist != list.end(); plist++) 
        cout << *plist << " "; 
    cout<<endl; 
} 

//测试list容器的功能 
int main() 
{ 
    //list1对象初始为空 
    INTLIST list1; 
    //list2对象最初有10个值为6的元素 
    INTLIST list2(10,6); 
    //list3对象最初有9个值为6的元素 
    INTLIST list3(list2.begin(),--list2.end()); 
    
    //声明一个名为i的双向迭代器 
    INTLIST::iterator i; 
    
    //从前向后显示各list对象的元素 
    put_list(list1,"list1"); 
    put_list(list2,"list2"); 
    put_list(list3,"list3"); 
    
    //从list1序列后面添加两个元素 
    list1.push_back(2); 
    list1.push_back(4); 
    cout<<"list1.push_back(2) and list1.push_back(4):"<<endl; 
    put_list(list1,"list1"); 
    
    //从list1序列前面添加两个元素 
    list1.push_front(5); 
    list1.push_front(7); 
    cout<<"list1.push_front(5) and list1.push_front(7):"<<endl; 
    put_list(list1,"list1"); 
    
    //在list1序列中间插入数据 插入3个9
    list1.insert(++list1.begin(),3,9); 
    cout<<"list1.insert(list1.begin()+1,3,9):"<<endl; 
    put_list(list1,"list1"); 
    
    //测试引用类函数 输出第一个元素和最后一个元素
    cout<<"list1.front()="<<list1.front()<<endl; 
    cout<<"list1.back()="<<list1.back()<<endl; 
    
    //从list1序列的前后各移去一个元素 删除元素
    list1.pop_front(); 
    list1.pop_back(); 
    cout<<"list1.pop_front() and list1.pop_back():"<<endl; 
    put_list(list1,"list1"); 
    
    //清除list1中的第2个元素 
    list1.erase(++list1.begin()); 
    cout<<"list1.erase(++list1.begin()):"<<endl; 
    put_list(list1,"list1"); 
    
    //对list2赋值并显示 
    list2.assign(8,1); 
    cout<<"list2.assign(8,1):"<<endl; 
    put_list(list2,"list2"); 
    
    //显示序列的状态信息 
    cout<<"list1.max_size(): "<<list1.max_size()<<endl; 
    cout<<"list1.size(): "<<list1.size()<<endl; 
    cout<<"list1.empty(): "<<list1.empty()<<endl; 
    
    //list序列容器的运算 
    put_list(list1,"list1"); 
    put_list(list3,"list3"); 
    cout<<"list1>list3: "<<(list1>list3)<<endl; 
    cout<<"list1<list3: "<<(list1<list3)<<endl; 
    
    //对list1容器排序 
    list1.sort(); 
    put_list(list1,"list1"); 
    
    //结合处理 合并list
    list1.splice(++list1.begin(), list3); 
    put_list(list1,"list1"); 
    put_list(list3,"list3"); 
    return 0;
} 

  输出结果如下:

The contents of list1 :
The contents of list2 : 6 6 6 6 6 6 6 6 6 6
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1.push_back(2) and list1.push_back(4):
The contents of list1 : 2 4
list1.push_front(5) and list1.push_front(7):
The contents of list1 : 7 5 2 4
list1.insert(list1.begin()+1,3,9):
The contents of list1 : 7 9 9 9 5 2 4
list1.front()=7
list1.back()=4
list1.pop_front() and list1.pop_back():
The contents of list1 : 9 9 9 5 2
list1.erase(++list1.begin()):
The contents of list1 : 9 9 5 2
list2.assign(8,1):
The contents of list2 : 1 1 1 1 1 1 1 1
list1.max_size(): 1073741823
list1.size(): 4
list1.empty(): 0
The contents of list1 : 9 9 5 2
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1>list3: 1
list1<list3: 0
The contents of list1 : 2 5 9 9
The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9
The contents of list3 :

二、Vector使用

在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结。

1 基本操作

(1)头文件#include<vector>.

(2)创建vector对象,vector<int> vec;

(3)尾部插入数字:vec.push_back(a);

(4)使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的。

(5)使用迭代器访问元素.

vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
    cout<<*it<<endl;

(6)插入元素:    vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;

(7)删除元素:    vec.erase(vec.begin()+2);删除第3个元素

vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始

(8)向量大小:vec.size();

(9)清空:vec.clear();

2、结构体作为vector元素

vector的元素不仅仅可以使int,double,string,还可以是结构体,但是要注意:结构体要定义为全局的,否则会出错。下面是一段简短的程序代码:

复制代码
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

typedef struct rect
{
    int id;
    int length;
    int width;

  //对于向量元素是结构体的,可在结构体内部定义比较函数,下面按照id,length,width升序排序。
  bool operator< (const rect &a)  const
    {
        if(id!=a.id)
            return id<a.id;
        else
        {
            if(length!=a.length)
                return length<a.length;
            else
                return width<a.width;
        }
    } }Rect; int main() { vector<Rect> vec; Rect rect; rect.id=1; rect.length=2; rect.width=3; vec.push_back(rect); vector<Rect>::iterator it=vec.begin(); cout<<(*it).id<<' '<<(*it).length<<' '<<(*it).width<<endl; return 0; }
复制代码

 3  算法

(1) 使用reverse将元素翻转:需要头文件#include<algorithm>

reverse(vec.begin(),vec.end());将元素翻转(在vector中,如果一个函数中需要两个迭代器,

一般后一个都不包含.)

(2)使用sort排序:需要头文件#include<algorithm>,

sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).

可以通过重写排序比较函数按照降序比较,如下:

定义排序比较函数:

bool Comp(const int &a,const int &b)
{
    return a>b;
}
调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

4、lower_bound()与up_bound()使用

// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20

  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 10); //          ^
  up= std::upper_bound (v.begin(), v.end(), 10); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '
';
  std::cout << "upper_bound at position " << (up - v.begin()) << '
';
  std::cout << *low<<" "<<*up;
  return 0;
}

三、vector与list区别

c++标准库中,容器vector和list都可以用来存放一组类型相同的数据。而且二者不同于数组的一点是,支持动态增长。但它们还是有有几点不同

(1)  vector是顺序表,表示的是一块连续的内存,元素被顺序存储;list是双向连接表,在内存中不一定连续。

(2)当数值内存不够时,vector会重新申请一块足够大的连续内存,把原来的数据拷贝到新的内存里面;list因为不用考虑内存的连续,因此新增开销比vector小。

(3)list只能通过指针访问元素,随机访问元素的效率特别低,在需要频繁随机存取元素时,使用vector更加合适。

(4)当向vector插入或者删除一个元素时,需要复制移动待插入元素右边的所有元素;因此在有频繁插入删除操作时,使用list更加合适。

四、stack使用

引入头文件
#include<stack>
stack<int> s
s.push()
s.pop()
s.top()
s.empty()
s.size()

五、Queue的使用

引入头文件

#include<queue>
定义queue 对象的示例代码如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()

免责声明:文章转载自《c++ List、Vector、Stack、Queue使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue2.0模拟锚点实现定位平滑滚动前端开发:这10个Chrome扩展你不得不知下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vector的用法

vector容器是笔试时最经常用到的容器,它实际是一个类模板,它所具有一些成员函数我们必须熟练使用,这样才会加快编程速度。 首先加头文件并定义: #include<vector> vector<int>vec; 1、获取vector里的元素个数 int len =vec.size(); 2、向vector添加元素 int temp;...

C++ 迭代器(STL迭代器)iterator详解

要访问顺序容器和关联容器中的元素,需要通过“迭代器(iterator)”进行,迭代器是一个变量,相当于容器和操作容器的算法之间的中介。迭代器可以指向容器中的某个元素,通过迭代器就可以读写它指向的元素。从这一点上看,迭代器和指针类型。 迭代器按照定义方式分为以下四种: 正向迭代器,定义方式: 容器类名::iterator 迭代器名; 常量正向迭代器,定义方式...

[C++ STL] vector使用详解

一、概述 vector(向量): 是一种序列式容器,事实上和数组差不多,但它比数组更优越。一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界。而vector正好弥补了这个缺陷,它的特征是相当于可分配拓展的数组(动态数组),它的随机访问快,在中间插入和删除慢,但在末端插入和删除快。 二、定义及初始化 使用之前必须加相应容器的头文件: #...

‎Cocos2d-x 学习笔记(15.1) EventDispatcher

EventDispatcher对监听器进行管理,围绕着监听器工作。可以添加、删除、暂停/恢复监听器、分发事件到监听器。 1. 一些成员 /** 把ListenerID和同ID监听器的容器对应 */ std::unordered_map<EventListener::ListenerID, EventListenerVector*>...

C++ Primer学习笔记

目录 12.1 动态内存与智能指针 12.1.1 shared_ptr类 12.1.2 直接管理内存 12.1.3 shared_ptr和new结合使用 12.1.4 智能指针和异常 12.1.5 unique_ptr 12.1.6 weak_ptr 12.2 动态数组 12.2.1 new和数组 12.2.2 alllocator类...

如何对list进行洗牌操作

标准库函数 std::random_shuffle 提供了一个洗牌功能,但是参数只能是随机迭代器。vector可以,但list不行。 那么如何对list进行洗牌呢?一个比较简单的方法就是先从list构造一个临时的vector,对此临时的vector进行洗牌,然后再把洗牌后的vector复制回原来的list中。 代码如下: template<typen...