---
title: "STL 之search,search_n,sort,binary_search"
author: "mywebcode"
date: "2014-04-15T15:20:00Z"
tags: []
slug: "stl-search-search-n-sort-binary-search"
draft: false
lastmod: "2014-04-30T09:14:04Z"
id: 2058264
url: "https://mywebcode.iteye.com/blog/2058264"
---

[返回](http://blog.csdn.net/haifengzhilian/article/details/23684225)

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

声明：

```
#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.