简体   繁体   中英

In C++, How do I store values into a vector that is inside of a vector when the two vectors are of different types?

I am writing a program where I need to use the following data structure:

struct shape
{  
    std::vector<float> verts; // contains the x & y values for each vertex   
    char type;                // the type of shape being stored.   
    float shapeCol[3];        // stores the color of the shape being stored.   
    float shapeSize;          // stores the size of the shape if it is a line or point
};

In my main program I need a vector of type shape . How would I store values into the vector inside of the struct shapes using the the vector of struct shapes.

For instance, vector<shape> myshapes ;

If I wanted to store a value into the first index of my verts vector, inside of my first index of my myshapes vector how would I do this?

in pseudo code it would look something like this, with i being the index:

myshapes[i].vector[i] = 4;   // but I know this is incorrect

Would this be easier to implement using a STL list instead and if so what would that syntax look like?

Thanks for the help I am new to vectors so any advice would be appreciated.

vector supports the use of the [] operator. The syntax and semantics are very similar to using the [] operator with arrays. See: http://en.cppreference.com/w/cpp/container/vector/operator_at .

As with any struct member, you need to access it by name. myshapes[i].verts[j] = 4; .

The general advice given is to use std::vector as your default container of choice. Naturally if you have specific needs (like adding/removing items in the middle of the container) other containers may have better performance characteristics.

If your vector(s) start out empty, you'll have to add elements to them before you can index into them with operator[] . This is usually done with push_back (to add an existing shape object) or emplace_back (to construct a new shape object directly in the vector).

Given vector<shape> myshapes , you could add some shapes like this:

// add 10 shapes
for (size_t n = 0; n < 10; n++) {
    shape s; // creates a new, blank shape object

    // initialize the shape's data
    s.type = ...;
    s.shapeSize = ...;
    // etc.

    // add verts
    s.verts.push_back(1.0f);
    s.verts.push_back(2.0f);
    s.verts.push_back(3.0f);
    // etc.

    // add the shape to the vector
    myshapes.push_back(std::move(s));
}

(Since we're done with s on that last line, we can use std::move . This allows push_back to move the shape's data into the vector instead of copying it. Look up move semantics for more info.)

Once you have stuff in the vector, you can access elements by index like this:

myshapes[index of shape].verts[index of vertex within that shape]

Using [] with an invalid index or when the vector is empty invokes undefined behavior (don't do it or your program will crash/malfunction).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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