0

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;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
wrousseau
  • 311
  • 3
  • 12
  • I see that you have ref to vector. Perhaps the index does not match your push_back call? (Not empty on entry?) – Photon May 23 '13 at 08:42
  • No, I'm pretty sure what you are trying to do should work. You'll have to post some more code / a minimal repro. – Mike Vine May 23 '13 at 08:43
  • Did you provide a size for your vector at its construction ? – JBL May 23 '13 at 08:43
  • Indeed, the vector is passed from another function. This snipped is from a function whose goal is to `push_back` elements in it. – wrousseau May 23 '13 at 08:43
  • 1
    If you already have data in the vector your new item will go at pos *N* but you'll print out the value at pos 0 - is this your problem? Try printing out v.back().name. – Mike Vine May 23 '13 at 08:46
  • @Mike Vine, you seem to be right, it prints the correct values. I am however puzzled as for where data was previously inserted in the vector. I have added some code up there as it was requested but I'm not sure it will help much... Maybe the "resize" fills `len` elements with empty `DataBridge` ? – wrousseau May 23 '13 at 08:50

2 Answers2

1

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).

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
0

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.

Mike Vine
  • 9,468
  • 25
  • 44
  • That was exactly the issue, now fixed. Thanks a lot, I thought that by providing no secondary argument to `resize`, it would have the effect of `reserve` (I did not know about that function though). – wrousseau May 23 '13 at 08:53