std::vector<double> data {32.5, 30.1, 36.3, 40.0, 39.2}; std::cout << "Enter additional data values separated by spaces or Ctrl+Z to end:" << std::endl; std::copy(std::istream_iterator<double>(std::cin) , std::istream_iterator<double>(),std::back_inserter(data)); std::copy(std::begin(data), std::end(data),std::ostream_iterator<double> (std:: cout," "))用初始化列表生成 data 容器。第一次调用 copy() 时,使用一个 istream_iterator 对象作为第一个参数,它能够从标准输入流中读取 double 类型的值。第二个参数是一个流的结束迭代器,当识别到流结束时,istream_iterator 会变为结束迭代器;当从键盘输入
Ctrl+Z
时, 这也会发生在 cin 中。#include <iostream> #include <string> #include <algorithm> #include <vector> using std::string; using std::vector; int main() { vector<string> words; // Stores words to be sorted words.reserve(10); // Allocate some space for elements std::cout << "Enter words separated by spaces. Enter Ctrl+Z on a separate line to end:" << std::endl; std::copy(std::istream_iterator <string> {std::cin}, std::istream_iterator <string> {},std::back_inserter(words)); std::cout << "Starting sort." << std::endl; bool out_of_order {false}; while (true) { for (auto first = start + 1; first != last; ++first) { if (*(first - 1) > *first) { // Out of order so swap them std::swap(*first, *(first - 1)); out_of_order = true; } } if (!out_of_order) // If they are in order (no swaps necessary)... break; // ...we are done... out_of_order = false; // ...otherwise, go round again. } std::cout << "your words in ascending sequence:" << std::endl; std::copy(std::begin(words), std::end(words), std::ostream_iterator < string > {std::cout, " "}); std::cout << std::endl; // Create a new vector by moving elements from words vector vector<string> words_copy {std::make_move_iterator(std::begin(words)),std::make_move_iterator(std::end(words))}; std::cout << "\nAfter moving elements from words, words_copy contains:" << std::endl; std::copy(std::begin(words_copy), std::end(words_copy), std::ostream_iterator < string > {std::cout, " "}); std::cout << std::endl; // See what's happened to elements in words vector... std::cout << "\nwords vector has " << words.size() << " elements\n"; if (words.front().empty()) std::cout << "First element is empty string object." << std::endl; std::cout << "First element is \"" << words.front() << "\"" << std::endl; }示例输出如下:
Enter words separated by spaces. Enter Ctrl+Z on a separate line to end:
one two three four five six seven eight
^Z
Starting sort.
your words in ascending sequence:
eight five four one seven six three two
After moving elements from words, words_copy contains:
eight five four one seven six three two
words vector has 8 elements
First element is empty string object.
First element is ""
Ctrl+Z
时,流迭代器就会匹配到它,这相当于文件流的 EOF。template<typename RandomIter> void bubble_sort(RandomIter start, RandomIter last) { std::cout << "Starting sort." << std::endl; bool out_of_order {false}; // true when values are not in order while (true) { for (auto first = start + 1; first != last; ++first) { if (*(first - 1) > *first) { // Out of order so swap them std::swap(*first, *(first - 1)); out_of_order = true; } } if (!out_of_order) // If they are in order (no swaps necessary)... break; // ...we are done... out_of_order = false; // ...otherwise, go round again. } }模板类型参数是迭代器类型。因为 for 循环中迭代器算术操作的原因,bubble_sort() 算法需要使用随机访问迭代器。只要容器可以提供随机访问迭代器,算法就可以对这个容器的内容进行排序;这也包括标准数组和字符串对象。如果在前面的 main() 中使用此代码, 就可以使用下面的语句替换掉 main() 中对 words 进行排序的部分:
bubble_sort(std::begin(words), std::end(words)); // Sort the words array定义一个只用迭代器实现操作的函数模板,会使这个函数的用法变得更灵活。任何处理一段元素的算法都可以用这种方式生成。
本文链接:http://task.lmcjl.com/news/15011.html