stl 学习笔记 10

// stl/list1.cpp
#include  < iostream >
#include 
< list >
#include 
< algorithm >
using   namespace  std;

void  PrintList( const  list < int >&  l1, const  list < int >&  l2)
{
    cout 
<<"list1: ";
    copy(l1.begin(),l1.end(),ostream_iterator
<int>(cout," "));
    cout 
<<endl;

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

}


int  main()
{
    list
<int> list1,list2;

    
for(int i = 0; i <6++i)
    
{
        list1.push_back(i);
        list2.push_front(i);
    }


    PrintList(list1,list2);

    
// splice
    /*
    list1 所有的元素移动到list2查找到3的位置前
    
*/

    list2.splice(find(list2.begin(),list2.end(),
3),list1);

    PrintList(list1,list2);

    
//splice
    /*
    list2 begin 的元素放到list2.end()
    
*/

    list2.splice(list2.end(),list2,list2.begin());

    PrintList(list1,list2);

    
// sort
    list2.sort();
    
    list1 
= list2;
    
    
//移除若干相邻的存在的元素
    list2.unique();

    PrintList(list1,list2);

    
//merge
    
//list2的元素全部存到list1,并保持有序
    list1.merge(list2);

    PrintList(list1,list2);
}


// result
/*
list1: 0 1 2 3 4 5
list2: 5 4 3 2 1 0

list1:
list2: 5 4 0 1 2 3 4 5 3 2 1 0
//========            ==========
//======== 插入到这里  ==========

list1:
list2: 4 0 1 2 3 4 5 3 2 1 0 5


list1: 0 0 1 1 2 2 3 3 4 4 5 5
list2: 0 1 2 3 4 5

list1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
list2:
*/

 

set, multiset

 

// stl/set2.cpp

//
#include  < iostream >
#include 
< set >
using   namespace  std;

int  main()
{
    
set<int> c;
    c.insert(
1);
    c.insert(
2);
    c.insert(
4);
    c.insert(
5);
    c.insert(
6);

    
//lower_bound
    cout <<"lower_bound(3) " << *c.lower_bound(3)<<endl;
    
//upper_bound
    cout <<"upper_bound(3) " << *c.upper_bound(3)<<endl;
    
//equla_bound
    cout <<"equal_range(3) " << *c.equal_range(3).first<< " " <<*c.equal_range(3).second<<endl;

    cout 
<<endl;

    cout 
<<"lower_bound(5) " << *c.lower_bound(5)<<endl;
    cout 
<<"upper_bound(5) " << *c.upper_bound(5)<<endl;
    cout 
<<"equal_range(5) " << *c.equal_range(5).first<< " " <<*c.equal_range(5).second<<endl;
}


/*
//result
===============================
lower_bound(3) 4
upper_bound(3) 4
equal_range(3) 4 4

lower_bound(5) 5
upper_bound(5) 6
equal_range(5) 5 6
*/

 

 

// stl/set1.cpp
/*
=====
=====
*/

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

int  main()
{
    
//typedef set
    typedef set<int,greater<int> > IntSet;
    IntSet coll1;

    coll1.insert(
4);
    coll1.insert(
3);
    coll1.insert(
5);
    coll1.insert(
1);
    coll1.insert(
6);
    coll1.insert(
2);
    coll1.insert(
5);

    IntSet::iterator pos;

    
for(pos = coll1.begin(); pos != coll1.end(); ++pos)
        cout 
<<*pos <<' ';
    cout 
<< endl;

    
//pair

    pair
<IntSet::iterator,bool> status = coll1.insert(4);

    
if(status.second)
    
{
        cout 
<<"4 inserteed as element"
            
<<distance(coll1.begin(),status.first) + 1
            
<<endl;
    }

    
else
    
{
        cout 
<<"4 already exists" <<endl;
    }


    
set<int> coll2(coll1.begin(),coll1.end());

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

    coll2.erase(coll2.begin(),coll2.find(
3));

    size_t num 
= coll2.erase(5);

    cout 
<<num <<" element(s) removed" <<endl;

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


/*
result
==========================
6 5 4 3 2 1
4 already exists
1 2 3 4 5 6
1element(s) removed
3 4 6
*/

 

 

// stl/mset1.cpp

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

int  main()
{
    typedef multiset
<int,greater<int> > IntSet;

    IntSet coll1;

    coll1.insert(
4);
    coll1.insert(
3);
    coll1.insert(
5);
    coll1.insert(
1);
    coll1.insert(
6);
    coll1.insert(
2);
    coll1.insert(
5);

    IntSet::iterator pos;

    
for(pos = coll1.begin(); pos != coll1.end();++pos)
        cout 
<<*pos <<' ';
    cout 
<<endl;

    IntSet::iterator ipos 
=coll1.insert(4);

    cout 
<<"4 inserted as element "
         
<<distance(coll1.begin(),ipos)+1
         
<<endl;
     multiset
<int> coll2(coll1.begin(),coll1.end());
    copy(coll2.begin(),coll2.end(),ostream_iterator
<int> (cout," "));
    cout 
<<endl;
    coll2.erase(coll2.begin(),coll2.find(
3));

    
int num = coll2.erase(5);
    cout 
<<num<<" element(s) removed" <<endl;

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


/*
re
==================================
6 5 5 4 3 2 1
4 inserted as element 5
1 2 3 4 4 5 5 6
2 element(s) removed
3 4 4 6
*/

 

 

// stl/setcmp
/*
===============
set 执行期间指定排序准则

===============
*/

#include 
< iostream >
#include 
< set >
#include 
" print.h "
using   namespace  std;

// function object
//
template  < class  T >
class  RuntimeCmp
{
public:
    
enum cmp_mode{normal,reverse};
private:
    cmp_mode mode;
public:
    RuntimeCmp(cmp_mode m 
= normal)
        :mode(m)
    
{}

    
bool operator()(const T& t1,const T& t2)const
    
{
        
return mode == normal ? t1 <t2:t2<t1;
    }


    
bool operator== (const RuntimeCmp& rc)
    
{
        
return mode == rc.mode;
    }

}
;

// IntSet
typedef  set < int ,RuntimeCmp < int >   > IntSet;

void  fill(IntSet &   set );

// main
int  main()
{
    IntSet coll1;
    fill(coll1);
    PRINT_ELEMENTS(coll1,
"coll1: ");

    RuntimeCmp
<int> reverse_order(RuntimeCmp<int>::reverse);
    IntSet coll2(reverse_order);
    fill(coll2);

    PRINT_ELEMENTS(coll2,
"coll2: ");

    coll1 
= coll2;
    coll1.insert(
3);
    PRINT_ELEMENTS(coll1,
"coll1: ");

    
if(coll1.value_comp() == coll2.value_comp())
    
{
        cout 
<<"coll1 and coll2 have some sorting criterion"
            
<<endl;
    }

    
else
    
{
        cout 
<<"coll1 and coll2 have different sorting criterion"
            
<<endl;
    }

}


void  fill(IntSet &   set )
{
    
set.insert(4);
    
set.insert(7);
    
set.insert(5);
    
set.insert(1);
    
set.insert(6);
    
set.insert(2);
    
set.insert(5);
}


/*
result
============================
coll1: 1 2 4 5 6 7
coll2: 7 6 5 4 2 1
coll1: 7 6 5 4 3 2 1
coll1 and coll2 have some sorting criterion
===========================================
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值