MFC ,List使用

出自http://www.cnblogs.com/yuehui/archive/2012/06/15/2550449.html

 List容器
双向线性表list容器
   list类定义了双向的线性表。Vector类支持随机访问,但list只能支持顺序访问,由于list是双向的,因此我们可以按从前到后或者从后到前的顺序来访问list。
1.创建一个list对象
#include<list>
using namespace std;
     
list<int> lst1;
list<char> lst2;
2.向list对象中添加数值
从前面添加:
         lst1.push_front(0);
         lst1.push_front(1);
从后面添加:
   lst1.push_back(0);
         lst1.push_back(1);
3.删除操作
删除list对象中的第一个元素
  lst1.pop_front();
删除list对象中的最后一个元素
  lst1.pop_back();
4.获得list对象的存储容量
 lst1.size();
5.获得list对象中第一个和最后一个元素
 list<int>::iterator p = lst1.begin();
        list<int>::iterator p = lst1.end();
6.在list对象中插入元素
        list<int>::iterator p = lst1.begin();
        p++;
        lst1.insert(p, 2 , 100);
7.在list对象中删除元素;
 list<int>::iterator p1 = lst1.begin();
        list<int>::iterator p2 = lst1.begin();
        for (i=0;i<5;i++) p2++;
        lst1.erase(p1, p2);
8.访问list对象中的内容
 list<int>::iterator p = lst1.begin();
        while (p!=lst1.end())
 {
        *p = *p + 100;
         p++;
        }
9.将list对象中的内容排序;
 lst1.sort();

 

 

 

list应用问题

要求使用list解题
    输入:第一个行为总组数,从第二行开始为每组数,每组数的第一个数为该组数的个数。
    输出:将所有组的数排序无重复输出。
    输入
    2
    3
    3
    4
    5
    5
    1
    2
    3
    4
    5
    输出
    1 
    2
    3
    4
    5

 

 

程序源码:

#include<iostream>
#include<list>
using namespace std;

int main()
{
 //定义一个整型的list对象lst
 list<int> lst;
 int i;
 
 //往lst头和尾插入整数
 lst.push_front(10);
 lst.push_front(20);
 lst.push_front(30);
    lst.push_back(1);
 lst.push_back(2);
 lst.push_back(3);

    for (i=0; i<=10;i++)  lst.push_back(i);

 //输出lst中的所有元素
 //定义一个迭代器p
 list<int>::iterator p = lst.begin();
 while (p!=lst.end())
 {
  cout << *p << "  ";
  p++;
 }
 cout << endl;
 
 //将lst中的元素排序
 lst.sort();

 p = lst.begin();
 while (p!=lst.end())
 {
  cout << *p << "  ";
  p++;
 }
 cout << endl;

 //删除lst中的重复元素
 lst.unique();
 
 p = lst.begin();
 while (p!=lst.end())
 {
  cout << *p << "  ";
  p++;
 }
 cout << endl;

 return 0;

}

posted @ 2015-03-04 11:00  chen_sg  阅读(346)  评论(0编辑  收藏  举报