• Advertisement
Sign in to follow this  

seriously newbish question :)

This topic is 1840 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

can you encapsulate a vector inside another vector? im trying to make five "rooms" each with nine "sections", that being the 8 directions and the center, but I just realized that I have no idea if I actually can, and if so, how :-), I want to be able to access and modify the strings within the room(section) easily.

 

I hope this makes sense, I am new to programming.

 

thx.

Share this post


Link to post
Share on other sites
Advertisement

If you're asking if you can have a "vector of vectors", yes.  Otherwise it just sounds like you're talking about generic data encapsulation.

 

Perhaps you're thinking about a list/vector of Room objects, and each Room object contains a list/vector of Section objects?  That's definitely allowable.  You can nest objects and data many, many levels deep depending on how your object composition is set up.

Share this post


Link to post
Share on other sites

I just found an answer,

 

vector<vector<string>> room[10];

 

and supposudly to access it one would use sub-brackets such as

 

room[4].resize(9);

 

to make the fourth room a size of 9 or simply

 

room.resize(4); to make four rooms.

 

Confirmation would be great :)

Share this post


Link to post
Share on other sites
vector<vector<string>> room[10];

 

 

This code would create an array of 10 vectors of vectors of strings.


I think you want this:

 

vector<vector<string>> room(10);

 

 

This would create an vector of vectors of strings, instantiated with 10 elements.

Edited by kidman171

Share this post


Link to post
Share on other sites

May I ask why you want to use vectors here? If you have five rooms and each room contains nine sections, the added overhead from the dynamic array would be overkill. A simple 2D array would suffice, e.g., string room[5][9];

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement