'Pushing a static array into a std::vector?
I'm trying to do the following:
I have:
std::vector<std::vector<GLdouble[2]>> ThreadPts(4);
then I try to do:
GLdouble tmp[2];
while(step--)
{
fx += dfx;
fy += dfy;
dfx += ddfx;
dfy += ddfy;
ddfx += dddfx;
ddfy += dddfy;
tmp[0] = fx;
tmp[1] = fy;
ThreadPts[currentvector].push_back(tmp);
}
But the compiler says:
Error 15 error C2440: 'initializing' : cannot convert from 'const GLdouble [2]' to 'double [2]' C:\Program Files\Microsoft Visual Studio 9.0\VC\include\vector 1211
How could I do this then? I'm using VS 2008 and don;t have std::array, and I don't have boost.
Thanks
Solution 1:[1]
Instead of a raw array of 2 members, wrap it in a struct like a Point:
struct Point {
GLDouble[2] coords;
void setCoordinates(GLDouble x, GLDouble y)
{
coords[0] = x;
coords[1] = y;
}
/* consider adding constructor(s) and other methods here,
* if appropriate
*/
};
std::vector<std::vector<Point>> ThreadPts(4);
while(step--)
{
fx += dfx;
fy += dfy;
dfx += ddfx;
dfy += ddfy;
ddfx += dddfx;
ddfy += dddfy;
Point p;
p.setCoordinates(fx,fy);
ThreadPts[currentvector].push_back(p);
}
It takes the same amount of memory as a 2-element array, and has more readable semantics than an array.
Solution 2:[2]
Solution 3:[3]
You could also use a std::pair
std::vector<std::vector<std::pair<GLdouble[2],GLdouble[2]> > > ThreadPts(4);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Stephen |
| Solution 3 | Falmarri |
