在c ++中推回向量中的多种类型的数据 [英] push back multiple types of data in a vector in c++

查看:15
本文介绍了在c ++中推回向量中的多种类型的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I have a vector full of points like this:

vector<Point3f> cluster_points

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

Now I am getting distance between 2 points for each point in the vector. I want to store all these data in a container like below:

{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 中执行此操作?

How can I do this in 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 类型并且你失去了聚合构造.请参阅他的答案,了解 C++11 兼容解决方案.

Just like Khouri Giordano noted in the comments section, this isn't supported in C++11 because when using in-class initialization, it becomes a non-POD type and you lose aggregate construction. See his answer for C++11 compatible solutions.

这篇关于在c ++中推回向量中的多种类型的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆