list

目录

 

源码声明:

元素要求:

容器特点:

成员函数:

函数列表:

Member functions

Non-member function overloads

特别注意:


源码声明:

template < class T, class Alloc = allocator<T> > class list;

元素要求:

可复制;可赋值

容器特点:

首段和尾端增减元素高效,中间插入或删除元素需要移动其他元素

成员函数:

http://www.cplusplus.com/reference/list/list/

函数列表:

Member functions

(constructor)

Construct list (public member function )

(destructor)

List destructor (public member function )

operator=

Assign content (public member function )


Iterators:

begin

Return iterator to beginning (public member function )

end

Return iterator to end (public member function )

rbegin

Return reverse iterator to reverse beginning (public member function )

rend

Return reverse iterator to reverse end (public member function )

cbegin C++11

Return const_iterator to beginning (public member function )

cend C++11

Return const_iterator to end (public member function )

crbegin C++11

Return const_reverse_iterator to reverse beginning (public member function )

crend C++11

Return const_reverse_iterator to reverse end (public member function )


Capacity:

empty

Test whether container is empty (public member function )

size

Return size (public member function )

max_size

Return maximum size (public member function )


Element access:

front

Access first element (public member function )

back

Access last element (public member function )


Modifiers:

assign

Assign new content to container (public member function )

emplace_front C++11

Construct and insert element at beginning (public member function )

push_front

Insert element at beginning (public member function )

pop_front

Delete first element (public member function )

emplace_back C++11

Construct and insert element at the end (public member function )

push_back

Add element at the end (public member function )

pop_back

Delete last element (public member function )

emplace C++11

Construct and insert element (public member function )

insert

Insert elements (public member function )

erase

Erase elements (public member function )

swap

Swap content (public member function )

resize

Change size (public member function )

clear

Clear content (public member function )


Operations:

splice

Transfer elements from list to list (public member function )

remove

Remove elements with specific value (public member function )

remove_if

Remove elements fulfilling condition (public member function template )

unique

Remove duplicate values (public member function )

merge

Merge sorted lists (public member function )

sort

Sort elements in container (public member function )

reverse

Reverse the order of elements (public member function )


Observers:

get_allocator

Get allocator (public member function )

 

Non-member function overloads

relational operators (list)

Relational operators for list (function )

swap (list)

Exchanges the contents of two lists (function template )

特别注意:

splice:

//entire list (1)	
void splice (const_iterator position, list& x);
void splice (const_iterator position, list&& x);

//single element (2)	
void splice (const_iterator position, list& x, const_iterator i);
void splice (const_iterator position, list&& x, const_iterator i);

//element range (3)	
void splice (const_iterator position, list& x,
             const_iterator first, const_iterator last);
void splice (const_iterator position, list&& x,
             const_iterator first, const_iterator last);

 

Transfer elements from list to list

Transfers elements from x into the container, inserting them at position.
This effectively inserts those elements into the container and removes them from x, altering the sizes of both containers. The operation does not involve the construction or destruction of any element. They are transferred, no matter whether x is an lvalue or an rvalue, or whether the value_type supports move-construction or not.
The first version (1) transfers all the elements of x into the container.
The second version (2) transfers only the element pointed by i from x into the container.
The third version (3) transfers the range [first,last) from x into the container.

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l1 = { 1,2,3 }, l2 = { 10,20,30 };
	list<int>::iterator p1 = l1.begin();
	++p1;
	list<int>::iterator p2 = l2.begin();
#if 0
	l1.splice(p1, l2);
	for (list<int>::iterator i = l1.begin(); i != l1.end(); ++i)
		cout << *i << " ";
	cout << endl;
	if (l2.empty())
		cout << "l2 is empty.\n";
#endif

#if 1
	l1.splice(p1, l2, p2);
	for (list<int>::iterator i = l1.begin(); i != l1.end(); ++i)
		cout << *i << " ";
	cout << endl;
	cout <<"*p2= "<< *p2 << endl;
	++p2;
	cout << "*(++p2)= " << *p2 << endl;
#endif

#if 0
	list<int>::iterator p3 = l2.end();
	--p3;
	l1.splice(p1, l2, p2, p3);
	for (list<int>::iterator i = l1.begin(); i != l1.end(); ++i)
		cout<< *i << " ";
	cout << endl;
	cout << "the size of l2 is: " << l2.size() << endl;
#endif
	return 0;
}

第一段输出:

1 10 20 30 2 3

l2 is empty.

第二段输出:

1 10 2 3

*p2= 10

*(++p2)= 2

可以看出这个“10”是直接被移动过来的

第三段输出:

1 10 20 2 3

the size of l2 is 1

remove:

void remove (const value_type& val);

Remove elements with specific value.

Removes from the container all the elements that compare equal to val. This calls the destructor of these objects and reduces the container size by the number of elements removed.
Unlike member function list::erase, which erases elements by their position (using an iterator), this function (list::remove) removes elements by their value.
A similar function, list::remove_if, exists, which allows for a condition other than an equality comparison to determine whether an element is removed.

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l = { 1,2,3,4,5,6 };
	l.remove(3);
	for (list<int>::iterator i=l.begin(); i != l.end(); ++i)
		cout << *i << " ";
	cout << endl<<l.size()<<endl;
	return 0;
}

输出:

1 2 4 5 6

5

erase:

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

Erase elements

Removes from the list container either a single element (position) or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.
Unlike other standard sequence containers, list and forward_list objects are specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence.

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l = { 1,2,3,4,5,6 };
	list<int>::iterator k = l.begin();
	++k;
	l.erase(k);
	for (list<int>::iterator i=l.begin(); i != l.end(); ++i)
		cout << *i << " ";
	cout << endl<<l.size()<<endl;
	return 0;
}
输出:

1 3 4 5 6

5

swap:

After the call to this member function, the elements in this container are those which were in x before the call, and the elements of x are those which were in this. All iterators, references and pointers remain valid for the swapped objects.

merge:

//(1)
  void merge (list& x);
  void merge (list&& x);
//(2)
template <class Compare>
  void merge (list& x, Compare comp);
template <class Compare>
  void merge (list&& x, Compare comp);

Merge sorted lists

Merges x into the list by transferring all of its elements at their respective ordered positions into the container (both containers shall already be ordered).

This effectively removes all the elements in x (which becomes empty), and inserts them into their ordered position within container (which expands in size by the number of elements transferred). The operation is performed without constructing nor destroying any element: they are transferred, no matter whether x is an lvalue or an rvalue, or whether the value_type supports move-construction or not.

The template versions with two parameters (2), have the same behavior, but take a specific predicate (comp) to perform the comparison operation between elements. This comparison shall produce a strict weak ordering of the elements (i.e., a consistent transitive comparison, without considering its reflexiveness).

This function requires that the list containers have their elements already ordered by value (or by comp) before the call. For an alternative on unordered lists, see list::splice.

Assuming such ordering, each element of x is inserted at the position that corresponds to its value according to the strict weak ordering defined by operator< or comp. The resulting order of equivalent elements is stable (i.e., equivalent elements preserve the relative order they had before the call, and existing elements precede those equivalent inserted from x).

The function does nothing if (&x == this).

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l1 = { 5,4,3,2,1 }, l2 = { 5,2,7,8 };
	list<int>::iterator k = l2.begin();
	l2.sort();
	l1.sort();//if miss this step, there will be an error of "Sequence not ordered"
	l1.merge(l2);
	for (list<int>::iterator i = l1.begin(); i != l1.end(); ++i)
		cout << *i << " ";
	cout << endl << *k << endl;
	return 0;
}

输出:

1 2 2 3 4 5 5 7 8
5

unique:

//(1)
void unique();
//(2)
template <class BinaryPredicate>
  void unique (BinaryPredicate binary_pred);

Remove duplicate values

The version with no parameters (1), removes all but the first element from every consecutive group of equal elements in the container.
Notice that an element is only removed from the list container if it compares equal to the element immediately preceding it. Thus, this function is especially useful for sorted lists.
The second version (2), takes as argument a specific comparison function that determine the "uniqueness" of an element. In fact, any behavior can be implemented (and not only an equality comparison), but notice that the function will call binary_pred(*i,*(i-1)) for all pairs of elements (where i is an iterator to an element, starting from the second) and remove i from the list if the predicate returns true.
The elements removed are destroyed.

#include <iostream>
#include <list>
using namespace std;

bool same_int_part(double first, double second);
int main()
{
	list<double> l = { 1.4, 3.5, 1.63, 1.97 };
	l.unique(same_int_part);
	for (list<double>::iterator i=l.begin(); i != l.end(); ++i)
		cout << *i << " ";
	cout << endl;
	return 0;
}

bool same_int_part(double first, double second)
{
	return (int(first) == int(second));
}

输出:

1.4 3.5 1.63

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值