std::forward_list<std::string> my_words {"three", "six", "eight"}; auto count = std::distance(std::begin(my_words),std::end(my_words)); // Result is 3distance() 的第一個引數是一個開始疊代器,第二個引數是一個結束疊代器,它們指定了元素範圍。當需要將前向疊代器移動多個位置時,advance() 就派上了用場。例如:
std::forward_list<int> data {10, 21, 43, 87, 175, 351}; auto iter = std::begin(data); size_t n {3}; std::advance(iter, n); std::cout << "The " << n+1 << "th element is n << *iter << std::endl; // Outputs 87這並不神奇。advance() 函數會將前向疊代器自增需要的次數。這使我們不必去迴圈自增疊代器。需要記住的是這個函數自增的是作為第一個引數的迭代器,但是並不會返回它——advance() 的返回型別為 void。
std::forward_list<std::string> my_words {"three", "six", "eight"} std::forward_list<std::string> your_words {"seven", "four", "nine"}; my_words.splice_after(my_words.before_begin(), your_words, ++std::begin(your_words));這個操作的效果是將 your_words 的最後一個元素黏接到 my_words 的開始位置,因此 my_words 會包含這些字串物件:"ninef"、"three"、"six"、"eight"。這時 your_words 就只剩下兩個字串元素:"seven"和"four”。
my_words.splice_after (my_words . before_begin () , your_words,++std::begin(your_words), std::end(your_words));最後兩個疊代器引數,指定了第三個引數所指定的 fbrward_list<T> 容器的元素範圍。在這個範圍內的元素,除了第一個,其他的都被移到第一個引數所指定容器的特定位置。 因此,如果在容器初始化後執行這條語句,my_words 會包含"four"、"nine"、"three"、"six"、 "eight",your_words 僅僅包含"seven”。
my_words.splice_after(my_words.before_begin(), your_words);上面的程式碼會將 your_words 中的全部元素拼接到第一個元素指定的位置。
// Defines the Box class for Ex2_06 #ifndef BOX_H #define BOX_H #include <iostream> // For standard streams #include <utility> // For comparison operator templates using namespace std::rel_ops; // Comparison operator template namespace class Box { private: size_t length {}; size_t width {}; size_t height {}; public: explicit Box(size_t l = 1, size_t w = 1, size_t h = 1) : length {l}, width {w}, height {h} {} double volume() const { return length*width*height; } bool operator<(const Box& box) { return volume() < box.volume(); } bool operator==(const Box& box) { return length == box.length&& width == box.width&&height == box.height; } friend std::istream& operator>>(std::istream& in, Box& box); friend std::ostream& operator<<(std::ostream& out, const Box& box); }; inline std::istream& operator>>(std::istream& in, Box& box) { std::cout << "Enter box length, width, & height separated by spaces - Ctrl+Z to end: "; size_t value; in >> value; if (in.eof()) return in; box.length = value; in >> value; box.width = value; in >> value; box.height = value; return in; } inline std::ostream& operator<<(std::ostream& out, const Box& box) { out << "Box(" << box.length << "," << box.width << "," << box.height << ") "; return out; } #endifutility 標頭檔案中的名稱空間 std::relops 包含一些比較運算子的模板。如果一個類已經定義了 operator<() 和 operator==(),那麼在需要時,這個模板會生成剩下的比較運算子函數。
// Working with a forward list #include <algorithm> // For copy() #include <iostream> // For standard streams #include <forward_list> // For forward_list container #include <iterator> // For stream iterators #include "Box.h" // List a range of elements template<typename Iter> void list_elements(Iter begin, Iter end) { size_t perline {6}; // Maximum items per line size_t count {}; // Item count while (begin != end) { std::cout << *begin++; if (++count % perline == 0) { std::cout << "n"; } } std::cout << std::endl; } int main() { std::forward_list<Box> boxes; std::copy(std::istream_iterator<Box>(std::cin), std::istream_iterator<Box>(), std::front_inserter(boxes)); boxes.sort(); // Sort the boxes std::cout << "nAfter sorting the sequence is:n"; // Just to show that we can with Box objects - use an ostream iterator std::copy(std::begin(boxes), std::end(boxes), std::ostream_iterator<Box>(std::cout, " ")); std::cout << std::endl; // Insert more boxes std::forward_list<Box> more_boxes {Box {3, 3, 3}, Box {5, 5, 5}, Box {4, 4, 4}, Box {2, 2, 2}}; boxes.insert_after(boxes.before_begin(), std::begin(more_boxes), std::end(more_boxes)); std::cout << "After inserting more boxes the sequence is:n"; list_elements(std::begin(boxes), std::end(boxes)); boxes.sort(); // Sort the boxes std::cout << std::endl; std::cout << "The sorted sequence is now:n"; list_elements(std::begin(boxes), std::end(boxes)); more_boxes.sort(); boxes.merge(more_boxes); // Merge more_boxes std::cout << "After merging more_boxes the sequence is:n"; list_elements(std::begin(boxes), std::end(boxes)); boxes.unique(); std::cout << "After removing successive duplicates the sequence is:n"; list_elements(std::begin(boxes), std::end(boxes)); // Eliminate the small ones const double max_v {30.0}; boxes.remove_if([max_v](const Box& box){ return box.volume() < max_v; }); std::cout << "After removing those with volume less than 30 the sorted sequence is:n"; list_elements(std::begin(boxes), std::end(boxes)); }執行結果為:
Enter box length, width, & height separated by spaces - Ctrl+Z to end: 4 4 5
Enter box length, width, & height separated by spaces - Ctrl+Z to end: 6 5 7
Enter box length, width, & height separated by spaces - Ctrl+Z to end: 2 2 3
Enter box length, width, & height separated by spaces - Ctrl+Z to end: 12 3
Enter box length, width, & height separated by spaces - Ctrl+Z to end: 3 3 4
Enter box length, width, & height separated by spaces - Ctrl+Z to end: 3 3 3
Enter box length, width, & height separated by spaces - Ctrl+Z to end: ^Z
After sorting the sequence is:
Box(l,2,3) Box(2,2,3) Box(3,3,3) Box(3,3,4) Box(4,4,5) Box(6,5,7)
After inserting more boxes the sequence is:
Box(3,3,3) Box(5,5,5) Box(4,4,4) Box{2,2,2) Box(1,2,3) Box(2,2,3)
Box(3,3,3) Box(3,3,4) Box(4,4,5) Box(6,5,7)
The sorted sequence is now:
Box(l,2,3) Box(2,2,2) Box(2,2,3) Box(3,3,3) Box(3,3,3) Box(3,3,4)
Box (4,4,4) Box(4,4,5) Box(5,5,5) Box(6,5,7)
After merging more_boxes the sequence is:
Box(1,2,3) Box(2,2,2) Box(2,2,2) Box(2,2,3) Box(3,3,3) Box(3,3,3)
Box(3,3,3) Box(3,3,4) Box(4,4^4) Box(4,4,4) Box(4,4,5) Box(5,5,5)
Box(5,5,5) Box(6,5,7)
After removing successive duplicates the sequence is:
Box(1,2,3) Box(2,2,2) Box(2,2,3) Box (3,3,3) Box(3,3,4) Box(4,4,4)
Box(4,4,5) Box(5,5,5) Box(6,5,7)
After removing those with volume less than 30 the sorted sequence is:
Box(3,3,4) Box(4,4) Box(4,4,5) Box(5,5,5) Box(6,5,7)