[英]how to iterate through several vectors with DIFFERENT types?
在這里,我有一個不同大小的自定義實體集合(不同類型的向量: Type1
、 Type2
、 Type3
、 Type4
,為簡單起見,我簡化為兩種類型):
#include <stdio.h>
#include <vector>
template<typename DataType>
struct AbstractEntity
{
public:
virtual std::vector<DataType>& getParticlesCPU() = 0;
virtual void print() {};
};
// there is one of
// realization of AbstractEntity class
template<typename DataType>
class ClothEntity : public AbstractEntity<DataType>
{
public:
ClothEntity(){
particles.push_back(DataType(1));
particles.push_back(DataType(2));
}
virtual std::vector<DataType>& getParticlesCPU() override {
return particles;
}
virtual void print(){
for (auto& a : particles)
printf("%d\n", a.x);
}
private:
std::vector<DataType> particles;
};
struct Type1{
int x;
Type1(int x_){x = x_;}
};
struct Type2{
int x;
float y;
Type2(int x_){x = x_; y = 0;}
};
class EntityCollection{
using Entity1 = AbstractEntity<Type1>;
using Entity2 = AbstractEntity<Type2>;
public:
EntityCollection(){}
void push_back(Entity1* entity){
entities1.push_back(entity);
}
void push_back(Entity2* entity){
entities2.push_back(entity);
}
std::vector<Entity1*> getEntity1(){
return entities1;
}
std::vector<Entity2*> getEntity2(){
return entities2;
}
private:
template< typename... Ts >
using custom_function_t = void (*) ( Ts... );
template<typename Entity, typename... Ts >
double fun_per_entity( std::vector<Entity>& entities, custom_function_t<Ts...> f, Ts... args ) {
for(auto& entity : entities){
f(entity, args...);
}
}
public:
template<typename... Ts >
void run_function(custom_function_t<Ts...> f, Ts... args){
fun_per_entity(entities1, f, args...);
fun_per_entity(entities2, f, args...);
}
private:
std::vector<Entity1*> entities1;
std::vector<Entity2*> entities2;
};
int main()
{
EntityCollection ec;
ClothEntity<Type1> e1;
ClothEntity<Type2> e2;
ec.push_back(&e1);
ec.push_back(&e2);
// ec.run_function(print); // does not work
for (auto& ev: ec.getEntity1())
ev->print();
for (auto& ev: ec.getEntity2())
ev->print();
return 0;
}
我的目標是安全地遍歷所有向量(entities1、entities2、...)以運行給定的外部函數f_external(Entity e, Args...)
:
for (auto& e : entities1)
f_external(e, args...)
for (auto& e : entities2)
f_external(e, args...)
或內部函數f_internal(Args...)
:
for (auto& e : entities1)
e->f_internal(args...)
for (auto& e : entities2)
e->f_internal(args...)
現在,我每次都在寫很多代碼:
EntityCollection ec;
for (auto& e : ec.getEntity1())
e->f_internal(args...)
for (auto& e : ec.getEntity2())
e->f_internal(args...)
如何自動化它以最大程度地減少添加新類型時的錯誤和更改? 如果您使用C++17
,我將不勝感激。
使您的run_function
成員函數采用仿函數而不是函數指針:
template<typename Entity, typename F, typename... Ts>
static void fun_per_entity(std::vector<Entity*>& entities, F&& f, Ts&&... args) {
for (auto& entity : entities) {
f(entity, args...);
}
}
public:
template<typename F, typename... Ts>
void run_function(F&& f, Ts&&... args) {
fun_per_entity(entities1, f, args...);
fun_per_entity(entities2, f, args...);
fun_per_entity(entities3, f, args...);
// ...
}
您可以將它與 lambda 一起使用:
ec.run_function([&](auto* e) { f_external(e, args...); });
ec.run_function([&](auto* e) { e->f_internal(args...); });
還可以考慮使template<typename DataType> class AbstractEntity
從一些非模板基礎派生,因此您只需要一個向量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.