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() << " elementsn"; 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定義一個只用疊代器實現操作的函數模板,會使這個函數的用法變得更靈活。任何處理一段元素的演算法都可以用這種方式生成。