C++编程思想 第2卷 第6章 通用算法 概述

标准库中的通用算法还提供一个词汇表来描述各种解法
随着对算法的熟悉
会获得一个新的词汇集合用来讨论现在正在做什么
这些词汇比以往用的词汇具有更高层次的抽象copy算法的例子

//: C06:CopyInts.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.
// Copies ints without an explicit loop.
#include <algorithm>
#include <cassert>
#include <cstddef>  // For size_t
using namespace std;

int main() {
  int a[] = { 10, 20, 30 };
  const size_t SIZE = sizeof a / sizeof a[0];
  int b[SIZE];
  copy(a, a + SIZE, b);
  for(size_t i = 0; i < SIZE; ++i)
    assert(a[i] == b[i]);
} ///:~

无异常对话框
copy()算法的前两个参数表示输入序列的范围
此处是数组a
范围用一对指针表示
copy()算法可以用于任何一种类型的序列
复制string类

//: C06:CopyStrings.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.
// Copies strings.
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <string>
using namespace std;

int main() {
  string a[] = {"read", "my", "lips"};
  const size_t SIZE = sizeof a / sizeof a[0];
  string b[SIZE];
  copy(a, a + SIZE, b);
  assert(equal(a, a + SIZE, b));
} ///:~

无异常对话框

算法equal,仅当第1个序列的每一个元素与第2个序列的相应元素相等时返回true
对每个序列遍历了两次
一次用来复制
一次用来比较
而不是单一的一次循环copy()能够处理序列
该序列由类似指针的任意类型来限制
例如迭代器

//: C06:CopyVector.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.
// Copies the contents of a vector.
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <vector>
using namespace std;

int main() {
  int a[] = { 10, 20, 30 };
  const size_t SIZE = sizeof a / sizeof a[0];
  vector<int> v1(a, a + SIZE);
  vector<int> v2(SIZE);
  copy(v1.begin(), v1.end(), v2.begin());
  assert(equal(v1.begin(), v1.end(), v2.begin()));
} ///:~

无异常对话框
第1个vector对象v1由数组a中的整数序列来初始化
第2个vector对象v2定义
使用一个不同的能够为SIZE个元素分配空间的vector构造函数
并且将其初始化为0
使用back_inserter()
不需要在建立输出vector对象v2时必须确定其大小

//: C06:InsertVector.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.
// Appends the contents of a vector to another.
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <iterator>
#include <vector>
using namespace std;

int main() {
  int a[] = { 10, 20, 30 };
  const size_t SIZE = sizeof a / sizeof a[0];
  vector<int> v1(a, a + SIZE);
  vector<int> v2;  // v2 is empty here
  copy(v1.begin(), v1.end(), back_inserter(v2));
  assert(equal(v1.begin(), v1.end(), v2.begin()));
} ///:~

无异常对话框

back_inserter()函数在头文件<iterator>中定义

迭代器本质上与指针相同
使用可以在标准库中以一种能够接受迭代器和指针两种参数的方式来实现算法

猜你喜欢

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