简体   繁体   English

将点向量和整数向量保持在一个数组的结构中

[英]Keep vectors of Point and vector of int in structure of one array

I have to following structure of Point and int type, 我必须遵循Point和int类型的结构,

struct PointValue{ Point p; int c; };

In below code, I can keep points and value manually. 在下面的代码中,我可以手动保留积分和价值。

PointValue const data[] =
    {
        { { 19, 187 }, 119 },
        { { 20, 21 }, 255 },
        { { 20, 92 }, 255 },
        { { 22, 190 }, 39 },
        { { 23, 184 }, 39 }, 
        }

But... I want to take points and values from vectors and put in variable data. 但是...我想从向量中获取点和值,然后放入可变数据中。

Edited....... 编辑.......

For example 例如
I have vector <Point> pts and vector <int> ptsVal; 我有vector <Point> ptsvector <int> ptsVal; I want to keep all points and its corresponding value to one array like in data showing above example. 我想将所有点及其对应的值保留在一个数组中,就像上面示例所示的数据一样。

But, I did this small test 但是,我做了这个小测试

PointValue const data[5] {};
    for (int i = 0; i < pts.size(); i++) {
        data { { {pts[i].y, pts[i].x}, ptsVal[i]} };
    }

Error: error C2064: term does not evaluate to a function taking 1 arguments 错误:错误C2064:术语未求值为带有1个参数的函数

Not getting this error. 没有收到此错误。
Anyone can help me to clear it. 任何人都可以帮助我清除它。

Try this: 尝试这个:

std::vector<Point> pts;
std::vector<int> ptsVal;

std::vector<PointValue> data;
data.reserve(pts.size());
for (int i = 0; i < (int)pts.size(); ++i)
    data.push_back({pts[i], ptsVal[i] });

Or, if you prefer arrays instead of vectors: 或者,如果您更喜欢数组而不是向量:

struct PointValue data[2];
for (int i = 0; i < 2; ++i)
    data[i] = {pts[i], ptsVal[i]};

make sure you have a constructor: 确保您有一个构造函数:

struct point{ 
    point(int x, int y) : x(x), y(y){}
    int x,y; 
};

And try this code: 并尝试以下代码:

std::vector<PointValue> merge (const std::vector<int>& my_ints,const std::vector<point>& my_points){
   //ASSERT my_ints.size()==my_points.size()
   std::vector<PointValue> result;
   result.reserve(my_ints.size());
   for(auto it1=my_ints.begin(),it2=my_points.begin();it1!=my_ints.end();++it1,++it2){
       result.emplace_back(*it1,*it2)
   }
}

You may optimize the for loop a bit. 您可以稍微优化for loop

It depends on the 'Point' class. 它取决于“ Point”类。 If it is defined like this: 如果定义如下:

struct Point{
    int x,y; 
};

or like this: 或像这样:

struct Point{ 
    Point(int p, int q) : x(p), y(q){}
    int getX() const { return x; }
    int getY() const { return y; }

private:
    int x,y; 
};

That is, with a public constructor taking two integers then it should work. 也就是说,在使用两个整数的公共构造函数中,它应该可以工作。 I ran it in gcc 4.8 and it works just fine. 我在gcc 4.8中运行它,效果很好。 But, if there is no way to publicly construct Point from two integers then that method won't work 但是,如果没有办法从两个整数公开构造Point ,则该方法将不起作用

OK. 好。 Well in that case here is a working solution. 那么在这种情况下,这是一个可行的解决方案。 But I am not recommending this type of coding it is just to get you working. 但是我不建议您使用这种类型的编码,只是为了让您工作。

#include <iostream>
#include <vector>

using namespace std;

struct Point{
    int x,y; 
};

struct PointValue{ Point p; int c; };

PointValue const data[] =
{
   { { 19, 187 }, 119 },
   { { 20, 21 }, 255 },
   { { 20, 92 }, 255 },
   { { 22, 190 }, 39 },
   { { 23, 184 }, 39 }
};


int main(int, char**){

    vector<PointValue> pts(5);
    for(int i = 0; i < 5; ++i)
       pts[i] = (PointValue){ {1,2}, 3};

    for(int i = 0; i < 5; ++i)
       cout << "pts[" << i << "] = { {" << pts[i].p.x << ", " << pts[i].p.y << "} " << pts[i].c << "}\n";

    return 0;
}

Hope that gets you working but you might consider looking into other programming techniques some time. 希望能使您工作,但您可能会考虑在一段时间内研究其他编程技术。 Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM