C++编程思想 第2卷 第7章 通用容器 基本序列容器:vector list deque 链表 特殊的list操作

list有一些特殊的内置操作使其以更好的方式利用list结构
除了reverse()和sort() 还有另外一些操作

//: C07:ListSpecialFunctions.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
//{L} Noisy
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include "Noisy.h"
#include "PrintContainer.h"
using namespace std;

int main() {
  typedef list<Noisy> LN;
  LN l1, l2, l3, l4;
  generate_n(back_inserter(l1), 6, NoisyGen());
  generate_n(back_inserter(l2), 6, NoisyGen());
  generate_n(back_inserter(l3), 6, NoisyGen());
  generate_n(back_inserter(l4), 6, NoisyGen());
  print(l1, "l1", " "); print(l2, "l2", " ");
  print(l3, "l3", " "); print(l4, "l4", " ");
  LN::iterator it1 = l1.begin();
  ++it1; ++it1; ++it1;
  l1.splice(it1, l2);
  print(l1, "l1 after splice(it1, l2)", " ");
  print(l2, "l2 after splice(it1, l2)", " ");
  LN::iterator it2 = l3.begin();
  ++it2; ++it2; ++it2;
  l1.splice(it1, l3, it2);
  print(l1, "l1 after splice(it1, l3, it2)", " ");
  LN::iterator it3 = l4.begin(), it4 = l4.end();
  ++it3; --it4;
  l1.splice(it1, l4, it3, it4);
  print(l1, "l1 after splice(it1,l4,it3,it4)", " ");
  Noisy n;
  LN l5(3, n);
  generate_n(back_inserter(l5), 4, NoisyGen());
  l5.push_back(n);
  print(l5, "l5 before remove()", " ");
  l5.remove(l5.front());
  print(l5, "l5 after remove()", " ");
  l1.sort(); l5.sort();
  l5.merge(l1);
  print(l5, "l5 after l5.merge(l1)", " ");
  cout << "\n Cleanup" << endl;
  getchar();
} ///:~


输出
d[0]
c[0]
~[0]
d[1]
c[1]
~[1]
d[2]
c[2]
~[2]
d[3]
c[3]
~[3]
d[4]
c[4]
~[4]
d[5]
c[5]
~[5]
d[6]
c[6]
~[6]
d[7]
c[7]
~[7]
d[8]
c[8]
~[8]
d[9]
c[9]
~[9]
d[10]
c[10]
~[10]
d[11]
c[11]
~[11]
d[12]
c[12]
~[12]
d[13]
c[13]
~[13]
d[14]
c[14]
~[14]
d[15]
c[15]
~[15]
d[16]
c[16]
~[16]
d[17]
c[17]
~[17]
d[18]
c[18]
~[18]
d[19]
c[19]
~[19]
d[20]
c[20]
~[20]
d[21]
c[21]
~[21]
d[22]
c[22]
~[22]
d[23]
c[23]
~[23]
l1:  0 1 2 3 4 5
l2:  6 7 8 9 10 11
l3:  12 13 14 15 16 17
l4:  18 19 20 21 22 23
l1 after splice(it1, l2):  0 1 2 6 7 8 9 10 11 3 4 5
l2 after splice(it1, l2):
l1 after splice(it1, l3, it2):  0 1 2 6 7 8 9 10 11 15 3 4 5
l1 after splice(it1,l4,it3,it4):  0 1 2 6 7 8 9 10 11 15 19 20 21 22 3 4 5
d[24]
c[24]
c[24]
c[24]
d[25]
c[25]
~[25]
d[26]
c[26]
~[26]
d[27]
c[27]
~[27]
d[28]
c[28]
~[28]
c[24]
l5 before remove():  24 24 24 25 26 27 28 24
c[24]
~[24]
~[24]
~[24]
~[24]
~[24]
l5 after remove():  25 26 27 28
l5 after l5.merge(l1):  0 1 2 3 4 5 6 7 8 9 10 11 15 19 20 21 22 25 26 27 28

 Cleanup

用Noisy对象填充了4个list之后
一个list通过3种方式结合成另一个list
整个链表l2在迭代器it1出被接合为链表l1


一个unique()成员函数将从list中删除所有重复的对象
唯一的条件是首先对list进行排序

//: C07:UniqueList.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Testing list's unique() function.
#include <iostream>
#include <iterator>
#include <list>
using namespace std;

int a[] = { 1, 3, 1, 4, 1, 5, 1, 6, 1 };
const int ASZ = sizeof a / sizeof *a;

int main() {
  // For output:
  ostream_iterator<int> out(cout, " ");
  list<int> li(a, a + ASZ);
  li.unique();
  // Oops! No duplicates removed:
  copy(li.begin(), li.end(), out);
  cout << endl;
  // Must sort it first:
  li.sort();
  copy(li.begin(), li.end(), out);
  cout << endl;
  // Now unique() will have an effect:
  li.unique();
  copy(li.begin(), li.end(), out);
  cout << endl;
  getchar();
} ///:~


输出
1 3 1 4 1 5 1 6 1
1 1 1 1 1 3 4 5 6
1 3 4 5 61 3 1 4 1 5 1 6 1
1 1 1 1 1 3 4 5 6
1 3 4 5 6

这里使用的list构造函数采用来自另外一个容器的起始和超越末尾的迭代器
并将容器中的所有元素复制到其自己的list中

函数unique()仅仅将相邻的重复元素输出
因此调用unique()之前需要对序列进行排序

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/82422489