Sign in to follow this  

a vector of vectors (c++/stl)

This topic is 3462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My fear is that this is a dumb way to do this, but would it work the way I expected it would appeal to my laziness, which is why I have tried. =) On the surface, there is to be an arbitrary number of containers that a user can put an arbitrary number of things in. My instinct said to do this:
// define it thus:
vector<vector <MyObject> > v_ofa_v;

// add an item to it thus:
MyObject something = new MyObject();
(v_ofa_v[container_id]).push_back(something);
This is resulting in an immediate and complaint-free crash. If this form looks like it should work to you, then the issue could easily be with how I've defined MyObject. But I thought I'd start here, to see if 1. I'm doing this correctly, and 2. if there is some smarter way to accomplish this. Thank you in advance for your time.

Share this post


Link to post
Share on other sites
A container of containers should be fine for this; just be sure to choose (for both the 'outer' and 'inner' container) a container type that makes sense given the operations that you want to perform on it.

Your example can be re-written as:
vector<vector <MyObject> > v_ofa_v;
// ...
v_ofa_v[container_id].push_back(new MyObject());
Note that in practice you'll probably want to use a smart pointer of some type, but that's (probably) a different topic.

Other than that, I don't see anything wrong with your example [oops - except for what DriveMeFar mentioned about storing MyObject by value - but that would be a compile- rather than run-time error]. What I'd recommend though is to replace indexed access with at() here, as it's guaranteed (AFAIK) to throw an exception if the index is out of range.

If that isn't the problem, then we'll probably need to see more code.

Share this post


Link to post
Share on other sites
When you get this crash, what is container_id? What is v_ofa_v.size()?

I'm willing to bet that you're never actually adding any vector<MyObject>s to your vector of vectors, so you crash trying to index into an empty vector.

Also, your innermost vector stores MyObjects by value, yet you are inserting a pointer to a MyObject (though this is probably just a typo on your part).

Share this post


Link to post
Share on other sites
I don't see anything inherently wrong, but it would really depend on your application. Can you be more specific about what you are trying to do? If you think the problem may be with MyObject, post that as well.

Share this post


Link to post
Share on other sites
I'm gonna plead ignorance here, but I Driv3 is on the money: I am not explictly adding vectors to my vector of vectors...because I vainly hoped it would do it itself @_@. I had intended to avoid pointers here, but perhaps I've failed in that regard?

So would I need to do something like this?


v_ofa_v.pushback(new vector<MyObject>);
v_ofa_v.back.pushback(new MyObject);


It feels weird to me since no asterisks are in the definition...but I wouldn't be surprised if they were there implicitally. =\ I feel like a noob.

Share this post


Link to post
Share on other sites
Quote:
Original post by serratemplar
I'm gonna plead ignorance here, but I Driv3 is on the money: I am not explictly adding vectors to my vector of vectors...because I vainly hoped it would do it itself @_@. I had intended to avoid pointers here, but perhaps I've failed in that regard?

So would I need to do something like this?


v_ofa_v.pushback(new vector<MyObject>);
v_ofa_v.back.pushback(new MyObject);


It feels weird to me since no asterisks are in the definition...but I wouldn't be surprised if they were there implicitally. =\ I feel like a noob.


That code should work, assuming you are wanting to create a new container, then add a new MyObject. Once a container has been initialized like this, you can use it as you had originally.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sr_Guapo
Quote:
Original post by serratemplar

v_ofa_v.pushback(new vector<MyObject>);
v_ofa_v.back.pushback(new MyObject);


It feels weird to me since no asterisks are in the definition...but I wouldn't be surprised if they were there implicitally. =\ I feel like a noob.


That code should work, assuming you are wanting to create a new container, then add a new MyObject. Once a container has been initialized like this, you can use it as you had originally.


Only if you had:
std::vector< std::vector<MyObject*> *> v_ofa_v;

If you want v_ofa_v to be of type std::vector<std::vector <MyObject> >, you could do something like the following:


std::vector<std::vector<MyObject> > v_ofa_v(NUM_OBJ_CONTAINERS); //your outermost vector will contain NUM_OBJ_CONTAINERS vector<MyObject>s

v_ofa_v[index].push_back(MyObject(/*ctor params*/));

And, if you wanted to add more vectors of MyObjects after initialization:
v_ofa_v.push_back(std::vector<MyObject>());


The important thing to grasp here is that a vector of vectors is in no way different from a vector of Ts. You wouldn't do:
std::vector<int> int_v;
int_v[20] = 0;
Without making sure that there were at least 21 ints in int_v first, so don't expect it to work just because your type happens to be another vector.

Share this post


Link to post
Share on other sites
It seems to be working now =) An additional layer on the problem was that the user can arbitarily select a container (that doesn't exist yet) and dump something in it. Here was how I fixed it:


if (v_ofa_v.size() < target_index+1) // +1 to account for zero-based index
v_ofa_v.resize(target_index+1);

v_ofa_v[target_index].push_back(new MyObject(...));


Thanks all!

Share this post


Link to post
Share on other sites

This topic is 3462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this