STL forward_list

前向链表简介

前向链表是用单链表实现的,可在常量时间内在链表中做插入或删除操作
list比之forward_list,双向链表要消耗额外的空间存储每个元素和在插入和删除元素时一个轻微的更高的时间开销,所以forward_list更有效率,虽然只能向前遍历。
forward_list是唯一的标准容器中故意不给出size()成员函数的,这样是为了更高效而考虑,可以用distance(c.begin(),c.end())来得到forward_list的大小,这将消耗一个线性时间,而如果同list一样实现size()成员函数的话,那样要消耗一些额外的存储空间[用于链表中的内部计数得出size()]和使得插入和删除元素时有一个轻微的效率降低,实现size()要消耗一个常量的时间。

构造函数
// empty (default)
1.explicit forward_list ( const allocator_type& alloc = allocator_type() );
//copy
2.forward_list ( const forward_list& fwdlst );
  forward_list ( const forward_list& fwdlst, const allocator_type& alloc );
//move
3.forward_list ( forward_list&& fwdlst );
  forward_list ( forward_list&& fwdlst, const allocator_type& alloc );
//size
4.explicit forward_list ( size_type n );
//fill
5.explicit forward_list ( size_type n, const value_type& val, const allocator_type& alloc = allocator_type() );
//range
6.template < class InputIterator >
         forward_list ( InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type() );
//initializer list
7.forward_list ( initializer_list<value_type> il, const allocator_type& alloc = allocator_type() );
  1. eg:  
  2.   // constructors used in the same order as described above:  
  3.   std::forward_list<int> first;                      // default: empty  
  4.   std::cout << "first:"for (int& x: first) std::cout << " " << x; std::cout << std::endl;  
  5.   std::forward_list<int> second (4);                 // fill: 4 "default-constructed" int's  
  6.   std::cout << "second:"for (int& x: second) std::cout << " " << x; std::cout << std::endl;  
  7.   std::forward_list<int> third (3,77);               // fill: 3 sevety-sevens  
  8.   std::cout << "third:"for (int& x: third) std::cout << " " << x; std::cout << std::endl;  
  9.   std::forward_list<int> fourth (third.begin(), third.end()); // range initialization  
  10.   std::cout << "fourth:"for (int& x: fourth) std::cout << " " << x; std::cout << std::endl;  
  11.   std::forward_list<int> fifth (fourth);             // copy constructor  
  12.   std::cout << "fifth:"for (int& x: fifth) std::cout << " " << x; std::cout << std::endl;  
  13.   std::forward_list<int> sixth (std::move(fifth));   // move ctor. (fifth wasted)  
  14.   std::cout << "sixth:"for (int& x: sixth) std::cout << " " << x; std::cout << std::endl;  
  15.   std::forward_list<int> seventh = {3, 52, 25, 90};  // initializer_list constructor  
  16.   std::cout << "seventh:"for (int& x: seventh) std::cout << " " << x; std::cout << std::endl;  
Possible output:
forward_list constructor examples:
first:
second 0 0 0 0
third: 77 77 77
fourth: 77 77 77
fifth: 77 77 77
sixth: 77 77 77
seventh: 3 52 25 90
operator=
函数原型:
1.forward_list& operator= ( const forward_list& fwdlst ); //copy
2.forward_list& operator= ( forward_list&& fwdlst ); //move
3.forward_list& operator= ( initializer_list<value_type> il );//initializer list
  1. eg:  
  2. #include <forward_list>  
  3. template<class Container>//一个自定义的类模板  
  4. Container by_two (const Container& x) {  
  5.   Container temp(x); for (auto& x:temp) x*=2; return temp;  
  6. }  
  7. int main ()  
  8. {  
  9.   std::forward_list<int> first (4);      // 4 ints  
  10.   std::forward_list<int> second (3,5);   // 3 ints with value 5  
  11.   first = second;                        // copy assignment  
  12.   second = by_two(first);                // move assignment  
  13.   std::cout << "first: ";  
  14.   for (int& x : first) std::cout << " " << x;  
  15.   std::cout << std::endl;  
  16.   std::cout << "second: ";  
  17.   for (int& x : second) std::cout << " " << x;  
  18.   std::cout << std::endl;  
Output:
first: 5 5 5
second: 10 10 10
Iterators
before_beginReturn iterator to before beginning
beginReturn iterator to beginning
endReturn iterator to end
cbefore_beginReturn const_iterator to before beginning //just like before_begin
cbeginReturn const_iterator to beginning
cendReturn const_iterator to end 
  1. eg:  
  2. //<strong>before_begin</strong> 不能被解引用,用于emplace_after, insert_after, erase_after or splice_after  
  3.   std::forward_list<int> mylist = {20, 30, 40, 50};  
  4.   mylist.insert_after ( mylist.before_begin(), 11 );  
  5.   std::cout << "mylist contains:";  
  6.   for ( int& x: mylist ) std::cout << " " << x;  
Output:
mylist contains: 11 20 30 40 50
Capacity
emptyTest whether array is empty
max_sizemaximum size
Element access
frontAccess first element
Modifiers
assignAssign content
emplace_frontConstruct and insert element at beginning//construct with args
emplace_afterConstruct and insert element
insert_afterInsert elements
erase_afterErase elements
swapSwap content//algorithm exists swap, and the same behavior.
clearClear content
resizeChange size
push_frontInsert element at beginning→list、forward_list and deque unique
pop_frontDelete first element→list、forward_list and deque unique
// assign
// range
1.template <class InputIterator>
  void assign ( InputIterator first, InputIterator last );
//fill
2.void assign ( size_type n, const value_type& val );
//initializer list
3. void assign ( initializer_list<value_type> il );
  1. eg:  
  2.   first.assign (4,15);                           // 15 15 15 15 fill  
  3.   second.assign (first.begin(),first.end());     // 15 15 15 15 range  
  4.   first.assign ( {77, 2, 16} );                  // 77 2 16 initializer_list  
// emplace_front
1.template <class... Args>
void emplace_front (Args&&... args);
  1. eg:  
  2.   std::forward_list< std::pair<int,char> > mylist;  
  3.   mylist.emplace_front(10,'a');  
  4.   mylist.emplace_front(20,'b');  
  5.   mylist.emplace_front(30,'c');  
  6.   std::cout << "mylist contains:";  
  7.   for (auto& x: mylist)  
  8.     std::cout << " (" << x.first << "," << x.second << ")";  
Output:
mylist contains: (30,c) (20,b) (10,a)
// emplace_after
template <class... Args>
iterator emplace_after ( const_iterator position, Args&&... args );
  1. eg:  
  2.   std::forward_list< std::pair<int,char> > mylist;  
  3.   auto it = mylist.before_begin();  
  4.   it = mylist.emplace_after ( it, 100, 'x' );  
  5.   it = mylist.emplace_after ( it, 200, 'y' );  
  6.   it = mylist.emplace_after ( it, 300, 'z' );  
  7.   std::cout << "mylist contains:";  
  8.   for (auto& x: mylist)  
  9.     std::cout << " (" << x.first << "," << x.second << ")";  
Output:
mylist contains: (100,x) (200,y) (300,z)
// insert_after
1.iterator insert_after ( const_iterator position, const value_type& val );
2.iterator insert_after ( const_iterator position, value_type&& val );
3.iterator insert_after ( const_iterator position, size_type n, const value_type& val );
4.template <class InputIterator>
  iterator insert_after ( const_iterator position, InputIterator first, InputIterator last );
5.iterator insert_after ( const_iterator position, initializer_list<value_type> il );
  1. eg:  
  2.   std::array<int,3> myarray = { 11, 22, 33 };  
  3.   std::forward_list<int> mylist;  
  4.   std::forward_list<int>::iterator it;  
  5.   it = mylist.insert_after ( mylist.before_begin(), 10 );          // 10  
  6.                                                                    //  ^  <- it  
  7.   it = mylist.insert_after ( it, 2, 20 );                          // 10 20 20  
  8.                                                                    //        ^  
  9.   it = mylist.insert_after ( it, myarray.begin(), myarray.end() ); // 10 20 20 11 22 33  
  10.                                                                    //                 ^  
  11.   it = mylist.begin();                                             //  ^  
  12.   it = mylist.insert_after ( it, {1,2,3} );                        // 10 1 2 3 20 20 11 22 33  
  13.                                                                    //        ^  
  14.   std::cout << "mylist contains:";  
  15.   for (int& x: mylist) std::cout << " " << x;  
Output:
mylist contains: 10 1 2 3 20 20 11 22 33
// erase_after
1.iterator erase ( const_iterator position );
2.iterator erase ( const_iterator position, const_iterator last );
  1. eg:  
  2.   std::forward_list<int> mylist = {10, 20, 30, 40, 50};  
  3.                                             // 10 20 30 40 50  
  4.   auto it = mylist.begin();                 // ^  
  5.   it = mylist.erase_after(it);              // 10 30 40 50  
  6.                                             //    ^  
  7.   it = mylist.erase_after(it,mylist.end()); // 10 30  
  8.                                             //       ^  
  9.   std::cout << "mylist contains:";  
  10.   for (int& x: mylist) std::cout << " " << x;  
Output:
mylist contains: 10 30
Operations
splice_afterMove elements from another forward_list
removeRemove elements with specific value//algorithm exists one operating between two iterators.
remove_ifRemove elements fulfilling condition//same
uniqueRemove duplicate values//same
mergeMerge sorted lists
sortSort elements in container
reverseReverse the order of elements
// splice_aftermoves the elements in the range (first,last) to position in 3
1.
void splice_after ( const_iterator position, forward_list& fwdlst );//移动是fwdlst里的所有元素到position
void splice_after ( const_iterator position, forward_list&& fwdlst );
2.
void splice_after ( const_iterator position, forward_list& fwdlst, const_iterator i );//移动的是i的后一元素
void splice_after ( const_iterator position, forward_list&& fwdlst, const_iterator i );
3.
void splice_after ( const_iterator position, forward_list& fwdlst,//移动的是fwdlst里的(first,last)到position
                    const_iterator first, const_iterator last );
void splice_after ( const_iterator position, forward_list&& fwdlst,
                    const_iterator first, const_iterator last );
  1. eg:  
  2.   std::forward_list<int> first = { 1, 2, 3 };  
  3.   std::forward_list<int> second = { 10, 20, 30  };  
  4.   auto it = first.begin();  // points to the 1  
  5.   first.splice_after ( first.before_begin(), second );  
  6.                           // first: 10 20 30 1 2 3  
  7.                           // second: (empty)  
  8.                           // "it" still points to the 1 (now first's 4th element)  
  9.   second.splice_after ( second.before_begin(), first, first.begin(), it);  
  10.                           // first: 10 2 3  
  11.                           // second: 20 30 1  
  12.   first.splice_after ( first.before_begin(), second, second.begin() );  
  13.                           // first: 30 10 2 3  
  14.                           // second: 20 1  
  15.                           // * notice that what is moved is AFTER the iterator  
  16.   std::cout << "first contains:";  
  17.   for (int& x: first) std::cout << " " << x;  
  18.   std::cout << std::endl;  
  19.   std::cout << "second contains:";  
  20.   for (int& x: second) std::cout << " " << x;  
Output:
first contains: 30 10 2 3
second contains: 20 1
// merge
1.void merge ( forward_list& fwdlst );
  void merge ( forward_list&& fwdlst );
2.template <class Compare>
  void merge ( forward_list& fwdlst, Compare comp );
template <class Compare>
  void merge ( forward_list&& fwdlst, Compare comp );
  1. eg:  
  2.   std::forward_list<double> first = {4.2, 2.9, 3.1};  
  3.   std::forward_list<double> second = {1.4, 7.7, 3.1};  
  4.   std::forward_list<double> third = {6.2, 3.7, 7.1};  
  5.   first.sort();  
  6.   second.sort();  
  7.   first.merge(second);  
  8.   std::cout << "first contains:";  
  9.   for (double& x: first) std::cout << " " << x;  
  10.   std::cout << std::endl;  
  11.   first.sort (std::greater<double>());  
  12.   third.sort (std::greater<double>());  
  13.   first.merge (third, std::greater<double>());  
  14.   std::cout << "first contains:";  
  15.   for (double& x: first) std::cout << " " << x;  
Output:
first contains: 1.4 2.9 3.1 3.1 4.2 7.7
first contains: 7.7 7.1 6.2 4.2 3.7 3.1 3.1 2.9 1.4
Observers
get_allocator
Global functions
operators (forward_list)Global relational operator functions for forward_list
swap (forward_list)Exchanges the contents of two forward_list containers//没有实际的移动,只是交换数据的引用
// operators(forward_list) just like array container
1.template <class T, class Alloc>
  bool operator== ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
2.template <class T, class Alloc>
  bool operator!= ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
3.template <class T, class Alloc>
  bool operator< ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
4.template <class T, class Alloc>
  bool operator> ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
5.template <class T, class Alloc>
  bool operator>= ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
6.template <class T, class Alloc>
  bool operator<= ( const forward_list<T,Alloc>& lhs, const forward_list<T,Alloc>& rhs );
// swap
template <class T, class Alloc>
  void swap ( forward_list<T,Alloc>& lhs,
              forward_list<T,Alloc>& rhs );
  1. eg:  
  2.   std::forward_list<int> first = {10, 20, 30};  
  3.   std::forward_list<int> second = {100, 200};  
  4.   std::forward_list<int>::iterator it;  
  5.   swap(first,second);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值