现在的位置: 首页 > 综合 > 正文

如何在list中查找

2012年01月28日 ⁄ 综合 ⁄ 共 851字 ⁄ 字号 评论关闭

如果list存储的是能够比较大小的数值型数据,如int,double等,可以直接采用find进行查找,

例子:

#include
<iostream>

#include
<list>

#include
<algorithm>

using namespace std;

int_tmain(int argc, _TCHAR* argv[])

{

    list<int> listTemp;

    list<int>::iterator k ;

    listTemp.push_back(10);

    listTemp.push_back(12);

    listTemp.push_back(15);

   

    k= find(listTemp.begin(),listTemp.end(),10);

    if(k != listTemp.end())

    {

       cout<<"find"<<endl;

    }

   

    k= find(listTemp.begin(),listTemp.end(),15);

    if(k != listTemp.end())

    {

       cout<<"find"<<endl;

    }

    else

    {

       cout<<"have not find"<<endl;

    }

    k= find(listTemp.begin(),listTemp.end(),12);

    if(k != listTemp.end())

    {

       cout<<"find"<<endl;

    }

    else

    {

       cout<<"have not find"<<endl;

    }

    k= find(listTemp.begin(),listTemp.end(),11);

    if(k != listTemp.end())

    {

       cout<<"find"<<endl;

    }

    else

    {

       cout<<"have not find"<<endl;

    }

 

    return 0;

}

抱歉!评论已关闭.