STL中vector查找算法find()和find_if()深入比较

来源:互联网 发布:怎么成为算法工程师 编辑:程序博客网 时间:2024/05/01 16:54
// test.cpp
//*****************STL的vector<>通用算法find()和find_if():从test-1到test-4逐个升级,慢慢体会吧!
//Author by superchao@topplusvision.com


#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;


//STL的通用算法find()和find_if()可以查找指定对象,参数1:即iterator指着开始的位置,参数2:即iterator指着停止处理的地方。
//注意:包含开始和结束的位置的元素。


/*
//1. 从vector容器中查找指定的基本对象:find()算法
int _tmain(int argc, _TCHAR* argv[])
{
vector<char*> objvec;
vector<char*>::iterator objit;
objvec.push_back("hahaha");
objvec.push_back("gagaga");
objvec.push_back("lalala");
objit = find(objvec.begin(), objvec.end(), "lalala");
if (objit == objvec.end())
{
cout<<"---------------没找到----------------"<<endl;
}
else
{
cout<<"***********找到了************:"<<*objit<<endl;
}
getchar();
//system( "PAUSE "); 
return 0;
}//*/


/*
//2. 从vector容器中查找指定的高级对象,利用函数对象进行查找:find_if()算法
struct istb
{
public:
int m_nlen;
char* m_pchar;
public:
istb(char* pchar, int nlen)
{
cout<<"执行构造函数"<<endl;
m_nlen=nlen;
m_pchar=new char[m_nlen+1];
strcpy_s(m_pchar, m_nlen+1, pchar);
}
istb(const istb&objtb)//需要定义拷贝构造函数
{
cout<<"执行拷贝构造函数"<<endl;
m_nlen = objtb.m_nlen;
m_pchar = new char[m_nlen+1];
strcpy_s(m_pchar, m_nlen+1, objtb.m_pchar);
}
bool operator()(const char* pchar)
{
return (strcmp(pchar,m_pchar) == 0);
}
~istb()
{
delete m_pchar;
cout<<"执行析构函数"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<char*> objvec;
vector<char*>::iterator objit;
objvec.push_back("hahaha");
objvec.push_back("gagaga");
objvec.push_back("lalala");

istb objtb("lalala", 10);//第1种方式:先生成对象
objit = find_if(objvec.begin(), objvec.end(), objtb);//第1种方式:再使用对象
//objit = find_if(objvec.begin(), objvec.end(), istb("lalala", 10));//第2种方式:直接生成临时对象
if (objit == objvec.end())
{
cout<<"---------------没找到----------------"<<endl;
}
else
{
cout<<"***********找到了************:"<<*objit<<endl;
}
getchar();
//system( "PAUSE "); 
return 0;
}//*/


/*
//3. 从vector容器中查找指定的高级对象的某个属性,利用函数对象进行查找:find_if()算法
struct istb
{
public:
int m_nID;
char* m_pchar;
public:
istb(char* pchar, int nID)
{
cout<<"执行构造函数"<<endl;
m_nID=nID;
m_pchar=new char[10];
strcpy_s(m_pchar, 10, pchar);
}
istb(const istb&objtb)//需要定义拷贝构造函数
{
cout<<"执行拷贝构造函数"<<endl;
m_nID = objtb.m_nID;
m_pchar = new char[10];
strcpy_s(m_pchar, 10, objtb.m_pchar);
}
bool operator()(const istb& objtb)
{
return (objtb.m_nID == m_nID);
//return (strcmp(objtb.m_pchar, m_pchar) == 0);
}
~istb()
{
delete m_pchar;
cout<<"执行析构函数"<<endl;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
vector<istb> objvec;
vector<istb>::iterator objit;
objvec.push_back(istb("hahaha5", 5));
objvec.push_back(istb("hahaha1", 1));
objvec.push_back(istb("hahaha6", 6));
objvec.push_back(istb("hahaha4", 4));
objvec.push_back(istb("hahaha3", 3));
objvec.push_back(istb("hahaha2", 2));


istb objtb("hahaha333", 3);//待查找对象


objit = find_if(objvec.begin(), objvec.end(), objtb);
if (objit == objvec.end())
{
cout<<"---------------没找到----------------"<<endl;
}
else
{
int nindex=distance(objvec.begin(), objit);
cout<<"找到了:"<<(objit->m_pchar)<<",其位置在第 "<<nindex<<" 个,内容为:"<<(objvec[nindex].m_pchar)<<endl;
}


getchar();
//system( "PAUSE "); 
return 0;
}//*/




//4. 从vector容器中查找指定的高级对象的某个属性,利用函数对象进行查找:find_if()算法;利用sort函数进行排序后再比较;
struct istb
{
public:
int m_nID;
char m_char[10];//注意:此处需要将test-3中的动态分配的指针换为固定的内存空间,不然利用sort函数进行排序后就会出现内存错误!
public:
istb(char* pchar, int nID)
{
cout<<"执行构造函数"<<endl;
m_nID=nID;
strcpy_s(m_char, 10, pchar);
}
istb(const istb&objtb)//需要定义拷贝构造函数
{
cout<<"执行拷贝构造函数"<<endl;
m_nID = objtb.m_nID;
strcpy_s(m_char, 10, objtb.m_char);
}
bool operator()(const istb& objtb)
{
return (objtb.m_nID == m_nID);
//return (strcmp(objtb.m_char, m_char) == 0);
}
~istb()
{
cout<<"执行析构函数"<<endl;
}
};
bool vIDSmaller(const istb& obj1, const istb& obj2)
{
return (obj1.m_nID<obj2.m_nID);
};


int _tmain(int argc, _TCHAR* argv[])
{
vector<istb> objvec;
vector<istb>::iterator objit;
objvec.push_back(istb("hahaha5", 5));
objvec.push_back(istb("hahaha1", 1));
objvec.push_back(istb("hahaha6", 6));
objvec.push_back(istb("hahaha4", 4));
objvec.push_back(istb("hahaha3", 3));
objvec.push_back(istb("hahaha2", 2));


istb objtb("lalala", 3);//待查找对象


objit = find_if(objvec.begin(), objvec.end(), objtb);
if (objit == objvec.end())
{
cout<<"---------------(排序前)没找到----------------"<<endl;
}
else
{
int nindex=distance(objvec.begin(), objit);
cout<<"(排序前)找到了:"<<objit->m_char<<",其位置在第 "<<nindex<<" 个,内容为:"<<(objvec[nindex].m_char)<<endl;
}


sort(objvec.begin(), objvec.end(), vIDSmaller);//排序


objit = find_if(objvec.begin(), objvec.end(), objtb);
if (objit == objvec.end())
{
cout<<"---------------(排序后)没找到----------------"<<endl;
}
else
{
int nindex=distance(objvec.begin(), objit);
cout<<"(排序后)找到了:"<<objit->m_char<<",其位置在第 "<<nindex<<" 个,内容为:"<<(objvec[nindex].m_char)<<endl;
}


getchar();
//system( "PAUSE "); 
return 0;
}//*/
0 0
原创粉丝点击