• C++ List的用法


      1 Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.
      2 
      3 assign() 给list赋值 
      4 back() 返回最后一个元素 
      5 begin() 返回指向第一个元素的迭代器 
      6 clear() 删除所有元素 
      7 empty() 如果list是空的则返回true 
      8 end() 返回末尾的迭代器 
      9 erase() 删除一个元素 
     10 front() 返回第一个元素 
     11 get_allocator() 返回list的配置器 
     12 insert() 插入一个元素到list中 
     13 max_size() 返回list能容纳的最大元素数量 
     14 merge() 合并两个list 
     15 pop_back() 删除最后一个元素 
     16 pop_front() 删除第一个元素 
     17 push_back() 在list的末尾添加一个元素 
     18 push_front() 在list的头部添加一个元素 
     19 rbegin() 返回指向第一个元素的逆向迭代器 
     20 remove() 从list删除元素 
     21 remove_if() 按指定条件删除元素 
     22 rend() 指向list末尾的逆向迭代器 
     23 resize() 改变list的大小 
     24 reverse() 把list的元素倒转 
     25 size() 返回list中的元素个数 
     26 sort() 给list排序 
     27 splice() 合并两个list 
     28 swap() 交换两个list 
     29 unique() 删除list中重复的元素
     30 
     31 实例一:
     32 [cpp] view plain copy
     33 #include <iostream>   
     34 #include <list>   
     35 #include <numeric>   
     36 #include <algorithm>   
     37 using namespace std;   
     38 
     39 //创建一个list容器的实例LISTINT   
     40 typedef list<int> LISTINT;   
     41 //创建一个list容器的实例LISTCHAR   
     42 typedef list<int> LISTCHAR;   
     43 
     44 void main()   
     45 {   
     46     //用list容器处理整型数据    
     47     //用LISTINT创建一个名为listOne的list对象   
     48     LISTINT listOne;   
     49     //声明i为迭代器   
     50     LISTINT::iterator i;   
     51 
     52     //从前面向listOne容器中添加数据   
     53     listOne.push_front (2);   
     54     listOne.push_front (1);   
     55 
     56     //从后面向listOne容器中添加数据   
     57     listOne.push_back (3);   
     58     listOne.push_back (4);   
     59 
     60     //从前向后显示listOne中的数据   
     61     cout<<"listOne.begin()--- listOne.end():"<<endl;   
     62     for (i = listOne.begin(); i != listOne.end(); ++i)   
     63         cout << *i << " ";   
     64     cout << endl;   
     65 
     66     //从后向后显示listOne中的数据   
     67     LISTINT::reverse_iterator ir;   
     68     cout<<"listOne.rbegin()---listOne.rend():"<<endl;   
     69     for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {   
     70         cout << *ir << " ";   
     71     }   
     72     cout << endl;   
     73 
     74     //使用STL的accumulate(累加)算法   
     75     int result = accumulate(listOne.begin(), listOne.end(),0);   
     76     cout<<"Sum="<<result<<endl;   
     77     cout<<"------------------"<<endl;   
     78 
     79     //--------------------------   
     80     //用list容器处理字符型数据   
     81     //--------------------------   
     82 
     83     //用LISTCHAR创建一个名为listOne的list对象   
     84     LISTCHAR listTwo;   
     85     //声明i为迭代器   
     86     LISTCHAR::iterator j;   
     87 
     88     //从前面向listTwo容器中添加数据   
     89     listTwo.push_front ('A');   
     90     listTwo.push_front ('B');   
     91 
     92     //从后面向listTwo容器中添加数据   
     93     listTwo.push_back ('x');   
     94     listTwo.push_back ('y');   
     95 
     96     //从前向后显示listTwo中的数据   
     97     cout<<"listTwo.begin()---listTwo.end():"<<endl;   
     98     for (j = listTwo.begin(); j != listTwo.end(); ++j)   
     99         cout << char(*j) << " ";   
    100     cout << endl;   
    101 
    102     //使用STL的max_element算法求listTwo中的最大元素并显示   
    103     j=max_element(listTwo.begin(),listTwo.end());   
    104     cout << "The maximum element in listTwo is: "<<char(*j)<<endl;   
    105 }   
    106 
    107 结果:
    108 listOne.begin()--- listOne.end():
    109 1 2 3 4
    110 listOne.rbegin()---listOne.rend():
    111 4 3 2 1
    112 Sum=10
    113 ------------------
    114 listTwo.begin()---listTwo.end():
    115 B A x y
    116 The maximum element in listTwo is: y
    117 Press any key to continue
    118 
    119 实例二:
    120 [cpp] view plain copy
    121 #include <iostream>   
    122 #include <list>   
    123 
    124 using namespace std;   
    125 typedef list<int> INTLIST;   
    126 
    127 //从前向后显示list队列的全部元素   
    128 void put_list(INTLIST list, char *name)   
    129 {   
    130     INTLIST::iterator plist;   
    131 
    132     cout << "The contents of " << name << " : ";   
    133     for(plist = list.begin(); plist != list.end(); plist++)   
    134         cout << *plist << " ";   
    135     cout<<endl;   
    136 }   
    137 
    138 //测试list容器的功能   
    139 void main(void)   
    140 {   
    141     //list1对象初始为空   
    142     INTLIST list1;   
    143     //list2对象最初有10个值为6的元素   
    144     INTLIST list2(10,6);   
    145     //list3对象最初有3个值为6的元素   
    146     INTLIST list3(list2.begin(),--list2.end());   
    147 
    148     //声明一个名为i的双向迭代器   
    149     INTLIST::iterator i;   
    150 
    151     //从前向后显示各list对象的元素   
    152     put_list(list1,"list1");   
    153     put_list(list2,"list2");   
    154     put_list(list3,"list3");   
    155 
    156     //从list1序列后面添加两个元素   
    157     list1.push_back(2);   
    158     list1.push_back(4);   
    159     cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;   
    160     put_list(list1,"list1");   
    161 
    162     //从list1序列前面添加两个元素   
    163     list1.push_front(5);   
    164     list1.push_front(7);   
    165     cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;   
    166     put_list(list1,"list1");   
    167 
    168     //在list1序列中间插入数据   
    169     list1.insert(++list1.begin(),3,9);   
    170     cout<<"list1.insert(list1.begin()+1,3,9):"<<endl;   
    171     put_list(list1,"list1");   
    172 
    173     //测试引用类函数   
    174     cout<<"list1.front()="<<list1.front()<<endl;   
    175     cout<<"list1.back()="<<list1.back()<<endl;   
    176 
    177     //从list1序列的前后各移去一个元素   
    178     list1.pop_front();   
    179     list1.pop_back();   
    180     cout<<"list1.pop_front() and list1.pop_back():"<<endl;   
    181     put_list(list1,"list1");   
    182 
    183     //清除list1中的第2个元素   
    184     list1.erase(++list1.begin());   
    185     cout<<"list1.erase(++list1.begin()):"<<endl;   
    186     put_list(list1,"list1");   
    187 
    188     //对list2赋值并显示   
    189     list2.assign(8,1);   
    190     cout<<"list2.assign(8,1):"<<endl;   
    191     put_list(list2,"list2");   
    192 
    193     //显示序列的状态信息   
    194     cout<<"list1.max_size(): "<<list1.max_size()<<endl;   
    195     cout<<"list1.size(): "<<list1.size()<<endl;   
    196     cout<<"list1.empty(): "<<list1.empty()<<endl;   
    197 
    198     //list序列容器的运算   
    199     put_list(list1,"list1");   
    200     put_list(list3,"list3");   
    201     cout<<"list1>list3: "<<(list1>list3)<<endl;   
    202     cout<<"list1<list3: "<<(list1<list3)<<endl;   
    203 
    204     //对list1容器排序   
    205     list1.sort();   
    206     put_list(list1,"list1");   
    207 
    208     //结合处理   
    209     list1.splice(++list1.begin(), list3);   
    210     put_list(list1,"list1");   
    211     put_list(list3,"list3");   
    212 }   
    213 
    214 结果:
    215 The contents of list1 :
    216 The contents of list2 : 6 6 6 6 6 6 6 6 6 6
    217 The contents of list3 : 6 6 6 6 6 6 6 6 6
    218 list1.push_back(2) and list1.push_back(4):
    219 The contents of list1 : 2 4
    220 list1.push_front(5) and list1.push_front(7):
    221 The contents of list1 : 7 5 2 4
    222 list1.insert(list1.begin()+1,3,9):
    223 The contents of list1 : 7 9 9 9 5 2 4
    224 list1.front()=7
    225 list1.back()=4
    226 list1.pop_front() and list1.pop_back():
    227 The contents of list1 : 9 9 9 5 2
    228 list1.erase(++list1.begin()):
    229 The contents of list1 : 9 9 5 2
    230 list2.assign(8,1):
    231 The contents of list2 : 1 1 1 1 1 1 1 1
    232 list1.max_size(): 1073741823
    233 list1.size(): 4
    234 list1.empty(): 0
    235 The contents of list1 : 9 9 5 2
    236 The contents of list3 : 6 6 6 6 6 6 6 6 6
    237 list1>list3: 1
    238 list1<list3: 0
    239 The contents of list1 : 2 5 9 9
    240 The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9
    241 The contents of list3 :
    242 Press any key to continue
  • 相关阅读:
    spring和mybatis整合配置文件
    点击不同按钮,加载不同的页面(不使用iframe的情况下)
    两个完整的jquery slide多方面滑动效果实例
    autofac无法解析一例
    c# Random太快产生的随机数会重复
    linq和ef关于group by取最大值的两种写法
    JavaScript 汉字与拼音互转终极方案 附JS拼音输入法
    使用USB Key(加密狗)实现身份认证
    让Dreamweaver支持cshtml (MVC Razor环境)
    使用EF扩展EntityFramework.BulkInsert实现批量插入
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/12204183.html
Copyright © 2020-2023  润新知