stl的查找

闻缺陷则喜何志丹
《深入浅出Visual c++》书籍作者
博客专家认证
2014-07-06 09:22:23
stl的查找

std::find,在容器制定系列查找元素,元素的值和第三个参数相等(==)则找到。
std::find_if ,在容器制定系列查找元素,_P(元素的值)为TRUE则找到。

//VC6 和STL配合的很不好
//所以回调函数的返回值不要是void型,也不能是常量
class CRenWu
{
public:
CRenWu(ULONG uID=0,CString strName=""):m_uID(uID)
{
m_strName = strName;
}
bool operator==(const CRenWu& other ) const
{
return m_uID == other.m_uID;
}
bool operator==(const ULONG uID) const
{
return m_uID == uID ;
}

bool operator<(const CRenWu& other ) const
{
return m_uID < other.m_uID ;
}

int Trace()
{
CString str;
str.Format("%s(%d) ",m_strName,m_uID);
TRACE(str);
return 0;
}
BOOL IsGM()
{
return ( 3 == m_uID || 4 == m_uID );
}
static bool QuanXianTong( CRenWu& ths, CRenWu& other)
{
return (ths.IsGM() == other.IsGM());
}
protected:
CString m_strName;
ULONG m_uID;
};

void CFind_searchDlg::OnButton1()
{

std::vector<CRenWu> rens;//不需要排序
rens.push_back(CRenWu(10,"测试"));
rens.push_back(CRenWu(1,"张三"));
rens.push_back(CRenWu(2,"李四"));
rens.push_back(CRenWu(3,"大话三国"));
rens.push_back(CRenWu(3,"萧相"));

//通过参数为const ULONG的==查找
std::vector<CRenWu>::iterator it = std::find(rens.begin(),rens.end(),1);
it->Trace();

//通过参数为const CRenWu& 的==查找
it = std::find(rens.begin(),rens.end(),CRenWu(10));
it->Trace();

//查找第一个GM
it = std::find_if(rens.begin(),rens.end(),std::mem_fun_ref(&CRenWu::IsGM));
it->Trace();


}

std::find_end 从后向前找
std::find_first_of 在系列中找任何一个元素

void CFind_searchDlg::OnButton2()
{
int a[10] = {1,2,3,4,5,6,7,1,2,3};
int b[3] = {2,3,7};

int*p = std::find_end(a,a+10,a,a+3);
int iPos = p-a;//根据调试iPos的结果为7,从后向前查找序列1,2,3

p = std::find_first_of(a,a+10,b,b+3);//在a中查找2或3或7
iPos = p- a;//iPos的值为1
}

adjacent_find 的作用是在容器里找到相邻元素符合自定义条件的第一个元素对,并返回这个元素对的第一个向量。

void CFind_searchDlg::OnButton3()
{
std::vector<CRenWu> rens;

rens.push_back(CRenWu(3,"大话三国"));
rens.push_back(CRenWu(4,"萧相"));
rens.push_back(CRenWu(1,"张三"));
rens.push_back(CRenWu(1,"李四"));

//查找相邻元素是不是相等
std::vector<CRenWu>::iterator it = std::adjacent_find(rens.begin(),rens.end());
it->Trace();

//查找相邻的GM
it = std::adjacent_find(rens.begin(),rens.end(),CRenWu::QuanXianTong);
it->Trace();
}

std::search_n 查找多个相邻的元素。
std::search 查找子序列
void CFind_searchDlg::OnButton4()
{
int a[10] = {1,2,3,4,1,2,3,1,3,3};
int* p = std::search_n(a,a+10,2,3);//查找2个相邻的3
int iPos = p-a;//经调试iPos为8

p = std::search(a,a+10,a+4,a+7);
iPos = p - a;//经调试iPos为0
}


std::binary_search 二分查找,速度更快,必须已经排序。
void CFind_searchDlg::OnButton5()
{
int a[10] = {1,5,3,4,1,2,3,1,3,2};
bool b = std::binary_search(a,a+10,3);
//经调试,b的值为true
b = std::binary_search(a,a+10,5);
//经调试b的值为false
//原因:binary_search只能查找有序容器
std::sort(a,a+10);
b = std::binary_search(a,a+10,5);//经调试结果正确,为ture
}

std::equal_range,查找第一个等于的元素,查找第一个大于的元素。

void CFind_searchDlg::OnButton6()
{
int a[10] = {1,2,3,3,3,4,5,6,6,10};//必须是已经排序好的
std::pair<int*,int*> it = std::equal_range(a,a+10,3);
int iPos1 = it.first -a;//经调试值为2
int iPos2 = it.second-a;//经调试值为5

int*p = std::lower_bound(a,a+10,3);
int iPos = p-a;//经调试值为2

p = std::upper_bound(a,a+10,3);
iPos = p - a;//经调试值为5
}

void CFind_searchDlg::OnButton7()
{
std::vector<CRenWu> rens;

rens.push_back(CRenWu(1,"何大"));
rens.push_back(CRenWu(2,"许二"));
rens.push_back(CRenWu(3,"张三"));
rens.push_back(CRenWu(3,"李四"));
rens.push_back(CRenWu(5,"王五"));
rens.push_back(CRenWu(6,"赵六"));

const ULONG uID = 3;//经测试ID为,3,4,1,6都能有正确结果。rens没有元素也不会死
//CRenWu必须重载<
std::pair<std::vector<CRenWu>::iterator,std::vector<CRenWu>::iterator> it =
std::equal_range(rens.begin(),rens.end(),CRenWu(uID));

//输出ID小于3的人物
std::for_each(rens.begin(),it.first,std::mem_fun_ref(&CRenWu::Trace));
TRACE("\r\n");
//输出ID为3的人物
std::for_each(it.first,it.second,std::mem_fun_ref(&CRenWu::Trace));
TRACE("\r\n");
//输出ID大于3的人物
std::for_each(it.second,rens.end(),std::mem_fun_ref(&CRenWu::Trace));
TRACE("\r\n");
}

std::max_element,查找最大的元素。
std::min_element ,查找最小的元素。


源码下载:
csdn下载:
http://dowknload.csdn.net/detail/constmei/7600839百度网盘:
http://pan.baidu.com/s/1c0F2wi
...全文
48 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
baichi4141 2014-07-07
  • 打赏
  • 举报
回复
VC6下可以安装IntelC++编译器,虽然版本也比较旧了,但怎么也比VC6自带的要好很多,基本没遇到过STL编译错误的问题
  • 打赏
  • 举报
回复
VC6下就别用STL了

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧