STLのList
#include <list>
list
constructor
destructor
operator=
assign
front
back
begin
(iterator)
end
rbegin
(reverse_iterator or const)
rend
push_back
push_front
pop_back
pop_back()
pop_front
clear
erase
( )
remove
( )
remove_if
( ),
empty
max_size
size
resize
( )
reverse
sort
,
merge
splice
( )
insert
( )
swap
( )
unique
list( );
explicit list(
const Allocator& _Al
);
explicit list(
size_type _Count
);
list(
size_type _Count,
const Type& _Val
);
list(
size_type _Count,
const Type& _Val,
const Allocator& _Al
);
list(
const list<Type, Allocator>& _Right
);
template<class InputIterator>
list(
InputIterator _First,
InputIterator _Last
);
template<class InputIterator >
list(
InputIterator _First,
InputIterator _Last,
const Allocator& _Al
);
:
#include "stdafx.h"
#include <list>
#include <iostream>
void main( )
{
using namespace std;
list <int>::iterator c1_Iter, c2_Iter, c3_Iter, c4_Iter, c5_Iter, c6_Iter;
// Create an empty list c0
list <int> c0;//
// Create a list c1 with 3 elements of default value 0
list <int> c1( 3 );
// Create a list c2 with 5 elements of value 2 , 2
list <int> c2( 5, 2 );
// Create a list c3 with 3 elements of value 1 and with the
// allocator of list c2
list <int> c3( 3, 1, c2.get_allocator( ) );
// Create a copy, list c4, of list c2 L2 copy
list <int> c4(c2);
// Create a list c5 by copying the range c4[_First, _Last) c4
c4_Iter = c4.begin( );
c4_Iter++;
c4_Iter++;
list <int> c5( c4.begin( ), c4_Iter );
// Create a list c6 by copying the range c4[_First, _Last) and with
// the allocator of list c2
c4_Iter = c4.begin( );
c4_Iter++;
c4_Iter++;
c4_Iter++;
list <int> c6( c4.begin( ), c4_Iter, c2.get_allocator( ) );
cout << "c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
cout << "c2 =";
for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
cout << " " << *c2_Iter;
cout << endl;
cout << "c3 =";
for ( c3_Iter = c3.begin( ); c3_Iter != c3.end( ); c3_Iter++ )
cout << " " << *c3_Iter;
cout << endl;
cout << "c4 =";
for ( c4_Iter = c4.begin( ); c4_Iter != c4.end( ); c4_Iter++ )
cout << " " << *c4_Iter;
cout << endl;
cout << "c5 =";
for ( c5_Iter = c5.begin( ); c5_Iter != c5.end( ); c5_Iter++ )
cout << " " << *c5_Iter;
cout << endl;
cout << "c6 =";
for ( c6_Iter = c6.begin( ); c6_Iter != c6.end( ); c6_Iter++ )
cout << " " << *c6_Iter;
cout << endl;
}
get_allocator //
A type that represents the allocator class for a list object.
list <int> c1;
list <int, allocator<int> > c2 = list <int, allocator<int> >( allocator<int>( ) );
// c3 will use the same allocator class as c1
list <int> c3( c1.get_allocator( ) );
list<int>::allocator_type xlst = c1.get_allocator( );
csdn
2. assign() ,
L1.assign(4,3); // L1(3,3,3,3)
L1.assign(++list1.beging(), list2.end()); // L1(2,3)
3.operator=
L1 = list1; // L1(1,2,3)
4. front()
int nRet = list1.front() // nRet = 1
5. back()
int nRet = list1.back() // nRet = 3
6. begin() (iterator)
it = list1.begin(); // *it = 1
7. end() (list end()=begin())
it = list1.end();
--it; // *it = 3
8.rbegin() (reverse_iterator or const)
list<int>::reverse_iterator it = list1.rbegin(); // *it = 3
9. rend()
list<int>::reverse_iterator it = list1.rend(); // *(--riter) = 1
10.push_back()
list1.push_back(4) // list1(1,2,3,4)
11. push_front()
list1.push_front(4) // list1(4,1,2,3)
12. pop_back()
list1.pop_back() // list1(1,2)
13.pop_front()
list1.pop_front() // list1(2,3)
14.clear()
list1.clear(); // list1 ,list1.size() =0
15.erase() ( )
list1.erase(list1.begin()); // list1(2,3)
list1.erase(++list1.begin(),list1.end()); // list1(1)
16. remove() ( )
list L1(4,3,5,1,4)
L1.remove(4); // L1(3,5,1);
17.remove_if() ( ),
// 2
bool myFun(const int& value) { return (value < 2); }
list1.remove_if(myFun); // list1(3)
18.empty()
bool bRet = L1.empty(); // L1 ,bRet = true, bRet = false。
19.max_size()
list<int>::size_type nMax = list1.max_size();// nMax = 1073741823
20.size()
list<int>::size_type nRet = list1.size(); // nRet = 3
21.resize() ( )
list1.resize(5) // list1 (1,2,3,0,0)
list1.resize(5,4) // list1 (1,2,3,4,4)
22.reverse() :
list1.reverse(); // list1(3,2,1)
23.sort() , ( )
list L1(4,3,5,1,4)
L1.sort(); // L1(1,3,4,4,5)
L1.sort(greater<int>()); // L1(5,4,4,3,1)
24.merge()
//
list1.merge(list2); // list1(1,2,3,4,5,6) list2
//
L1(3,2,1), L2(6,5,4)
L1.merge(L2, greater<int>()); // list1(6,5,4,3,2,1) list2
25.splice() ( )
list1.splice(++list1.begin(),list2);
// list1(1,4,5,6,2,3) list2
list1.splice(++list1.begin(),list2,list2.begin());
// list1(1,4,2,3); list2(5,6)
list1.splice(++list1.begin(),list2,++list2.begin(),list2.end());
//list1(1,5,6,2,3); list2(4)
26.insert() ( )
list1.insert(++list1.begin(),9); // list1(1,9,2,3)
list1.insert(list1.begin(),2,9); // list1(9,9,1,2,3);
list1.insert(list1.begin(),list2.begin(),--list2.end());//list1(4,5,1,2,3);
27.swap() ( )
list1.swap(list2); // list1(4,5,6) list2(1,2,3)
28. unique()
L1(1,1,4,3,5,1)
L1.unique(); // L1(1,4,3,5,1)
#include "stdafx.h"
// -------------------------------------------------------------------------
// : STL list , 。//
// -------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;
list<int> g_list1;
list<int> g_list2;
////////////////////////////////////////////////////////////////////////////
void InitList(){
// push_back()
g_list1.push_back(1);
g_list1.push_back(2);
g_list1.push_back(3);
// push_front()
g_list2.push_front(6);
g_list2.push_front(5);
g_list2.push_front(4);}
//
void ShowList(list<int>& listTemp)
{ // size()
cout << listTemp.size() << endl;
for (list<int>::iterator it = listTemp.begin(); it != listTemp.end(); ++it)
{ cout << *it << ' ';
}
cout << endl;
}
//////////////////////////////////////////////////////////////////////////// ,
void constructor_test0()
{ list<int> listTemp;
cout << listTemp.size() << endl;
}
// , 0
void constructor_test1()
{ list<int> listTemp(3);
ShowList(listTemp);
}
// , , 1
void constructor_test2()
{ list<int> listTemp(5, 1);
ShowList(listTemp);
}
// , g_list1 copy
void constructor_test3()
{ list<int> listTemp(g_list1);
ShowList(listTemp);
}
// ,listTemp g_list1 [_First, _Last)
void constructor_test4()
{
list<int> listTemp(g_list1.begin(), g_list1.end());
ShowList(listTemp);
}// assign() ,
// template <class InputIterator>
// void assign ( InputIterator first, InputIterator last );
// void assign ( size_type n, const T& u );
void assign_test()
{
list<int> listTemp(5, 1);
ShowList(listTemp);
listTemp.assign(4, 3);
ShowList(listTemp);
listTemp.assign(++g_list1.begin(), g_list1.end());
ShowList(listTemp);}
// operator=
void operator_equality_test()
{
g_list1 = g_list2;
ShowList(g_list1);
ShowList(g_list2);
}
// front()
void front_test7()
{
cout << g_list1.front() << endl;
}
// back()
void back_test()
{
cout << g_list1.back() << endl;
}
// begin() (iterator)
void begin_test()
{
list<int>::iterator it1 = g_list1.begin();
cout << *++it1 << endl;
list<int>::const_iterator it2 = g_list1.begin();
it2++; // (*it2)++; // *it2 const
cout << *it2 << endl;
}
// end() [ ] (list end()= begin())
void end_test(){
list<int>::iterator it = g_list1.end();
// :
--it;
cout << *it << endl;
}
// rbegin()
void rbegin_test()
{
list<int>::reverse_iterator it = g_list1.rbegin();
for (; it != g_list1.rend(); ++it)
{
cout << *it << ' ';
}
cout << endl;
}
// rend()
void rend_test()
{
list<int>::reverse_iterator it = g_list1.rend();
--it;
cout << *it << endl;
}
// push_back()
void push_back_test()
{
ShowList(g_list1);
g_list1.push_back(4);
ShowList(g_list1);
}
// push_front()
void push_front_test()
{
ShowList(g_list1);
g_list1.push_front(4);
ShowList(g_list1);
}
// pop_back()
void pop_back_test()
{
ShowList(g_list1);
cout << endl;
g_list1.pop_back();
ShowList(g_list1);
}
// pop_front()
void pop_front_test()
{
ShowList(g_list1);
cout << endl;
g_list1.pop_front();
ShowList(g_list1);
}
// clear()
void clear_test()
{
ShowList(g_list1);
g_list1.clear();
ShowList(g_list1);
}
// erase() ( )
void erase_test()
{
ShowList(g_list1);
g_list1.erase(g_list1.begin());
ShowList(g_list1);
cout << endl;
ShowList(g_list2);
g_list2.erase(++g_list2.begin(), g_list2.end());
ShowList(g_list2);
}
// remove() ( )
void remove_test()
{
ShowList(g_list1);
g_list1.push_back(1);
ShowList(g_list1);
g_list1.remove(1);
ShowList(g_list1);
}
bool myFun(const int& value)
{
return (value < 2);
}
// remove_if() ( )
void remove_if_test()
{
ShowList(g_list1);
g_list1.remove_if(myFun);
ShowList(g_list1);
}
// empty()
void empty_test()
{
list<int> listTemp;
if (listTemp.empty())
cout << "listTemp " << endl;
else
cout << "listTemp " << endl;
}
//max_size() :1073741823
void max_size_test()
{
list<int>::size_type nMax = g_list1.max_size();
cout << nMax << endl;
}
// resize() ( ):
void resize_test()
{
ShowList(g_list1);
g_list1.resize(9); //
ShowList(g_list1);
cout << endl;
ShowList(g_list2);
g_list2.resize(9, 51); //
ShowList(g_list2);
}
// reverse()
void reverse_test()
{
ShowList(g_list1);
g_list1.reverse();
ShowList(g_list1);
}
// sort() , ( )
void sort_test()
{
list<int> listTemp;
listTemp.push_back(9);
listTemp.push_back(3);
listTemp.push_back(5);
listTemp.push_back(1);
listTemp.push_back(4);
listTemp.push_back(3);
ShowList(listTemp);
listTemp.sort();
ShowList(listTemp);
listTemp.sort(greater<int>()); //less<int>()
ShowList(listTemp);
}
// merge() .
void merge_test1()
{
list<int> listTemp2;
listTemp2.push_back(3);
listTemp2.push_back(4);
list<int> listTemp3;
listTemp3.push_back(9);
listTemp3.push_back(10);
ShowList(listTemp2);
cout << endl;
ShowList(listTemp3);
cout << endl;
listTemp2.merge(listTemp3);
ShowList(listTemp2);
}
bool myCmp (int first, int second)
{
return ( int(first)>int(second) );
}
// merge() .
void merge_test2()
{
list<int> listTemp2;
listTemp2.push_back(4);
listTemp2.push_back(3);
list<int> listTemp3;
listTemp3.push_back(10);
listTemp3.push_back(9);
ShowList(listTemp2);
cout << endl;
ShowList(listTemp3);
cout << endl;
// listTemp2.merge(listTemp3, greater<int>()); //
listTemp2.merge(listTemp3, myCmp);
ShowList(listTemp2);
}
// splice() ( ),
// void splice ( iterator position, list<T,Allocator>& x );
// void splice ( iterator position, list<T,Allocator>& x, iterator i );
// void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last );
void splice_test()
{
list<int> listTemp1(g_list1);
list<int> listTemp2(g_list2);
ShowList(listTemp1);
ShowList(listTemp2);
cout << endl;
// listTemp1.splice(++listTemp1.begin(), listTemp2);
ShowList(listTemp1); ShowList(listTemp2);
// listTemp1.assign(g_list1.begin(), g_list1.end());
listTemp2.assign(g_list2.begin(), g_list2.end());
listTemp1.splice(++listTemp1.begin(), listTemp2, ++listTemp2.begin());
ShowList(listTemp1); ShowList(listTemp2);
// listTemp1.assign(g_list1.begin(), g_list1.end());
listTemp2.assign(g_list2.begin(), g_list2.end());
listTemp1.splice(++listTemp1.begin(), listTemp2, ++listTemp2.begin(), listTemp2.end());
ShowList(listTemp1);
ShowList(listTemp2);
}
// insert() ( )
// iterator insert ( iterator position, const T& x );
// void insert ( iterator position, size_type n, const T& x );
// template <class InputIterator>
// void insert ( iterator position, InputIterator first, InputIterator last );
void insert_test()
{
list<int> listTemp1(g_list1);
ShowList(listTemp1);
listTemp1.insert(listTemp1.begin(), 51);
ShowList(listTemp1);
cout << endl; list<int> listTemp2(g_list1);
ShowList(listTemp2);
listTemp2.insert(listTemp2.begin(), 9, 51);
ShowList(listTemp2);
cout << endl;
list<int> listTemp3(g_list1);
ShowList(listTemp3);
listTemp3.insert(listTemp3.begin(), g_list2.begin(), g_list2.end());
ShowList(listTemp3);
}
// swap() ( )
void swap_test()
{
ShowList(g_list1);
ShowList(g_list2);
cout << endl;
g_list1.swap(g_list2);
ShowList(g_list1);
ShowList(g_list2);
}
bool same_integral_part (double first, double second)
{
return ( int(first)==int(second) );
}
// unique()
void unique_test()
{
list<int> listTemp;
listTemp.push_back(1);
listTemp.push_back(1);
listTemp.push_back(4);
listTemp.push_back(3);
listTemp.push_back(5);
listTemp.push_back(1);
list<int> listTemp2(listTemp);
ShowList(listTemp);
listTemp.unique();
//
ShowList(listTemp);
cout << endl;
listTemp.sort();
ShowList(listTemp);
listTemp.unique();
ShowList(listTemp);
cout << endl;
listTemp2.sort();
ShowList(listTemp2);
listTemp2.unique(same_integral_part);
ShowList(listTemp2);
}
// ,
int _tmain(int argc, _TCHAR* argv[])
{
InitList();
ShowList(g_list1);
// ShowList(g_list2);
// constructor_test0();
// constructor_test1();
// constructor_test2();
// constructor_test3();
// constructor_test4();
// assign_test();
// operator_equality_test();
// front_test7();
// back_test();
// begin_test();
// end_test();
// rbegin_test();
// rend_test();
// push_back_test();
// push_front_test();
// pop_back_test();
// pop_front_test();
// clear_test();
// erase_test();
// remove_test();
// remove_if_test();
// empty_test();
// max_size_test();
// resize_test();
// reverse_test();
// sort_test();
// merge_test1();
// merge_test2();
// splice_test();
// insert_test();
// swap_test();
// unique_test();
getchar();
return 0;
}