STL算法 ------- 区间的比较

1. equal(b, e, b2)                     比较第一个容器和第二个容器内容是否相等

2. equal( b, e, b2, p )

3. mismatch( b, e, b2 )            查找两个容器中第一个不相等的数据

4. mismatch(b, ,e, b2, p )

5. lexicographical_compare( b, e, b2, e2 )            检查第一个区间是否比第二个区间小

6. lexicographical_compare( b, e, b2, e2, p)

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

bool BothEvenOrOdd( int elem1, int elem2 )
{
	return elem1%2 == elem2 %2;
}

int main( int argc, char** argv )
{
	vector<int> vec;
	list<int> lst;

	for( int i=1; i<=7; ++i )
	{
		vec.insert(vec.end(), i);
	}
	for( int i=3; i<=9; ++i )
	{
		lst.push_back(i);
	}

	for(vector<int>::const_iterator citr=vec.begin(); citr!=vec.end(); ++citr )
	{
		cout<<*citr<<' ';
	}cout<<endl;
	for(list<int>::const_iterator citr=lst.begin(); citr!=lst.end(); ++citr)
	{
		cout<<*citr<<' ';
	}cout<<endl;

	if( equal(vec.begin(), vec.end(), lst.begin()) )
	{
		cout<<"equal "<<endl;
	}else{
		cout<<"not equal"<<endl;
	}

	if( equal(vec.begin(), vec.end(), lst.begin(), BothEvenOrOdd) )
	{
		cout<<"奇数对应奇数,偶数对应偶数 "<<endl;
	}else{
		cout<<"not 奇数对应奇数,偶数对应偶数 "<<endl;
	}
			

	return 0;
}

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

bool BothEvenOrOdd( int elem1, int elem2 )
{
	return elem1%2 == elem2 %2;
}

int main( int argc, char** argv )
{
	vector<int> vec;
	list<int> lst;

	for( int i=1; i<=6; ++i )
	{
		vec.insert(vec.end(), i);
	}
	for( int i=1; i<=16; i*=2 )
	{
		lst.push_back(i);
	}
	lst.push_back(3);

	for(vector<int>::const_iterator citr=vec.begin(); citr!=vec.end(); ++citr )
	{
		cout<<*citr<<' ';
	}cout<<endl;
	for(list<int>::const_iterator citr=lst.begin(); citr!=lst.end(); ++citr)
	{
		cout<<*citr<<' ';
	}cout<<endl;

	pair<vector<int>::iterator, list<int>::iterator> values; 
	values = mismatch( vec.begin(), vec.end(), lst.begin() );
	if( vec.end() == values.first )
	{
		cout<<"no mismatch"<<endl;
	}else{
		cout<<"first mismatch:"<<*values.first<<" "<<*values.second<<endl;
	}

	values = mismatch( vec.begin(), vec.end(), lst.begin(), less_equal<int>());
	if( vec.end() == values.first )
	{
		cout<<"no mismatch"<<endl;
	}else{
		cout<<"first mismatch:"<<*values.first<<" "<<*values.second<<endl;
	}

	return 0;
}

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

void PrintList( const list<int>& l )
{
    for(list<int>::const_iterator itr=l.begin(); itr!=l.end(); ++itr )
    {
        cout<<*itr<<' ';
    }cout<<endl;
}

bool LessForCollection(const list<int>& list1, const list<int>& list2 )
{
    if( lexicographical_compare(list1.begin(), list1.end(), list2.begin(), list2.end()) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main( int argc, char** argv )
{
    list<int> lst;
    list<int> lst1;
    list<int> lst2;
    list<int> lst3;

    for( int i=1; i<=6; ++i )
    {
        lst.push_back(i);
    }
    lst3 = lst2 = lst1 = lst;
    lst.push_back(7);
    lst1.push_back(1);
    lst2.push_back(2);
    lst2.push_back(0);
    lst3.push_back(2);


    PrintList(lst);
    PrintList(lst1);
    PrintList(lst2);
    PrintList(lst3);

    if( lexicographical_compare(lst3.begin(), lst3.end(), lst.begin(), lst.end()) )
    {
        cout<<"lst3 < lst"<<endl;
    }else{
        cout<<"lst3 >= lst"<<endl;
    }

    if( lexicographical_compare(lst3.begin(), lst3.end(), lst1.begin(), lst1.end()) )
    {
        cout<<"lst3 < lst1"<<endl;
    }else{
        cout<<"lst3 >= lst1"<<endl;
    }

    if( lexicographical_compare(lst3.begin(), lst3.end(), lst2.begin(), lst2.end()) )
    {
        cout<<"lst3 < lst2"<<endl;
    }else{
        cout<<"lst3 >= lst2"<<endl;
    }

    vector<list<int> > vecList;
    vecList.push_back(lst);
    vecList.push_back(lst1);
    vecList.push_back(lst2);
    vecList.push_back(lst3);
    vecList.push_back(lst3);
    vecList.push_back(lst2);
    vecList.push_back(lst1);
    vecList.push_back(lst);

    for_each(vecList.begin(), vecList.end(), PrintList);
    cout<<endl;
    sort( vecList.begin(), vecList.end(), LessForCollection );
    for_each(vecList.begin(), vecList.end(), PrintList);

    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值