gpt4 book ai didi

c++ - 在c++中的 vector 中推回多种类型的数据

转载 作者:太空狗 更新时间:2023-10-29 20:02:12 26 4
gpt4 key购买 nike

假设我有一个像这样充满点的 vector :

vector<Point3f> cluster_points

现在我得到 vector 中每个点的 2 点之间的距离。我想将所有这些数据存储在如下容器中:

{distance, (both point's index number from *cluster_points*)}  

例如

{70.54,  (0,1)};
{98.485, (1,2)};
{87.565, (2,3)};
{107.54, (3,4)};

我如何在 C++11 中执行此操作?

最佳答案

在 C++14 中:

struct DistanceBetweenPoints
{
double distance = {};
size_t p1 = {};
size_t p2 = {};
};

std::vector<DistanceBetweenPoints> foo;
foo.push_back({ 70.54, 0, 1 });
//...

编辑

就像 Khouri Giordano 在评论部分指出的那样,这在 C++11 中不受支持,因为在使用类内初始化时,它会变成非 POD 类型,并且会丢失聚合构造。查看他的 answer用于 C++11 兼容解决方案。

关于c++ - 在c++中的 vector 中推回多种类型的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45080937/

26 4 0