STL 之search,search_n,sort,binary_search

返回

作用:用来查找元素和元素排序


声明:

#include <algorithm>
template <class forwardItr1, class forwardItr2>
forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);
template <class forwardItr1, class forwardItr2,class binaryPredicate>
forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);

template <class forwardItr, class size,class Type>
forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value);
template <class forwardItr, class size,class Type,class binaryPredicate>
forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value,binaryPredicate op);

template<class randomAccessItr>
void sort(randomAccessItr first,randomAccessItr last);
template<class randomAccessItr, class compare>
void sort(randomAccessItr first, randomAccessItr last, compare op);

template<class forwardItr,class Type>
bool binary_search(forwardItr first,forwardItr last,const Type& searchValue);
template<class forwardItr,class Type,class compare>
bool binary_search(forwardItr first, forwardItr last, const Type& searchValue,compare op);

示例代码:

#include <iostream>
#include <list>

#include <string>
#include <numeric>
#include <iterator>
#include <vector>
#include <functional>

#include <algorithm>

using namespace std;

int main() {
	int intList[15] = {12,34,56,34,34,
					   78,38,43,12,25,
					   34,56,62,5,49};
	vector<int> vecList(intList,intList+15);
	int list[2] = {34, 56};
	vector<int>::iterator location;

	ostream_iterator<int> screen(cout, " ");

	cout << "vecList:" << endl;
	copy(vecList.begin(),vecList.end(),screen);
	cout << endl;
	cout << "list:" << endl;
	copy(list,list+2,screen);
	cout << endl;

	// search:查找一个集合是否在另一集合中
	// 与find的区别是,find是查找某一个元素
	location = search(vecList.begin(),vecList.end(),list,list + 2);
	if (location != vecList.end())
	{
		cout << "location:" << (location - vecList.begin()) << endl;
	} else {
		cout << "list is not in vecList" << endl;
	}
	// search_n :查找某个元素第n此出现的位置
	location = search_n(vecList.begin(),vecList.end(),2,34);
	if (location != vecList.end())
	{
		cout << "location:" << (location - vecList.begin()) << endl;
	} else {
		cout << "list is not in vecList" << endl;
	}

	// sort
	sort(vecList.begin(),vecList.end());
	cout << "vecList:" << endl;
	copy(vecList.begin(),vecList.end(),screen);
	cout << endl;

	bool found;
	// 用二分法查找:前提先排序
	found = binary_search(vecList.begin(),vecList.end(),43);

	if (found)
	{
		cout << "43 found in vecList." << endl;
	} else {
		cout << "43 not found in vecList." << endl;
	}

	return 0;
}

运行结果:

vecList:
12 34 56 34 34 78 38 43 12 25 34 56 62 5 49
list:
34 56
location:1
location:3
vecList:
5 12 12 25 34 34 34 34 38 43 49 56 56 62 78
43 found in vecList.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值