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