C++容器之forward_list
2014-06-03 20:53
281 查看
简介
Forward_list是一种能在常数时间内在任何位置插入和删除的顺序容器。Forward_list是单向链表。Forward_list和list的区别在于前者是单向链表,在内部元素只有一个链接到下一个元素,它的迭代器是前向有效的;后者是双向链表,在内部存在两个链接,一个链接指向下一个元素,另一个链接指向前面一个元素,它的迭代器是双向有效地,在存储方面list会消耗更多的空间,插入和删除元素会稍微消耗更多的时间。Forward_list的迭代器是虽然是单向的,但是它比list效率高。
Forward_list与其他标准容器(array、deque、vector)相比较,在容器的任何位置插入、提取、移动元素的效率更高。但是根据位置直接访问元素的操作其他几个容器效率更高。
成员函数
复制控制
forward_list::forward_list()
构造函数:创建一个forward_list容器对象,根据参数初始化其元素。forward_list::~forward_list()
析构函数:销毁一个forward_list容器对象。forward_list::operator=
赋值函数:为容器分配新的内容,代替其当前内容。示例代码
#include <iostream> #include <forward_list> using namespace std; void print(forward_list<int> l) { for(int &x : l){ cout << x << " "; } cout << endl; } template<class container> container by_two(const container& x) { container temp(x); for(auto &x : temp){ x *= 2; } return temp; } int main(void) { // forward_list::forward_list() forward_list<int> first; forward_list<int> second(5, 10); forward_list<int> third(second.begin(), second.end()); //unlike other container forward_list<int> fourth(second); forward_list<int> fifth(move(fourth)); forward_list<int> sixth = {2, 5, 9, 6}; cout << "first : "; print(first); cout << "second : "; print(second); cout << "third : "; print(third); cout << "fourth : "; print(fourth); cout << "fifth : "; print(fifth); cout << "sixth : "; print(sixth); // forward_list::operator= first = second; second = by_two(first); cout << "first : " ; print(first); print(second); return 0; }
Iterator
forward_list::before_begin()
返回一个迭代器,指向forwar_list容器第一个元素的前一个位置,返回值类型为iterator/const_iterator。forward_list::begin()
返回一个迭代器,指向forward_list容器第一个元素,返回值类型为iterator/const_iterator。forward_list::end()
返回容器最后一个元素的下一个位置。返回值类型为iterator/const_iterator。forward_list:cbefore_begin()
返回一个const_iterator迭代器,指向forwar_list容器第一个元素的前一个位置。forward_list::cbegin()
返回一个const_iterator迭代器,指向forward_list容器第一个元素。forward_list::cend()
返回一个const_iterator迭代器,指向forward_list容器最后一个元素的下一个位置。示例代码
#include <iostream> #include <forward_list> using namespace std; template<class container> void print(container& x) { for(auto &value : x){ cout << value << " "; } cout << endl; } int main(void) { forward_list<int> first = {12, 8, 32, 24}; // forward_list::before_begin() first.insert_after(first.before_begin(), 5); print(first); // forward_list::end() for(forward_list<int>::iterator it = first.begin(); it != first.end(); ++it){ cout << *it << " "; } cout << endl; // forward_list::cbefore_begin() first.insert_after(first.cbefore_begin(), 2); print(first); // forward_list::cbegin() cend() for(forward_list<int>::const_iterator it = first.cbegin(); it != first.cend(); ++it){ cout << *it << " "; } cout << endl; }
Capacity
forward_list::empty()
测试forward_list容器是否为空。返回值类型为bool,如果为空返回true,否则返回false。forward_list::max_size()
返回forward_list容器能存储元素的最大个数。示例代码
#include <iostream> #include <forward_list> using namespace std; int main(void) { forward_list<int> first = {2, 5, 7, 9}; // forward_list::empty() cout << "first is " << (first.empty() ? "empty" : "not empty!") << endl; // forward_list::max_size() cout << "The max size of first is " << first.max_size() << endl; return 0; }
Element access
forward_list::front()
返回forward_list容器第一个元素的引用。返回值类型为reference/const_reference。示例程序
#include <iostream> #include <forward_list> using namespace std; int main(void) { forward_list<int> first = {3, 4, 5}; // froward_list::front() forward_list<int>::reference re = first.front(); cout << "The first element is " << re << endl; re = 2; cout << "The first element is " << re << endl; return 0; }
Modifiers
forward_list::assign()
为forward_list容器分配新的内容,以代替其原来的内容,并且随之修改容器的大小。forward_list::emplace_front()
在forward_list容器第一个元素前插入一个元素,这个元素被它自身的构造函数构造。一般用于复合类型数据操作。forward_list::push_front()
在forward_list容器第一个元素前插入一个元素。forward_list::pop_front()
将forward_list容器的第一个元素删除,size减1。forward_list::emplace_after()
在指定位置后面插入一个元素,元素由其构造函数构造。一般用于复合类型数据操作。forward_list::insert_after()
在指定位置后面插入一个或一组元素。forward_list::erase_after()
将指定位置的一个或一组数据删除。forward_list::swap()
交换两个forward_list容器的内容。forward_list::resize()
重新设置forward_list容器的大小。如果设置的值n小于当前容器的容量,则只取容器前n个元素;如果n大于当前容器的容量且给定了指定填充值,则将容器扩展到容量为n,并用填充值填充,否则用默认构造函数值填充。forward_list::clear()
删除容器forward_list中的所有元素。示例程序
#include <iostream> #include <forward_list> using namespace std; template<class container> void print(container x) { for(auto &value : x){ cout << value << " "; } cout << endl; } int main(void) { forward_list<int> first; forward_list<int> second; // forward_list::assign() first.assign(3, 5); print(first); second.assign(first.begin(), first.end()); print(second); first.assign({2, 3, 4}); print(first); // forward_list::emplace_front() forward_list<pair<int, char>> list; list.emplace_front(10, 'a'); list.emplace_front(20, 'b'); list.emplace_front(30, 'c'); for(auto &x : list){ cout << "(" << x.first << "," << x.second << ")" << " "; } cout << endl; // forward_list::push_front() first.push_front(1); print(first); // forward_list::pop_front() first.pop_front(); print(first); // forward_list::emplace_after forward_list<pair<int, char>>::iterator it; it = list.before_begin(); list.emplace_after(it, 40, 'd'); for(auto &x : list){ cout << "(" << x.first << ',' << x.second << ")" << " "; } cout << endl; // forward_list::insert_after() first.insert_after(first.before_begin(), 1); print(first); first.insert_after(first.before_begin(), second.begin(), second.end()); print(first); first.insert_after(first.before_begin(), 3, 6); print(first); // forward_list::erase_after() first.erase_after(first.before_begin()); print(first); first.erase_after(first.begin()++, first.end()); print(first); // forward_list::swap() first.swap(second); print(first); print(second); // forward_lsit::resize() first.resize(2); print(first); first.resize(5); print(first); first.resize(10, 10); print(first); // froward_list::clear() first.clear(); print(first); return 0; }
Operations
forward_list::splice_after()
将一个forward_list容器中指定位置的一个或一组元素转移到另外一个forward_list容器的指定位置并将其从当前容器中移除。forward_list::remove()
删除容器中所有等于指定值的元素。forward_list::remove_if()
删除容器中所有满足指定条件的元素。forward_list::unique()
删除容器中所有相同或满足给定条件的元素,只保留第一个。调用这个函数前,必选先对容器中的元素进行排序。forward_list::merge()
将两个forward_list容器合并。调用该函数前序对容器进行排序。foreard_list::sort()
对rorward_ist容器进行排序,改变元素在容器中的存储位置。foreard_list:reverse()
反转容器中元素的存储位置。示例程序
#include <iostream> #include <forward_list> using namespace std; template<class container> void print(container x) { for(auto &value : x){ cout << value << ' '; } cout << endl; } bool condition(const int& value) { return(value > 5); } class is_old_class{ public: bool operator() (const int& value) { return ((value % 2) == 1); } }; bool same_integral_part(double first, double second) { return (int(first) == int(second)); } class is_near_class { public: bool operator() (double first, double second) { return (second - first) < 5.0; } }; int main(void) { forward_list<int> first = {1, 2, 3}; forward_list<int> second = {10, 20, 30}; forward_list<double> third = {15.2, 73.0, 3.14, 15.85, 69.5, 73.0, 3.99, 15.2, 69.2, 18.5}; // forward_list::splice_after auto it = first.begin(); first.splice_after(first.before_begin(), second); print(first); print(second); second.splice_after(second.before_begin(), first, first.begin(), it); print(first); print(second); first.splice_after(first.before_begin(), second, second.begin()); print(first); print(second); // forward_list::remove() first.remove(30); print(first); // forward_lsit::remove_if() first.remove_if(condition); print(first); is_old_class is_old_object; first.remove_if(is_old_object); print(first); // forward_list::unique() third.sort(); third.unique(); print(third); third.unique(same_integral_part); print(third); is_near_class is_near_object; third.unique(is_near_object); print(third); // forward_list::merge() first.sort(); second.sort(); first.merge(second); print(first); // forward_list::reverse() third.reverse(); print(third); }
相关文章推荐
- C++学习:list容器详解(一)
- C++ STL学习笔记四 list双向链表容器
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- c++ list 容器使用例子
- 【C++ STL学习之四】容器list深入学习
- C++ STL基本容器string,vector,list,deque,map..[转]
- C++ 中list容器的简要使用说明(2)
- linux下练习 c++ 类库中list的特性、关联式容器共性介绍
- Effective C++ (8) 顺序容器vector,list,deque
- 【C++ STL学习之四】容器list深入学习
- C++学习:list容器详解(一)
- C++ STL 容器技术 之 list双向链表容器
- C++ STL 容器技术 之 list双向链表容器
- 实现一个简单的c++ list容器(含sort排序 链表归并算法实现)
- C++顺序容器vector、deque、list
- C++学习:list容器详解(二)
- 【C++ STL学习之四】容器list深入学习
- c++容器(vector、list、deque)
- c++的STL模板库中3种容器类:vector,list,deque的比较
- c++中list容器的简要使用说明(1)