Chapter 11.順序容器forward_list[c++11]

前向鏈表簡介

前向鏈表是用單鏈表實現的,可在常量時間內在鏈表中做插入或刪除操作
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() );
eg:
  // constructors used in the same order as described above:
  std::forward_list<int> first;                      // default: empty
  std::cout << "first:"; for (int& x: first) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> second (4);                 // fill: 4 "default-constructed" int's
  std::cout << "second:"; for (int& x: second) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> third (3,77);               // fill: 3 sevety-sevens
  std::cout << "third:"; for (int& x: third) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> fourth (third.begin(), third.end()); // range initialization
  std::cout << "fourth:"; for (int& x: fourth) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> fifth (fourth);             // copy constructor
  std::cout << "fifth:"; for (int& x: fifth) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> sixth (std::move(fifth));   // move ctor. (fifth wasted)
  std::cout << "sixth:"; for (int& x: sixth) std::cout << " " << x; std::cout << std::endl;
  std::forward_list<int> seventh = {3, 52, 25, 90};  // initializer_list constructor
  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
eg:
#include <forward_list>
template<class Container>//一個自定義的類模板
Container by_two (const Container& x) {
  Container temp(x); for (auto& x:temp) x*=2; return temp;
}
int main ()
{
  std::forward_list<int> first (4);      // 4 ints
  std::forward_list<int> second (3,5);   // 3 ints with value 5
  first = second;                        // copy assignment
  second = by_two(first);                // move assignment
  std::cout << "first: ";
  for (int& x : first) std::cout << " " << x;
  std::cout << std::endl;
  std::cout << "second: ";
  for (int& x : second) std::cout << " " << x;
  std::cout << std::endl;
Output:
first: 5 5 5
second: 10 10 10
Iterators
before_begin Return iterator to before beginning
begin Return iterator to beginning
end Return iterator to end
cbefore_begin Return const_iterator to before beginning //just like before_begin
cbegin Return const_iterator to beginning
cend Return const_iterator to end 
eg:
//before_begin 不能被解引用,用於emplace_after, insert_after, erase_after or splice_after
  std::forward_list<int> mylist = {20, 30, 40, 50};
  mylist.insert_after ( mylist.before_begin(), 11 );
  std::cout << "mylist contains:";
  for ( int& x: mylist ) std::cout << " " << x;
Output:
mylist contains: 11 20 30 40 50
Capacity
empty Test whether array is empty
max_size maximum size
Element access
front Access first element
Modifiers
assign Assign content
emplace_front Construct and insert element at beginning//construct with args
emplace_after Construct and insert element
insert_after Insert elements
erase_after Erase elements
swap Swap content//algorithm exists swap, and the same behavior.
clear Clear content
resize Change size
push_front Insert element at beginning→list、forward_list and deque unique
pop_front Delete 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 );
eg:
  first.assign (4,15);                           // 15 15 15 15	fill
  second.assign (first.begin(),first.end());     // 15 15 15 15	range
  first.assign ( {77, 2, 16} );                  // 77 2 16	initializer_list
//emplace_front
1.template <class... Args>
void emplace_front (Args&&... args);
eg:
  std::forward_list< std::pair<int,char> > mylist;
  mylist.emplace_front(10,'a');
  mylist.emplace_front(20,'b');
  mylist.emplace_front(30,'c');
  std::cout << "mylist contains:";
  for (auto& x: mylist)
    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 );
eg:
  std::forward_list< std::pair<int,char> > mylist;
  auto it = mylist.before_begin();
  it = mylist.emplace_after ( it, 100, 'x' );
  it = mylist.emplace_after ( it, 200, 'y' );
  it = mylist.emplace_after ( it, 300, 'z' );
  std::cout << "mylist contains:";
  for (auto& x: mylist)
    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 );
eg:
  std::array<int,3> myarray = { 11, 22, 33 };
  std::forward_list<int> mylist;
  std::forward_list<int>::iterator it;
  it = mylist.insert_after ( mylist.before_begin(), 10 );          // 10
                                                                   //  ^  <- it
  it = mylist.insert_after ( it, 2, 20 );                          // 10 20 20
                                                                   //        ^
  it = mylist.insert_after ( it, myarray.begin(), myarray.end() ); // 10 20 20 11 22 33
                                                                   //                 ^
  it = mylist.begin();                                             //  ^
  it = mylist.insert_after ( it, {1,2,3} );                        // 10 1 2 3 20 20 11 22 33
                                                                   //        ^
  std::cout << "mylist contains:";
  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 );
eg:
  std::forward_list<int> mylist = {10, 20, 30, 40, 50};
                                            // 10 20 30 40 50
  auto it = mylist.begin();                 // ^
  it = mylist.erase_after(it);              // 10 30 40 50
                                            //    ^
  it = mylist.erase_after(it,mylist.end()); // 10 30
                                            //       ^
  std::cout << "mylist contains:";
  for (int& x: mylist) std::cout << " " << x;
Output:
mylist contains: 10 30
Operations
splice_after Move elements from another forward_list
remove Remove elements with specific value//algorithm exists one operating between two iterators.
remove_if Remove elements fulfilling condition//same
unique Remove duplicate values//same
merge Merge sorted lists
sort Sort elements in container
reverse Reverse 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 );
eg:
  std::forward_list<int> first = { 1, 2, 3 };
  std::forward_list<int> second = { 10, 20, 30  };
  auto it = first.begin();  // points to the 1
  first.splice_after ( first.before_begin(), second );
                          // first: 10 20 30 1 2 3
                          // second: (empty)
                          // "it" still points to the 1 (now first's 4th element)
  second.splice_after ( second.before_begin(), first, first.begin(), it);
                          // first: 10 2 3
                          // second: 20 30 1
  first.splice_after ( first.before_begin(), second, second.begin() );
                          // first: 30 10 2 3
                          // second: 20 1
                          // * notice that what is moved is AFTER the iterator
  std::cout << "first contains:";
  for (int& x: first) std::cout << " " << x;
  std::cout << std::endl;
  std::cout << "second contains:";
  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 );
eg:
  std::forward_list<double> first = {4.2, 2.9, 3.1};
  std::forward_list<double> second = {1.4, 7.7, 3.1};
  std::forward_list<double> third = {6.2, 3.7, 7.1};
  first.sort();
  second.sort();
  first.merge(second);
  std::cout << "first contains:";
  for (double& x: first) std::cout << " " << x;
  std::cout << std::endl;
  first.sort (std::greater<double>());
  third.sort (std::greater<double>());
  first.merge (third, std::greater<double>());
  std::cout << "first contains:";
  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 );
eg:
  std::forward_list<int> first = {10, 20, 30};
  std::forward_list<int> second = {100, 200};
  std::forward_list<int>::iterator it;
  swap(first,second);

發佈了39 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章