gpt4 book ai didi

c++ - 保存多种数据类型的数据结构 C++

转载 作者:行者123 更新时间:2023-11-30 00:50:05 25 4
gpt4 key购买 nike

在我的应用程序中有一个类(即 ItemData 类),它有 30 多个不同类型的成员变量,例如

int a;
int b;
std::string c;
float d;
double e;
double f;
char * g;

还有更多。

我的应用程序需要创建大量的项目数据类,因此应用程序的内存使用率很高。关于此类的一个特殊事实是 ItemData 类的大多数实例仅具有少数成员变量的值。

例如:实例一可能只有 a 和 b 的值。实例二只能有 b、c 和 g 的值。

因此,为了减少我的应用程序的内存使用量,我需要一种方法,只为创建实例时具有数据的成员变量分配内存。所以我想到了可以通过位置访问元素并在其上存储数据的通用数据结构。所以 ItemData 类有类似下面的东西,并在上面存储动态分配的数据。(我必须分别维护每个位置的位置和信息)

std::vector<void*> vec_DataArray;

如果 ItemData 实例 mItemData1 具有 a 和 d 的值:

mItemData1.vec_DataArray[0] = new int(iValue);
mItemData1.vec_DataArray[3] = new float(fValue);

有人可以告诉我这是否是减少应用程序内存使用的好方法?是否有任何通用容器可以容纳多种数据类型(对于 vec_DataArray),以便我可以在访问数据时避免 void* 到数据类型的转换。

最佳答案

我认为您应该尽可能避免使用可以容纳多种类型数据的数据结构,并考虑不同的设计。

例如,您可以考虑使用类似于 Flyweight pattern 的东西并将“外部”状态从 ItemData 类中拉出,只留下“内部”状态。我觉得它有点非面向对象,但它可能会满足您的需求。例如,您可以保留从项目索引到数据的单独映射。这是否有助于您的内存使用取决于数据的稀疏程度。

#include <unordered_map>
#include <iostream>

class Item {
private:
std::string type_; // "intrinsic" state
public:
Item(const std::string &type) : type_(type) {}

// pass in "extrinsic" state
void someOperation(std::string color, double speed) {

// do something using both "intrinsic" and "extrinsic" state...
std::cout << color << " " << type_ << " moving at " << speed << "mph\n";
}
};

class AnimalSim {
private:
std::unordered_map<int, std::string> duck_colors_; // Store extrinsic
std::unordered_map<int, std::string> sheep_colors_; // state separately
std::unordered_map<int, double> duck_speeds_; // in a more
std::unordered_map<int, double> sheep_speeds_; // efficent way.
public:
void run();
std::string getDuckColor(int duck_index) const;
std::string getSheepColor(int sheep_index) const;
double getDuckSpeed(int duck_index) const;
double getSheepSpeed(int sheep_index) const;
};

void
AnimalSim::run() {
auto duck = Item{"duck"}; // Create `Flyweight` objects that can be shared.
auto sheep = Item{"sheep"}; // Should probably be done by a factory with a cache.

// Create duck 0
duck_colors_.emplace(0, "red");
duck_speeds_.emplace(0, 150.0);

// Create duck 1 - has no speed
duck_colors_.emplace(1, "green");
size_t num_ducks = 2;

// Create sheep 0 - has no color
sheep_speeds_.emplace(0, 100.0);
size_t num_sheep = 1;

// Do something with all the ducks
for(size_t i = 0; i != num_ducks; ++i)
duck.someOperation(getDuckColor(i), getDuckSpeed(i));

// Do something with all the sheep
for(size_t i = 0; i != num_sheep; ++i)
sheep.someOperation(getSheepColor(i), getSheepSpeed(i));
}

std::string
AnimalSim::getDuckColor(int duck_index) const {
auto color_itr = duck_colors_.find(duck_index);
return color_itr != duck_colors_.end() ? color_itr->second : "black";
}

std::string
AnimalSim::getSheepColor(int sheep_index) const {
auto color_itr = sheep_colors_.find(sheep_index);
return color_itr != sheep_colors_.end() ? color_itr->second : "white";
}

double
AnimalSim::getDuckSpeed(int duck_index) const {
auto speed_itr = duck_speeds_.find(duck_index);
return speed_itr != duck_speeds_.end() ? speed_itr->second : 0.0;
}

double
AnimalSim::getSheepSpeed(int sheep_index) const {
auto speed_itr = sheep_speeds_.find(sheep_index);
return speed_itr != sheep_speeds_.end() ? speed_itr->second : 0.0;
}

int main() {
AnimalSim animal_sim;
animal_sim.run();
}

Live demo.

编辑:我看到您正在从数据库加载数据。在这种情况下,我想知道为什么您不只是在需要时从数据库中按需加载?

关于c++ - 保存多种数据类型的数据结构 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26157120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com