前向链表简介
前向链表是用单链表实现的,可在常量时间内在链表中做插入或删除操作
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:
- //<strong>before_begin</strong> 不能被解引用,用于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);