std::forward_list<std::string> my_words {"three", "six", "eight"};
auto count = std::distance(std::begin(my_words),std::end(my_words));
// Result is 3
distance() 的第一个参数是一个开始迭代器,第二个参数是一个结束迭代器,它们指定了元素范围。当需要将前向迭代器移动多个位置时,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;
}
#endif
utility 头文件中的命名空间 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)
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有