seriously newbish question :)

Started by
4 comments, last by MarkS_ 11 years, 3 months ago

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.

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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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


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.

:) glad I asked. Thank you very much

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];

This topic is closed to new replies.

Advertisement