VC编程之VC++中list::list的使用方法总结
小标 2018-08-14 来源 : 阅读 1492 评论 0

摘要:本文主要向大家介绍了VC编程之VC++中list::list的使用方法总结,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

本文主要向大家介绍了VC编程之VC++中list::list的使用方法总结,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

VC编程之VC++中list::list的使用方法总结

这几天在做图像处理方面的研究,其中有一部分是关于图像分割方面的,图像目标在分割出来之后要做进一步的处理,因此有必要将目标图像的信息保存在一个变量里面,一开始想到的是数组,但是马上就发现使用数组的缺点:数组长度固定,动态分配内存很容易导致错误发生。最重要的一点是我要保存目标图像的每一点的坐标值,使用数组就有点无能为力了。因此到百度、Google大神上面找思路,终于被我发现在c++的标准库里面还有这么一个模板类:list,下面就是对找到的资料的汇总和加工。


vc6自带的msdn帮助文档的解释
以下是引自msdn帮助文档(中文是我自己翻译的,错误之处请包涵。):


     The template class describes an object that controls a varying-length sequence of elements of type T. The sequence is stored as a bidirectional linked list of elements, each containing a member of type T.


    本模板类描述了一个对象,这个对象是类型为T的可变长度的序列元素。这个序列采用双向链表的方式存储每一个元素,其中每一个元素的数据流行都是T。


     The object allocates and frees storage for the sequence it controls through a protected object named allocator, of class A. Such an allocator object must have the same external interface as an object of template class allocator. Note that allocatoris not copied when the object is assigned.


     对序列对象的分配和释放操作通过一个受保护的对象allocator进行。这样一个allocator对象必须有相同的外部接口作为一个模板类分配器的对象。注意:当对象被分配之后allocator不能被复制。


    List reallocation occurs when a member function must insert or erase elements of the controlled sequence. In all such cases, only iterators or references that point at erased portions of the controlled sequence become invalid.


    当一个成员要进行insert或者erase操作时,列表的重新分配操作发生。在这种情况下,只有迭代器或者引用所指向的要删除的对象的指针变为无效。


msdn帮助文档自带的例子
下面为msdn帮助文档中自带的一个例子,该例展示了如何使用迭代器读取列表中的元素和进行插入操作。


#include <list>
#include <iostream>
using namespace std ;
typedef list<int> LISTINT;
void main()
{
    int rgTest1[] = {5,6,7};
    int rgTest2[] = {10,11,12};
    LISTINT listInt;
    LISTINT listAnother;
    LISTINT::iterator i;
    // Insert one at a time
    listInt.insert (listInt.begin(), 2);
    listInt.insert (listInt.begin(), 1);
    listInt.insert (listInt.end(), 3);
    // 1 2 3
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;
    // Insert 3 fours
    listInt.insert (listInt.end(), 3, 4);
    // 1 2 3 4 4 4
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;
    // Insert an array in there
    listInt.insert (listInt.end(), rgTest1, rgTest1 + 3);
    // 1 2 3 4 4 4 5 6 7
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;
    // Insert another LISTINT
    listAnother.insert (listAnother.begin(), rgTest2, rgTest2+3);
    listInt.insert (listInt.end(), listAnother.begin(), listAnother.end());
    // 1 2 3 4 4 4 5 6 7 10 11 12
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;
}
 
Program Output is:
1 2 3
1 2 3 4 4 4
1 2 3 4 4 4 5 6 7
1 2 3 4 4 4 5 6 7 10 11 12
list::list模板类的主要函数介绍
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_if() //按指定条件删除元素
remove() //从list删除元素
rend() //指向list末尾的逆向迭代器
resize() //改变list的大小
reverse() //把list的元素倒转
size() //返回list中的元素个数
sort() //给list排序
splice() //合并两个list 
swap() //交换两个list 
unique() //删除list中重复的元素
常用的操作主要是有插入操作、删除操作。list为实现头尾高效的插入和删除操作而提供了大多数的支持函数,而对于随机访问函数,则只能从头部或者尾部进行遍历操作。


关于remove和erase函数
上面的介绍中关于插入等等操作都有帮助的例子,但是对于删除函数,这个需要有一些注意的地方。下面请看例子:


#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std;
//创建一个list容器的实例LISTINT
typedef list<int> TESTINT;
void main()
{
  //使用TESTINT创建一个list类型的对象
  TESTINT test;
  //使用TESTINT创建一个迭代器对象
  TESTINT::iterator i; 
   //从前面向listOne容器中添加数据
  test.push_front (2);
  test.push_front (1);
  //从后面向listOne容器中添加数据
  test.push_back (3);
  test.push_back (4);
  //显示删除前的数据
  cout<<"before delete"<<endl;
  for (i = test.begin(); i != test.end(); ++i)
 cout << *i << " ";
  cout<<endl;
  //从列表中删除一个元素
  i = test.begin();
  while(i != test.end())
  {
    int tmp = *i;
    if(tmp == 2){
test.erase(i++);//在这里要是指针前进1个,否则迭代器失效
    }else{
i++;
    }  
  }
  //显示删除后的数据
  cout<<"after delete"<<endl;
  for (i = test.begin(); i != test.end(); ++i)
        cout << *i << " ";
  cout<<endl;



总结
      在使用list的时候不能使用随机访问的方式,只能按照迭代的方式进行访问,这样的话在进行删除操作的时候就可能会出现某一次删除操作之后指针没有指向下一个有效的元素而导致迭代器失效。因此,在进行删除操作时候最好使用while的方式,使用for循环如果控制不好,可能会导致迭代器失效。

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言VC/MFC频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved