tags:

views:

337

answers:

3

I'm trying to optimize the following code below to avoid having to copy and paste and just use SlaveForce and SlavePos properly, which are float[6] type, and baseForce and basePos are vector type:

typedef struct _NodeCoord
{
    float coords[6];
} NodeCoord;

int main()
{
    ...
    memcpy(tempNodeCoord.coords, SlaveForce, 6*sizeof(float));
    baseForce.push_back(tempNodeCoord);
    memcpy(tempNodeCoord.coords, SlavePos, 6*sizeof(float));
    basePos.push_back(tempNodeCoord);
    ...
}

I have tried some test codes. Only this appear to work:

std::vector<NodeCoord> tallon;
NodeCoord m;
memset(m.coords, 0, sizeof(m.coords));
tallon.push_back(m);

while this is along the lines of what I want to accomplish, but it doesn't work:

std::vector<float[6]> ed;
float e[6];
tallon.push_back(e);

I also tried writing a "typedef float[6] mytype;" line before the main function, but it doesn't work well either. Is there anything that I can do to eliminate the memcpy function calls (or rather push_back the float[6] variables directly)? Thanks in advance.

+2  A: 

Try a vector of vectors:

std::vector<std::vector<float> > ed;

ed.push_back(std::vector<float>(6, 0.0f));

In this case I'm pushing a temp vector of floats, with 6 elements with the value of 0.0f;

Info on vector's constructors.

luke
+1  A: 

In order to be storable in a vector, a type must be copyable and assignable, and C-style arrays are neither. You should instead use another vector, or possibly wrap the array in a struct.

anon
What would be the difference between using another vector and wrapping the array in a struct (which is what I'm doing already)?
stanigator
The wrapped array will probably be faster, but less flexible - for erxample, you can't find how many things are actually in an array.
anon
Can't I use sizeof(array)/sizeof(element) to find out how many elements are in the array?
stanigator
There are a lot of circumstances where that simply won't work. And if it does it only tells you the size of the array, not how many actual values it contains.
anon
In terms of cleanliness, does the the first method provide better readability?
stanigator
Youb should always prefer to use vectors rather than arrays as the first-order solution - the code will be much cleaner and more readable. However, an array wrapped in a struct can easily be turned into a fairly well-defined class, so it is another viable solution. These trade-offs are things that only experience can teach, I'm afraid - there is no "right" answer.
anon
You could use structures such as tvmet::Vector [http://tvmet.sourceforge.net/] or vnl_vector_fixed [http://public.kitware.com/vxl/doc/release/core/vnl/html/index.html], they are very nice abstractions for C++ small arrays of numbers!
e.tadeu
... and what is important: gaining abstraction without losing any performance!
e.tadeu
+2  A: 

This is c++, why not try a class?

class NodeCoord { 
public:
    float coords[6];
    NodeCoord() {memset(coords, 0, sizeof(coords));};
    ~NodeCoord() {};
};

std::vector<NodeCoord> tallon;
tallon.push_back(NodeCoord());

cout << tallon.front().coords[0];

And you can do a lot more with the class, if you choose. If you wish to avoid copies, just make shallow copies of the class in a copy constructor.

plastic chris