c++测试之使用STL find()查找数组中的元素

来源:岁月联盟 编辑:exp 时间:2012-05-30

#include <algorithm> 
#include <iostream> 
 
using namespace std; 
 
void main() 

    int IntValue[5] = {1,2,3,4,5}; 
    int* Result; 
 
    Result = find(IntValue, IntValue+4, 8); 
    if (Result != IntValue+4) 
    { 
        cout<<"Result = "<<*Result<<endl; 
    }else 
    { 
        cout <<"No corret result"<<endl; 
    } 
 
    while(1); 

测试结果:
1) 数组存在相应的值,find() 函数将返回对应值
2) 数组不存在相应值,find() 将返回find 函数的第二个参数值,可以以此来判断数组的值中时候存在相应的值

 


摘自 DriverMonkey的专栏