madalina
madalina

Reputation: 377

initialize variable involving vector data type

I have the following data types and variables:

typedef Seq< vector<int> > MxInt2d;
typedef std::vector<int>  edge_t;
typedef std::vector< edge_t> edge2d_t;

std::vector< edge2d_t > myEdgesIntersect;

I tried to initialize myEdgesIntersect like:

edge2d_t edge2d(2);

 //creating the vector of edges of intersections whenever an intersection is detected
for (int i=0;i<1;i++){
    edge2d[0][0]=sweepEvents[i][0];
    edge2d[0][1]=sweepEvents[i][1];
    edge2d[1][0]=sweepEvents[i+1][0];
    edge2d[1][1]=sweepEvents[i+1][1];
    std::cout<<edge2d[0][0]<<" "<<edge2d[0][1]<<endl;
    std::cout<<edge2d[1][0]<<" "<<edge2d[1][1]<<endl;
    myEdgesIntersect.push_back(edge2d);
    std::cout<<myEdgesIntersect[i][0][0]<<" "<<myEdgesIntersect[i][0][1]
            <<"    "<<myEdgesIntersect[i][1][0]<<" "<<myEdgesIntersect[i][1][1]<<endl;
}

But using this syntax when I try to display the variable myEdgesIntersect this is not initialized with the given values of edge2d[..][..] (which during the display are okay). I tried to display the variable myEdgesIntersect before the push_back and I got an bus error, so I think the problem is that the variable is not initialized. I tried to initialize it like:

 edge2d_t edge2d;
 edge2d[0][0]=0;
 edge2d[0][0]=0;
 edge2d[0][0]=0;
 edge2d[0][0]=0;
 edge2d[0][0]=0;
 myEdgesIntersect.push_back(edge2d);

but I got the same error, as actually is the same thing as in the loop. Apparently I do not know how to initialize this quite complicated variable that I really need. If you have any suggestions I would be more than happy.

thanks in advance, madalina

Upvotes: 0

Views: 405

Answers (4)

veefu
veefu

Reputation: 2890

In addition to what John said, I suspect your 'for' loop may have an off-by-one error:

for (int i=0;i<1;i++){ // i will only be 0

perhaps you want

    for (int i=0;i<=1;i++){ // i will iterate 0,1

Upvotes: 1

dirkgently
dirkgently

Reputation: 111130

Try:

edge2d_t ev(10, edge_t(10, 0));

(change the size from 10 to something that fits you.)

Upvotes: 1

John Dibling
John Dibling

Reputation: 101456

edge2d_t is a vector of vectors. In your first code block you set the size of the outer vector when you instantiate your edge2d variable, but not the inner vectors, so they are all size 0.

try this:

edge2d_t edge2d(2);
edge2d[0].resize(2);
edge2d[1].resize(2);

 //creating the vector of edges of intersections whenever an intersection is detected
for (int i=0;i<1;i++){
        edge2d[0][0]=sweepEvents[i][0];
        edge2d[0][1]=sweepEvents[i][1];
        edge2d[1][0]=sweepEvents[i+1][0];
        edge2d[1][1]=sweepEvents[i+1][1];
        std::cout<<edge2d[0][0]<<" "<<edge2d[0][1]<<endl;
        std::cout<<edge2d[1][0]<<" "<<edge2d[1][1]<<endl;
        myEdgesIntersect.push_back(edge2d);
        std::cout<<myEdgesIntersect[i][0][0]<<" "<<myEdgesIntersect[i][0][1]
            <<"    "<<myEdgesIntersect[i][1][0]<<" "<<myEdgesIntersect[i][1][1]<<endl;
}

Upvotes: 1

pauljwilliams
pauljwilliams

Reputation: 19225

If your array size really is fixed at compile time then you may be better off looking at a 2D array, rather than a 2 element vector containing 2 2 elemnet vectors.

Upvotes: 0

Related Questions