大佬教程收集整理的这篇文章主要介绍了C++ forward_list用法详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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","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 (),++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,& height separated by spaces - Ctrl+Z to end: 4 4 5
Enter Box length,& height separated by spaces - Ctrl+Z to end: 6 5 7
Enter Box length,& height separated by spaces - Ctrl+Z to end: 2 2 3
Enter Box length,& height separated by spaces - Ctrl+Z to end: 12 3
Enter Box length,& height separated by spaces - Ctrl+Z to end: 3 3 4
Enter Box length,& height separated by spaces - Ctrl+Z to end: 3 3 3
Enter Box length,& height separated by spaces - Ctrl+Z to end: ^Z
After sorTing the sequence is:
Box(l,3) Box(2,3) Box(3,4) Box(4,5) Box(6,7)
After inserTing more Boxes the sequence is:
Box(3,3) Box(5,5) Box(4,4) Box{2,2) Box(1,3)
Box(3,7)
The sorted sequence is Now:
Box(l,2) Box(2,4)
Box (4,5) Box(5,7)
After merging more_Boxes the sequence is:
Box(1,4^4) Box(4,5)
Box(5,7)
After removing successive duplicates the sequence is:
Box(1,3) Box (3,4)
Box(4,7)
After removing those with volume less than 30 the sorted sequence is:
Box(3,7)
以上是大佬教程为你收集整理的C++ forward_list用法详解全部内容,希望文章能够帮你解决C++ forward_list用法详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。