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

一、List使用

引入头文件#include

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.

(2)创建vector对象,vector 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

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

一般后一个都不包含.)

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

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()) << 'n';
  std::cout << "upper_bound at position " << (up - v.begin()) << 'n';
  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 s

s.push()

s.pop()

s.top()

s.empty()

s.size()

五、Queue的使用

引入头文件

include

定义queue 对象的示例代码如下:

queue q1;

queue q2;

queue 的基本操作有:

入队,如例:q.push(x); 将x 接到队列的末端。

出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值

访问队首元素,如例:q.front(),即最早被压入队列的元素。

访问队尾元素,如例:q.back(),即最后被压入队列的元素。

判断队列空,如例:q.empty(),当队列空时,返回true。

访问队列中的元素个数,如例:q.size()
原文链接: https://www.cnblogs.com/likailiche/p/4897145.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/223439

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午12:04
下一篇 2023年2月13日 下午12:04

相关推荐