Abc
Abc

Reputation: 824

Keep vectors of Point and vector of int in structure of one array

I have to following structure of Point and int type,

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; 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

Not getting this error.
Anyone can help me to clear it.

Upvotes: 1

Views: 242

Answers (4)

Jellybaby
Jellybaby

Reputation: 986

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 :)

Upvotes: 0

Adi Levin
Adi Levin

Reputation: 5233

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]};

Upvotes: 2

Jellybaby
Jellybaby

Reputation: 986

It depends on the 'Point' class. 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. But, if there is no way to publicly construct Point from two integers then that method won't work

Upvotes: 0

Humam Helfawi
Humam Helfawi

Reputation: 20274

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.

Upvotes: 0

Related Questions