传递一个结构类型c++
passing a type of a struct c++
我想将一种类型的结构传递给函数,然后函数将循环通过这些结构的向量来检查条件。这可能吗?
#define EMPTY -1
struct CHAMPION{
int champ_ID;
int weaponL_ID;
int weaponR_ID;
}
vector<string> Champions;
int getChampIndex("struct_obj" obj, int value){
for(int i=0; i<Champions.size(); i++){
if(Champions[i].obj == value){return i;}
}
}
int main(){
//fill Champions vector
SearchedChamp = getChampIndex(champ_ID, 17);
//process SearchedChamp
NoWeaponL = getChampIndex(weaponL_ID, EMPTY);
//process NoWeaponL
NoWeaponR = getChampIndex(weaponR_ID, EMPTY);
//process NoWeaponR
}
不能用字段来实现。我不确定是否可以使用第一个参数(对象)的占位符绑定实例函数。如果是这样,您可以使用"类绑定"std::function<int(void)>
作为getChampIndex
的第一个参数,将其绑定到结构的值getter(是的,这种方法需要int get_champID()
等),并与冠军列表的所有成员连续调用它,将实际的冠军实例作为第一个参数传递。这至少需要C++11(不确定什么时候会出现,我在过去几个月里一直在用C++14进行编码),此外,就可读性和健壮性而言,这是一个糟糕的设计。如果你需要这个,它肯定是一个严重设计错误的代码气味。
我假设vector<string> Champions
是vector<CHAMPION>
,因为否则整个getChampIndex
就没有意义了。
您可以使用指向成员的指针将该成员传递给getChampIndex
。
#include <vector>
#include <algorithm>
static constexpr int EMPTY = -1;
struct CHAMPION{
int champ_ID;
int weaponL_ID;
int weaponR_ID;
};
typedef int CHAMPION::*FieldPointer;
std::vector<CHAMPION> Champions;
std::vector<CHAMPION>::size_type getChampIndex(FieldPointer field, int value){
auto const i = std::find_if( Champions.begin(), Champions.end(), [&](auto const& c) {return (c.*field) == value;} );
// what do we do if there is no element with searched value?
return i - Champions.begin();
}
int main(int argc, char* argv[]) {
//fill Champions vector
auto SearchedChamp = getChampIndex(&CHAMPION::champ_ID, 17);
//process SearchedChamp
auto NoWeaponL = getChampIndex(&CHAMPION::weaponL_ID, EMPTY);
//process NoWeaponL
auto NoWeaponR = getChampIndex(&CHAMPION::weaponR_ID, EMPTY);
//process NoWeaponR
}
我认为结构本身的设计不是很合适,但这很难从问题描述中判断出来。如果你必须坚持设计,我可能不会使用这种技术,只使用lambda函数:
template<typename Predicate>
std::vector<CHAMPION>::size_type getChampIndex(Predicate pred){
auto const i = std::find_if( Champions.begin(), Champions.end(), pred );
return i - Champions.begin();
}
int main(int argc, char* argv[]) {
//fill Champions vector
auto SearchedChamp = getChampIndex([](CHAMPION const& c) {return c.champ_ID == 17; } );
//process SearchedChamp
auto NoWeaponL = getChampIndex([](CHAMPION const& c) {return c.weaponL_ID == EMPTY;} );
//process NoWeaponL
auto NoWeaponR = getChampIndex([](CHAMPION const& c) {return c.weaponR_ID == EMPTY;});
//process NoWeaponR
}
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何将数据从一个结构链接到另一个结构
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- std::transform 将一个结构数组复制到另一个结构数组
- 输入 ctrl + x 后如何再次 cin (cin<<x) 循环(cin 一个结构)
- 如何在级别上打印树,给定一个结构,该结构保存节点的值及其父节点的值,对于树中的每个节点
- 创建一个结构的关联数组,以创建一个缓冲区,供键快速访问
- 如何在另一个结构中初始化结构数组?
- 在另一个结构中声明内部结构会导致错误:结构使用无效
- 我想重置一个C++结构统计,我可以以某种方式使用 stat() 语法吗?
- C++可以输出一个结构的内存地址吗
- 通过 C++ 中的另一个结构成员访问结构
- C++ push_back() 一个结构体到一个向量中
- 难以从另一个结构中的函数返回结构
- 如何将向量添加到结构中以创建一个库存系统,在该系统中,我可以仅使用一个结构向系统添加多种不同的葡萄酒
- 如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
- 将一个结构的值复制到另一个结构
- 是否可以在同一未创建的结构内创建一个结构