C++ 如何定义查找函数?
尝试为我的向量定义查找函数,因为该向量包含多个数据;它是一个结构的向量 我正在获取一个ID的输入,并尝试在我的表中搜索该ID并找到其索引(如果该ID已经存在) 我这里有声明:C++ 如何定义查找函数?,c++,vector,C++,Vector,尝试为我的向量定义查找函数,因为该向量包含多个数据;它是一个结构的向量 我正在获取一个ID的输入,并尝试在我的表中搜索该ID并找到其索引(如果该ID已经存在) 我这里有声明: vector<Employee> Table; vector<Employee>::iterator It; vector<Employee>::iterator find_It; //Table has these values //Table.ID, Table.ch1, Tabl
vector<Employee> Table;
vector<Employee>::iterator It;
vector<Employee>::iterator find_It;
//Table has these values
//Table.ID, Table.ch1, Table.ch2
有没有办法用变量update_ID进行查找
我试着这样做:
find_It = find(Table.begin(), Table.end(), (*It).update_ID;
但显然我的vector员工没有名为update_ID的数据成员
我想做的另一个选择是创建我自己的find函数,我对如何定义这个函数有点困惑
我想返回ID的索引,其中Table.ID=update\u ID
我应该把什么作为返回类型和值参数?是吗
returntype find( Iterator, Iterator, update ID)
{
for (vector<Employee>::iterator myit = Table.begin(), Table.end(), myit++)
{
if update_ID == Table.ID
{
return myit;
}
}
return myit
}
returntype查找(迭代器、迭代器、更新ID)
{
对于(vector::iterator myit=Table.begin()、Table.end()、myit++)
{
如果update_ID==Table.ID
{
返回myit;
}
}
还我
}
显而易见的方法是将std::find_if()
与谓词一起使用。使用C++ 2011的符号,这可以是这样的:
std::vector<Employee>::iterator it(std::find_if(Table.begin(), Table.end(),
[=](Employee const& e) { return e.ID == update_ID; });
std::vector::iterator it(std::find_if(Table.begin(),Table.end(),
[=](Employee const&e){returne e.ID==update_ID;});
如果不能使用C++ 2011,可以为谓词创建一个函数对象,或者使用一个适合的函数,该函数具有绑定参数,用于<代码> UpDeTeYID。< /P> < P> C++标准库附带。
您正在查找find_if
,它接受一个指定比较的函子
// a functor taking the update_ID you
// are looking for as an argument in the constructor
struct myfind {
myfind(int needle) : needle(needle) {}
int needle;
bool operator()(const Employee& x) {
return x.ID == needle;
}
};
// use as
int update_ID = 23;
std::find_if(begin(Table), end(Table), myfind(update_ID));
您也可以使用lambda:
int id;
std::find_if(begin(Table), end(Table),
[=](const Employee& x) { return x.update_ID == id; });
您可以使用
std::find_if()
了解它的工作原理您可以使用自己的匹配功能。 我假设在您的第一个代码片段中,您指的是成员ID为ch1、ch2的员工,而不是表。 解决问题的一种方法是:
#include <vector>
#include<iostream>
#include<algorithm>
using std::cout;
using std::endl;
using std::vector;
struct Employee{
int ID;
int ch1;
int ch2;
};
int IDmatch;
bool match_id( Employee myemp){
return myemp.ID==IDmatch;
}
int main(){
vector<Employee> Table;
// fill example vector
Employee temp; // use this for inserting structs into your vector
for(int i=0; i<10; i++){
temp.ID = i; // 1,2,3,...
temp.ch1 = 10*i+1; // 11,21,32,...
temp.ch2 = 10*i+2; // 12,22,32,...
Table.push_back(temp);
}
vector<Employee>::iterator itv;
IDmatch = 3;
itv = find_if(Table.begin(), Table.end(), match_id);
cout << "found an Employee with ID=" << IDmatch << ": ch1=" << itv->ch1 << ", ch2=" << itv->ch2 << endl;
}
#包括
#包括
#包括
使用std::cout;
使用std::endl;
使用std::vector;
结构雇员{
int-ID;
int ch1;
int ch2;
};
int-IDmatch;
bool match_id(员工myemp){
返回myemp.ID==IDmatch;
}
int main(){
向量表;
//填充示例向量
Employee temp;//用于将结构插入到向量中
对于(int i=0;i当Table是一个向量时,Table.ID
是什么意思?ID
的类型是什么?啊,我明白了。谢谢你的帮助。但是在myfind的定义/声明中,在我使用的例子23中,我应该在哪里传递update_ID?@iarcfsil。把你需要的值放在那里。我还修改了这个例子,使它更接近你的代码。噢,哇,这s真的很简单。我最终接受了pmr的建议(本质上是同一件事)。你能解释一下你的建议和他的有什么不同吗?我不知道他在回答中定义myfind时做了什么
#include <vector>
#include<iostream>
#include<algorithm>
using std::cout;
using std::endl;
using std::vector;
struct Employee{
int ID;
int ch1;
int ch2;
};
int IDmatch;
bool match_id( Employee myemp){
return myemp.ID==IDmatch;
}
int main(){
vector<Employee> Table;
// fill example vector
Employee temp; // use this for inserting structs into your vector
for(int i=0; i<10; i++){
temp.ID = i; // 1,2,3,...
temp.ch1 = 10*i+1; // 11,21,32,...
temp.ch2 = 10*i+2; // 12,22,32,...
Table.push_back(temp);
}
vector<Employee>::iterator itv;
IDmatch = 3;
itv = find_if(Table.begin(), Table.end(), match_id);
cout << "found an Employee with ID=" << IDmatch << ": ch1=" << itv->ch1 << ", ch2=" << itv->ch2 << endl;
}