先说
#include "algostuff.h" 函数
	
	
		
			- 
				#ifndef algostuff_h 
 
- 
				#define algostuff_h 
 
- 
				  
 
- 
				#include <iostream> 
 
- 
				#include <vector> 
 
- 
				#include <list> 
 
- 
				#include <deque> 
 
- 
				#include <set> 
 
- 
				#include <map> 
 
- 
				#include <string> 
 
- 
				#include <algorithm> 
 
- 
				#include <functional> 
 
- 
				#include <numeric> 
 
- 
				  
 
- 
				//print all the elements 
 
- 
				template <class t> 
 
- 
				inline void print_elemnts(const t &col,const char *optcstr = " ") 
 
- 
				{ 
 
- 
				    typename t::const_iterator pos; 
 
- 
				    cout << optcstr; 
 
- 
				    for(pos = col.begin();pos != col.end();pos) 
 
- 
				        cout << *pos << " "; 
 
- 
				    cout << endl; 
 
- 
				} 
 
- 
				  
 
- 
				//insert values from first to last into the collection 
 
- 
				template <class t> 
 
- 
				inline void insert_elements(t &col,int first,int last) 
 
- 
				{ 
 
- 
				    for(int i = first;i <= last;i) 
 
- 
				        col.insert(col.end(),i); 
 
- 
				} 
 
- 
				  
 
- 
				#endif
			
 
 
	find算法
	
		
		
			
				- 
					template<class inputiterator, class t> 
 
- 
					  inputiterator find ( inputiterator first, inputiterator last, const t& value ) 
 
- 
					  { 
 
- 
					    for ( ;first!=last; first) if ( *first==value ) break; 
 
- 
					    return first; 
 
- 
					  } 
				
 
	 
	返回区间[first,end)中第一个值等于value的元素的位置。 
	
		如果没有找到匹配元素,则返回end。 
	
 
	
	
		
			- 
				list<string> fruit;
 
- 
				list<string>::iterator fruititerator;
 
- 
				
 
- 
				fruit.push_back("apple");
 
- 
				fruit.push_back("pineapple");
 
- 
				fruit.push_back("star apple");
 
- 
				
 
- 
				fruititerator = find (fruit.begin(), fruit.end(),"pineapple");
 
- 
				 
 
- 
				//如果没找到,则返回end
 
- 
				    if (fruititerator == fruit.end()) {
 
- 
				       cout << "fruit not found in list" << endl;
 
- 
				    }
 
- 
				    else {
 
- 
				       cout<< *fruititerator <<endl;
 
- 
				    } 
			
 
 
	
		find_if()算法 
	
template<class inputiterator, class predicate>  
inputiterator find_if ( inputiterator first, inputiterator last, predicate pred )  、 
 
	{  
    for ( ; first!=last ; first   ) if ( pred(*first) ) break;  
    return first;   
	}  
	它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。
	如果没找到,返回end。
	
	
		
			- 
				#include "algostuff.h" 
 
- 
				  
 
- 
				using namespace std; 
 
- 
				  
 
- 
				int main() 
 
- 
				{ 
 
- 
				    vector<int> intvec; 
 
- 
				  
 
- 
				    insert_elements(intvec,1,9); 
 
- 
				  
 
- 
				    vector<int>::iterator pos; 
 
- 
				    pos = find_if(intvec.begin(),intvec.end(), 
 
- 
				        not1(bind2nd(modulus<int>(),3))); 
 
- 
				  
 
- 
				    if(pos != intvec.end()) 
 
- 
				        cout << "the value divided by 3 exists,and the first value's position is " << 
 
- 
				        distance(intvec.begin(),pos)  1 << endl; 
 
- 
				    else 
 
- 
				        cout << "the value divided by 3 not found!" << endl; 
 
- 
				} 
			
 
 
                                   
              
              阅读(1827) | 评论(0) | 转发(0) |