-
forward_list 和 list 最主要的区别是:forward_list 不能反向遍历元素;只能从头到尾遍历。
-
forward_list 无法使用反向迭代器。只能从它得到const或non-const前向迭代器,这些迭代器都不能解引用,只能自增;
-
没有可以返回最后一个元素引用的成员函数back();只有成员函数front();
-
因为只能通过自增前面元素的迭代器来到达序列的终点,所以push_back()、pop_back()、emplace_back()也无法使用。
- 没有成员函数 size()
- forward_list 和 list —样都有成员函数 sort() 和 merge(),它们也都有 remove()、remove_if() 和unique(),所有这些函数的用法都和 list 相同。
- 函数
1、distance()
//distance() 函数来得到forward_list中元素的个数
forward_list<int> my_words {1, 2, 3};
int count = std::distance(begin(my_words),end(my_words));
2、advance()
//advance()函数没有返回值,函数会将前向迭代器自增需要的次数
forward_list<int> data {10, 21, 43, 87, 175, 351};
forward_list<int>::iterator iter = begin(data);
size_t n = 3;
//将当前迭代器iter向右移动n
std::advance(iter, n);
std