stl-list

list


assign
________________________________________

// assign a sequence to the list
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;

int main ()
{
    int ary[]={1,2,3,4,5};
    list<int> l;

    // assign to l the contains of ary
    l.assign(ary,ary+5);

    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    // replace l for 3 copies of 100
    l.assign(3,100);

    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;


    return 0;
}
OUTPUT:
// 1 2 3 4 5
// 100 100 100

 
back
________________________________________

// returns the last element
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;

template<class T, class D>
class Member
{
    public:
        Member(T t, D d) : name(t), sal(d) {}
        void print();
    private:
        T name;
        D sal;
};
template<class T, class D>
void Member<T,D>::print()
{
    cout << name << "  " << sal << endl;
}
//--------------------------------------
int main ()
{
    typedef Member<string,double> M;
    list<M> l;

    l.push_back(M("Robert",60000));
    l.push_back(M("Linda",75000));

    list<M>::iterator It = l.begin();
    cout << "Entire list:" << endl;

    while ( It != l.end() )
        (It++)->print();
    cout << endl;

    cout << "Return from back()" << endl;
    l.back().print();
    return 0;
}
OUTPUT:
// Entire list:
// Robert  60000
// Linda  75000
//
// Return from back()
// Linda  75000
 

begin
________________________________________

// returns an iterator to the beginning
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
#include <numeric>
using namespace std;

int main ()
{
    list<int> l(5);
    iota(l.begin(),l.end(),1);

    list<int>::iterator It = l.begin();
    while ( It != l.end() )
        cout << *It++ << " ";
    cout << endl;

    // third element of the list
    It = l.begin()+2;
    cout << *It << endl;

    return 0;
}
OUTPUT:
// 1 2 3 4 5
// 3


clear
________________________________________

// removes all elements
#include <iostream>
#include <list>
using namespace std;

int main ()
{
    list<int> l(5,10);
    cout << "Size of list = "
         << l.size() << endl;

    l.clear();
    cout << "After l.clear() size of list = "
         << l.size() << endl;

    return 0;
}
OUTPUT:
// Size of list = 5
// After l.clear() size of list = 0
 

empty
________________________________________

// true if the list is empty 
#include <iostream>
#include <list>
using namespace std;

int main ()
{
    list<int> l;

    cout << "List is ";
    l.empty() ? cout << "" : cout << "not ";
    cout << "empty" << endl;

    l.push_back(100);

    cout << "List is ";
    l.empty() ? cout << "" : cout << "not ";
    cout << "empty" << endl;

    return 0;
}
OUTPUT:
// List is empty
// List is not empty


end
________________________________________

// returns an iterator to the end
#include <iostream>
#include <list>
#include <numeric>
using namespace std;

int main () 
{
    list<int> li(10);
    iota(li.begin(),li.end(),1);

    list<int>::iterator It = li.begin();
    while ( It != li.end() )
        cout << *(It++) << " ";
    cout << endl;

    return 0;
}
OUTPUT:
// 1 2 3 4 5 6 7 8 9 10 


erase
________________________________________

// erase an elemen
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;

template <class T>
void print (list<T>& l)
{
    list<int>::iterator It = l.begin();
    while ( It != l.end() )
    {
        cout << *(It++) << " ";
    }
    cout << endl;
}
//=====================
int main () 
{
    list<int> li(10);
    iota(li.begin(),li.end(),1);

    print(li);
	
    list<int>::iterator It;
    It = find(li.begin(),li.end(),6);

    // erase at the pos It
    li.erase(It);
    print(li);
	
    It = find(li.begin(),li.end(),4);
	
    // erase from beginning to the pos It
    li.erase(li.begin(),It);
    print(li);

    return 0;
}
OUTPUT:
// 1 2 3 4 5 6 7 8 9 10 
// 1 2 3 4 5 7 8 9 10 
// 4 5 7 8 9 10 


front
________________________________________

// returns the first element
#include <iostream>
#include <list>

int main () 
{
    int ary[] = {1,2,3,4,5};
    list li;
	
    for ( int i=0; i<5; i++ )
    {
        li.push_front(ary[i]);	
        cout << "front() : " 
             << li.front() << endl;
    }

    return 0;
}
OUTPUT:
// front() : 1
// front() : 2
// front() : 3
// front() : 4
// front() : 5


insert
________________________________________

// insert elements into the list
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;

template <class T>
void print (list<T>& l)
{
    list<int>::iterator It = l.begin();
    while ( It != l.end() )
    {
        cout << *(It++) << " ";
    }
    cout << endl;
}
//====================================
int main () 
{
    list<int> li1(10,0);
    list<int> li2(5);
    list<int>::iterator It;
    iota(li2.begin(),li2.end(),1);

    cout << "li1 : ";
    print(li1);
    cout << "li2 : ";
    print(li2);

    It = li1.begin();
    // value of 20 at the pos It
    li1.insert(++It,20);
    cout << "li1 : ";
    print(li1);
    
    // two value of 25 at the beginning
    li1.insert(li1.begin(),2,25);
    cout << "li1 : ";
    print(li1);

    // contents of li2 at the end of li1
    li1.insert(li1.end(),li2.begin(),li2.end());
    cout << "li1 : ";
    print(li1);

    return 0;
}
OUTPUT:
// li1 : 0 0 0 0 0 0 0 0 0 0 
// li2 : 1 2 3 4 5 
// li1 : 0 20 0 0 0 0 0 0 0 0 0 
// li1 : 25 25 0 20 0 0 0 0 0 0 0 0 0 
// li1 : 25 25 0 20 0 0 0 0 0 0 0 0 0 1 2 3 4 5 


max_size
________________________________________

// returns the maximum number of elements the list can hold
#include <iostream>
#include <list>

int main () 
{
    list li(10);

    cout << "size() of li = "
     << li.size() << endl;
    cout << "max_size     = "
     << li.max_size() << endl;
    return 0;
}
OUTPUT:
// size() of li = 10
// max_size     = 4294967295


merge
________________________________________

// merge two lists
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;

int main () 
{
    int ary[] = {2,5,9,7,2,7,6,5};
    list<int> list1(ary,ary+4);
    list<int> list2(ary+4,ary+8);

    cout << "list1 : ";
    copy(list1.begin(),list1.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    cout << "list2 : ";
    copy(list2.begin(),list2.end(),
            ostream_iterator<int>(cout," "));
    cout << endl << endl;

    // you have to sort data before megring it
    list1.sort();
    list2.sort();
    list1.merge(list2);

    cout << "After \"list1.merge(list2)\" :" << endl;
    cout << "list1 : ";
    copy(list1.begin(),list1.end(),
            ostream_iterator(cout," "));
    cout << endl;

    cout << "size of list2 = " << list2.size()
         << endl;
    cout << "list2 is " << (list2.empty() ? "" : "not ")
         << "empty" << endl;

    return 0;
}
OUTPUT:
// list1 : 2 5 9 7 
// list2 : 2 7 6 5 
// 
// After "list1.merge(list2)" :
// list1 : 2 2 5 5 6 7 7 9 
// size of list2 = 0
// list2 is empty


pop_back
________________________________________

// removes the last element
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;

int main () 
{
    list<int> l(5);
    iota(l.begin(),l.end(),1);

    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    while ( !l.empty() )
    {
        l.pop_back();

    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
        cout << endl;
    }
    return 0;
}
OUTPUT:
// 1 2 3 4 5 
// 1 2 3 4 
// 1 2 3 
// 1 2 
// 1 


pop_front
________________________________________

// removes the first element
#include <iostream>
#include <list>
#include <algorithm>

int main () 
{
    list<int> l(5,0);
    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    cout << "Size of list = "
         << l.size() << endl;

    int size = l.size();
	 
    while ( !l.empty() )
    {
        l.pop_front();
        cout << "Size of list = "
             << l.size() << endl;
    }

    return 0;
}
OUTPUT:
// 0 0 0 0 0 
// Size of list = 5
// Size of list = 4
// Size of list = 3
// Size of list = 2
// Size of list = 1
// Size of list = 0
 

push_back
________________________________________

// add an element to the end of the list
#include <iostream>
#include <list>
#include <iomanip>
#include <string>
using namespace std;

template <class T>
class Name
{
    public:
        Name(T f, T l) : first(f), last(l) {}
        void print()
        {
            cout.setf(ios::left);
            cout << setw(15) << first.c_str()
                 << last << endl;
        }
    private:
        T first, last;
};

//==========================================
int main ()
{
    typedef Name<string> N;
    typedef list<N> L;
    L l;
    L::iterator It;

    N n1(string("Albert"),string("Johnson"));
    N n2("Lana","Vinokur");

    l.push_back(n1);
    l.push_back(n2);

    // unnamed object
    l.push_back(N("Linda","Bain"));

    It = l.begin();
    while ( It != l.end() )
        (It++)->print();
    cout << endl;

    return 0;
}
OUTPUT:
// Albert         Johnson
// Lana           Vinokur
// Linda          Bain


push_front
________________________________________

// add an element to the front of the list
#include <iostream>
#include <list>
#include <iomanip>
#include <string>
using namespace std;

template <class T>
class Name
{
    public:
        Name(T f, T l) : first(f), last(l) {}
        void print()
        {
            cout.setf(ios::left);
            cout << setw(15) << first.c_str()
                 << last << endl;
        }
    private:
        T first, last;
};

//==========================================
int main ()
{
    typedef Name<string> N;
    typedef list<N> L;
    L l;
    L::iterator It;

    N n1(string("Albert"),string("Johnson"));
    N n2("Lana","Vinokur");

    l.push_front(n1);
    l.push_front(n2);

    // unnamed object
    l.push_front(N("Linda","Bain"));

    It = l.begin();
    while ( It != l.end() )
        (It++)->print();
    cout << endl;

    return 0;
}
OUTPUT:
// Linda          Bain
// Lana           Vinokur
// Albert         Johnson 


rbegin
________________________________________

// returns a reverse iterator to the beginning of the list
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;

int main () 
{
    list<int> l(10);
    iota(l.begin(),l.end(),1);

    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    list<int>::reverse_iterator It = l.rbegin();
    while ( It != l.rend() )
    cout << *(It++) << " ";
    cout << endl;

    return 0;
}
OUTPUT:
// 1 2 3 4 5 6 7 8 9 10 
// 10 9 8 7 6 5 4 3 2 1 


remove
________________________________________

// removes elements from the list
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;

template <class T, class D>
class Salary
{
    public:
        Salary(T t) : id(t) {}
        Salary(T t,D d) : id(t), sal(d) {}
        void print ()
        { cout << id << "  " << sal << endl; }
    private:
        T id;
        D sal;
    friend bool operator == 
        (const Salary& s1,const Salary& s2)
    { return s1.id == s2.id; }
};
//==========================================
int main () 
{
    typedef Salary<string,double> S;
    typedef list<S> L;

    L l;
    l.push_back(S("012345",70000.0));
    l.push_back(S("012346",60000.0));
    l.push_back(S("012347",72000.0));

    L::iterator It = l.begin();
    while ( It != l.end() )
        (It++)->print();
    cout << endl;

    S s("012345");
    l.remove(s);
        
    It = l.begin();
    while ( It != l.end() )
        (It++)->print();
    cout << endl;

    return 0;
}
OUTPUT:
// 012345  70000
// 012346  60000
// 012347  72000
// 
// 012346  60000
// 012347  72000


remove_if
________________________________________

// removes elements conditionally
#include <iostream>
#include <list>
#include <algorithm>
OUTPUT:
 

rend
________________________________________

// returns a reverse iterator to the start of the list
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;

int main () 
{
    list<int> l(10);
    iota(l.begin(),l.end(),1);

    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    list<int>::reverse_iterator It = l.rbegin();
    while ( It != l.rend() )
    cout << *(It++) << " ";
    cout << endl;

    return 0;
}
OUTPUT:
// 1 2 3 4 5 6 7 8 9 10 
// 10 9 8 7 6 5 4 3 2 1 
 

resize
________________________________________

// change the size of the list
#include <iostream>
#include <list>

int main () 
{
    list<int> l(10);

    cout << "Size of list l = "
         << l.size();

    l.resize(100);
    cout << "After l.resize(100)" << endl;
    cout << "Size of list l = "
         << l.size();
	
    l.resize(5);
    cout << "After l.resize(5)" << endl;
    cout << "Size of list l = "
         << l.size();

    return 0;
}
OUTPUT:
// Size of list l = 10After l.resize(100)
// Size of list l = 100After l.resize(5)
// Size of list l = 5


reverse
________________________________________

// reverse the list
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;

int main () 
{
    list<int> l(10);
    iota(l.begin(),l.end(),1);

    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    l.reverse();
    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    return 0;
}
OUTPUT:
// 1 2 3 4 5 6 7 8 9 10 
// 10 9 8 7 6 5 4 3 2 1 


size
________________________________________

// the number the elements in the list
#include <iostream>
#include <list>
#include <algorithm>

int main () 
{
    list<int> l(5,0);
    copy(l.begin(),l.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    cout << "Size of list = "
         << l.size() << endl;

    int size = l.size();
	 
    for ( int i=0; i<size; i++ )
    // or while ( !l.empty() ) - safer
    {
        l.pop_front();
        cout << "Size of list = "
             << l.size() << endl;
    }

    return 0;
}
OUTPUT:
// 0 0 0 0 0 
// Size of list = 5
// Size of list = 4
// Size of list = 3
// Size of list = 2
// Size of list = 1
// Size of list = 0

 

sort 1.
________________________________________

// sorts the list
#include <iostream>
#include <list>
#include <algorithm>
#include <functional>
using namespace std;

template <class T>
class Print
{
    public:
        void operator () (T& t)
        
            cout << t << " ";
        }
};
//-----------------------------
int main () 
{
    int ary[] = {3,2,5,7,3,6,7,2,4,5};
    list<int> li(ary,ary+10);
    Print<int> print;

    cout << "Before sorting\nli : ";
    for_each(li.begin(),li.end(),print);
    cout << endl << endl;

    li.sort(greater<int>());

    cout << "After li.sort(greater())\nli : ";
    for_each(li.begin(),li.end(),print);
    cout << endl << endl;

    li.sort(less<int>());

    cout << "After li.sort(less())\nli : ";
    for_each(li.begin(),li.end(),print);
    cout << endl;

    return 0;
}
OUTPUT:
// Before sorting
// li : 3 2 5 7 3 6 7 2 4 5 
// 
// After li.sort(greater<int>())
// li : 7 7 6 5 5 4 3 3 2 2 
// 
// After li.sort(less<int>())
// li : 2 2 3 3 4 5 5 6 7 7 


sort 2.
________________________________________

// sorts with user datatype
#include <iostream>
#include <iomanip>
#include <list>
#include <string>
using namespace std;

template <class T>
class Member
{
    public:
        Member(T f, T l) :
            first_n(f), last_n(l) {}
        void print();
    private:
        string last_n, first_n;
    // for sort() list member function
    friend bool operator < (Member& m1,
            Member& m2)
    { return m1.last_n < m2.last_n; }
};
//---------------------------------------
template <class T>
void Member<T>::print()
{
    cout.setf(ios::left);
    cout << setw(15) << last_n.c_str()
         << first_n << endl;
}

typedef Member<string> M;
//========================================
int main () 
{
    list<M> li;
    li.push_back(M("Linda","Smith"));
    li.push_back(M("Frost","Robert"));
    li.push_back(M("Alex","Amstrong"));

    cout << "Before sorting by last name:\n"
     << "============================"
     << endl;

    list<M>::iterator It = li.begin();
    while ( It != li.end() )
    {
        (It++)->print();
    }
    cout << endl;

    li.sort();

    cout << "After sorting by last name:\n"
     << "============================"
     << endl;

    It = li.begin();
    while ( It != li.end() )
    {
        (It++)->print();
    }

	return 0;
}
OUTPUT:
// Before sorting by last name:
// ============================
// Smith          Linda
// Robert         Frost
// Amstrong       Alex
// 
// After sorting by last name:
// ============================
// Amstrong       Alex
// Robert         Frost
// Smith          Linda


splice
________________________________________

// merge two lists 
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;

template <class T>
class Print
{
    public:
        void operator () (T& t)
        {
            cout << t << " ";
        }
};
//====================================
int main ()
{
    ist<int> li1, li2, li3, li4;
    Print print;

    for ( int i=0; i<5; i++ )
    {
        li1.push_back(i);
        li2.push_back(i+5);
        li3.push_back(i+10);
        li4.push_back(i+15);
    }

    cout << "li1 : ";
    for_each(li1.begin(),li1.end(),print);
    cout << endl;

    cout << "li2 : ";
    for_each(li2.begin(),li2.end(),print);
    cout << endl;

    cout << "li3 : ";
    for_each(li3.begin(),li3.end(),print);
    cout << endl;

    cout << "li4 : ";
    for_each(li4.begin(),li4.end(),print);
    cout << endl << endl;

    li1.splice(li1.end(),li2);

    cout << "li1 : ";
    for_each(li1.begin(),li1.end(),print);
    cout << endl << endl;

    li1.splice(li1.end(),li3,li3.begin(),li3.end());

    cout << "li1 : ";
    for_each(li1.begin(),li1.end(),print);
    cout << endl << endl;

    list<int>::iterator It;
    It = find(li4.begin(),li4.end(),18);

    li1.splice(li1.begin(),li4,It);
    cout << "li1 : ";
    for_each(li1.begin(),li1.end(),print);
    cout << endl;

    cout << "li4 : ";
    for_each(li4.begin(),li4.end(),print);
    cout << endl;

    return 0;
}
OUTPUT:
// li1 : 0 1 2 3 4 
// li2 : 5 6 7 8 9 
// li3 : 10 11 12 13 14 
// li4 : 15 16 17 18 19 
// 
// li1 : 0 1 2 3 4 5 6 7 8 9 
// 
// li1 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
// 
// li1 : 18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
// li4 : 15 16 17 19 


swap
________________________________________

// exchange two lists 
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;

void print (list<int>& l)
{
    list<int>::iterator It = l.begin();
    while ( It != l.end() )
    {
        cout << *(It++) << " ";
    }
    cout << endl;
}
//===============================
int main () 
{
    list<int> li1(5), li2(5);
    iota(li1.begin(),li1.end(),1);
    iota(li2.begin(),li2.end(),5);

    cout << "li1 : ";
    print(li1);
    cout << "li2 : ";
    print(li2);

    li1.swap(li2);
	
    cout << endl <<"After swapping:" << endl;
    cout << "li1 : ";
    print(li1);
    cout << "li2 : ";
    print(li2);

    return 0;
}
OUTPUT:
// li1 : 1 2 3 4 5 
// li2 : 5 6 7 8 9 
//
// After swapping:
// li1 : 5 6 7 8 9 
// li2 : 1 2 3 4 5 


unique
________________________________________

// removes duplicate elements
#include <iostream>
#include <list>
#include <algorithm>
#include <iomanip>
#include <string>
using namespace std;

template <class T>
class Member
{
    public:
        Member(T f, T l) :
            first_n(f), last_n(l) {}
        void print();
    private:
        string last_n, first_n;
    // for sort function
    friend bool operator < (Member& m1,
            Member& m2)
    { return m1.last_n < m2.last_n; }

    // for merge and unique functions 
    friend bool operator == (Member& m1,
            Member& m2)
    { return m1.last_n == m2.last_n; }
};
//---------------------------------------
template <class T>
void Member<T>::print()
{
    cout.setf(ios::left);
    cout << setw(15) << last_n.c_str()
         << first_n << endl;
}

typedef Member<string> M;
//========================================
int main () 
{
    list<M> li1;
    li1.push_back(M("Linda","Smith"));
    li1.push_back(M("Robert","Frost"));
    li1.push_back(M("Alex","Amstrong"));

    list li2;
    li2.push_back(M("Linda","Smith"));
    li2.push_back(M("John","Wood"));
    li2.push_back(M("Alex","Amstrong"));

    li1.sort();
    li2.sort();
    li1.merge(li2);
    
    cout << "li1 after sorting and mergin"
         << endl;

    list<M>::iterator It = li1.begin();
    while ( It != li1.end() )
    {
        (It++)->print();
    }
    cout << endl;
    
    li1.unique();

    cout << "After li1.unique()" << endl;

    It = li1.begin();
    while ( It != li1.end() )
    {
        (It++)->print();
    }
    cout << endl;
    
    return 0;
}
OUTPUT:
// li1 after sorting and mergin
// Amstrong       Alex
// Amstrong       Alex
// Frost          Robert
// Smith          Linda
// Smith          Linda
// Wood           John
// 
// After li1.unique()
// Amstrong       Alex
// Frost          Robert
// Smith          Linda
// Wood           John


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值