Question

I'm having a little issue with vectors. From what I understand, push_back() will copy the argument and insert the copy in the vector.

struct BridgeData
{
    string name;
    string type;
};

For a simple aggregate class such as mine, the copy of an object should be made by copying both fields.

for ( int i = 0; i<len; i++)
{
    BridgeData data = {grp.get(1+i).asString().c_str()};
    v.push_back(data);
    cout << v[i].name << endl;
}

with vector<BridgeData> &v.

When I print data.name, I get the value I used in the braced list but when I print v[i].name, the field appears to be empty... Is the default copy "constructor" for such aggregate classes default initializing all fields?

EDIT:

Here's more code if that was not enough. I've got a class which contains as a data member vector<BridgeData> yarpGroups. I then pass it as a reference in the body of a method from the same class : readBridgeDataVector(bGeneral,"yarpgroups",yarpGroups,numberOfYarpGroups);. Please ignore the other arguments as they are irrelevant (I am sure of it). The earlier snipped is from this function :

void readBridgeDataVector(Bottle &rf, string name, vector<BridgeData> &v, int len)
{
    v.resize(len);
    if(rf.check(name.c_str()))
    {
        Bottle &grp = rf.findGroup(name.c_str());
        for ( int i = 0; i<len; i++)
        {
            BridgeData data = {grp.get(1+i).asString().c_str(),"float"};
            v.push_back(data);
            cout << v[0].name << endl;
        }
    }
    else
    {
        cout << "Could not find parameters for " << name << ". "
            << "Setting everything to null by default" << endl;
    }
}
Was it helpful?

Solution

You've resized the vector to size len.

This crates len objects in the vector using their default constructor.

So when you pushback() another object it's in place len+1.

The object in cell 0 is actually one of the default constructed objects.

What I think you wanted to do is use reserve() just to have enough room for the objects.


vector::resize()

If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

vector::reserve()

If n is greater than the current vector capacity, the function causes the container to reallocate its storage increasing its capacity to n (or greater).

OTHER TIPS

You are resizing your vector to len empty elements

You are then adding a new item (via push_back) at position len

You then print item at position 0 which is one of your original empty elements

Did you mean to reserve and not resize?

(Note: reserve() is generally not recommended though. Just leave your vector empty and push_back() the new elements you need). Also if you need to get the last element just use v.back() rather than your assumed index for ease of use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top