I am trying to learn about vector<vector<int>> some_name and im not finding much information about accessing the elements. I got some decently obscure information pointing towards using commands as such as a multidimensional array but theres no bounds checking from what I can see and that kinda worries me, I would rather use the built in commands (push_back and so fourth) but I simply cant find any straight forward information on how to run a command (such as push_back) on one of the "sub-vectors" if I could use that term.
I know im asking a lot of questions here, and thank you for everyone who is helping. This forum is doing much for my adventures in c++. hopefully some day I can return the favor and answer someone elses question!
Seeking information on multidimensional vectors
Page 1 of 111 Replies - 260 Views - Last Post: 10 January 2013 - 12:58 PM
Replies To: Seeking information on multidimensional vectors
#2
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 10:30 AM
You could create a multidimensional vector and that can be useful if you need to iterate over the entire array or if you need to do some matrix manipulation, but it's often easier to use a single dimension vector of a class or struct.
An example of both:
An example of both:
#include <iostream>
#include <vector>
using namespace std;
class two_d {
public:
int row, col;
two_d(int r, int cl) {
row = r;
col = cl;
}
};
int main(int argc, const char * argv[])
{
vector < vector <int> > twoVec;
vector <int> sample;
twoVec.push_back(sample);
twoVec.push_back(sample);
twoVec[0].push_back(1);
twoVec[0].push_back(2);
twoVec[1].push_back(3);
twoVec[1].push_back(4);
cout <<"A 2d vector of type int: \n";
cout << twoVec[0][0] << " " << twoVec[0][1] << endl;
cout << twoVec[1][0] << " " << twoVec[1][1] << endl;
vector <two_d> usually_better;
usually_better.push_back(two_d(1, 2));
usually_better.push_back(two_d(3, 4));
cout << "A 1d vector of type two_d:\n";
cout << usually_better[0].row << " "<< usually_better[0].col << endl;
cout << usually_better[1].row << " "<< usually_better[1].col << endl;
return 0;
}
#3
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 10:31 AM
Quote
I would rather use the built in commands (push_back and so fourth) but I simply cant find any straight forward information on how to run a command (such as push_back) on one of the "sub-vectors" if I could use that term.
Say you have a vector of 5 integer vectors and you want to push_back on the third. The third vector can be accessed using the command "myvector[2];". since this command returns a vector, you can simply call any member of vector off of this command. For example, "myvector[2].push_back(7);". This adds a 7 to the end of the 3rd vector.
#4
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 11:28 AM
Thanks guys! this current implementation is a basic x and y room, a 2d matrix if you will, and im wondering...
would be a logical and "resource applicable" way to make a list for a 5x5 grid, starting at 0,0 and ending at 5,5? correct me if you can think of something better... or if that makes no sence =+)
#include <iostream>
#include <vector>
using namespace std;
class two_d {
public:
int row, col;
two_d(int r, int cl) {
row = r;
col = cl;
}
};
int main()
{
vector<two_d> sample_grid
int increment_x = 0
int increment_y = 0
if (increment_x == 0, increment_x == 5, increment_x++)
{
if (increment_y == 0, increment_y == 5, increment_y++)
{ sample_grid.push_back(increment_x, increment_y) }
}
}
would be a logical and "resource applicable" way to make a list for a 5x5 grid, starting at 0,0 and ending at 5,5? correct me if you can think of something better... or if that makes no sence =+)
#5
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 11:35 AM
What do you plan on doing with this grid?
#6
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 11:39 AM
TBH its homework, the job is to establish classes of rooms, tools, puzzles, nouns, and verbs for a program I asked a tutor to help me make so I can learn c++, the end project is a console based adventure =+)
this is for the rooms, im trying to have a class that I can call to quickly set up a room where the player is (suppose to be) in one part of the room at all times, the room itself is a 3x3 grid but im using 5 because.... well I dont know why
this is for the rooms, im trying to have a class that I can call to quickly set up a room where the player is (suppose to be) in one part of the room at all times, the room itself is a 3x3 grid but im using 5 because.... well I dont know why
#7
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 11:46 AM
So then you want the room to be a 3x3 grid of a class/struct that can hold tools, puzzles, etc? You could have a vector of rooms where rooms are a class or struct that might have a two dimensional array containing another class/struct which would hold the different objects that might be in the room.
#8
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 11:56 AM
thats the idea, but the start of it all as I can see is making the nine space room, adding the ability to be in one of them (and move between them), then going back over the code after I have some tool classes mixing the two, at least thats the idea. am I wrong? the tutor suggested I work on making all the classes seperate first, if only to learn about classes and do stuff like this, going on websites and learning stuff 
My full code so far if it helps clarify things...
the idea I had when I started was making the code easily modifiable to add rooms, tools, etc.
My full code so far if it helps clarify things...
// Global Definitions
// An enumeration of each type of object in the game
#include <iostream>
#include <vector>
using namespace std;
enum OBJECT_TYPE // Creation of a data type
{
// Individual data types
OT_ROOM,
OT_TOOL,
OT_PUZZLE,
OT_NOUN,
OT_VERB,
OT_MAX_OBJECT_TYPE
};
class object_id // Class to call when using an object from OBJECT_TYPE
{
public: // Seen and used by any outside source
object_id() // Default constructor - Called if nothing else can be
{
type_of_object = OT_MAX_OBJECT_TYPE; // Makes type_of_object (as in private) the value of OT_MAX_OBJECT_TYPE, or the max and illigitimate value
}
object_id(OBJECT_TYPE intype/*type of object asked for*/) // Primary constructor - called when requested with a defining value from the enum
{
type_of_object = intype; // Assigns type_of_object the value sent to the class when it was called
switch (type_of_object)
case OT_TOOL:
tool.tool(true);
}
~object_id(); // Destroys the object
private: // seen only by the class itself
OBJECT_TYPE type_of_object; // Declares an enum value called type_of_object
};
class tool : public object_id // A class that interacts with the OBJECT_ID class as its parent
{
public: // Seen and used by any outside source
tool() : object_id(OT_TOOL) // Default constructor called by OBJECT_ID with a value of OT_TOOL
{
// ERROR CODE
}
tool(bool use) : object_id(OT_TOOL) // Constructor called if there has been a command to use the tool
{
if (use == true) in_use = true; // if constructor has been called with use being true, set in_use to true
if (use == false) in_use = false; // if constructor has been called with use being false, set in_use to false
}
~tool();
private: // seen by only the class itself
bool in_use; // Is the tool being used
};
class room : public object_id // Class for a room, sub-class of object_is
{
public: // seen by any other class
room() : object_id(OT_ROOM) // Default constructor, called if room class is called with out inpur
{
// ERROR CODE
}
room(bool enter) : object_id(OT_ROOM) // constructor to call is a boolean value is present, indicating in the room or not
{
if (enter == true) in_room = true; // if the boolean value is true, assign it true
if (enter == false) in_room = false; // if the boolean value is false, assign it false
}
room(int size_x, int size_y) : object_id(OT_ROOM) // constructor to call if two integers are present, aka the size of the room
{
//temporary code for practice with multidimensional vectors
int num = 5;
vector<int> temp;
room_size.push_back(temp);
room_size[1].push_back(num);
}
~room();
private:
bool in_room; // private data indicating if the player is in the room or not
vector<vector<int>> room_size; // multidimensional vector of room size, length and width
}
the idea I had when I started was making the code easily modifiable to add rooms, tools, etc.
#9
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 12:04 PM
Your tutor is right. Work on the classes first. Then think of the classes as if they were types like any other. You can have an array or vector of any type, and a class can contain any type. That means you can have a vector of classrooms and each classroom can have its own array or vector of another class (room_objects?). Where you "are" would be the classroom number and the position in that classroom's grid.
#10
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 12:21 PM
room(int size_x, int size_y) : object_id(OT_ROOM)
{
//temporary code for practice with multidimensional vectors
int num = 5;
vector<int> temp;
room_size.push_back(temp);
room_size[1].push_back(num);
}
~room();
private:
bool in_room;
vector<vector<int>> room_size;
Is my way of saying "call this to make a room with an x,y size" by telling it to modify vector<vector<int>> room_size in this case to a 5x5. Originally I was going to make a vector of vectors, but now im seeing my implementation was wrong, instead of making room_size with sub vectors like that im thinking from reading all of this that it would be better to make a vector of a class containing two values, x and y
class room_dimensions {
public:
int inX, inY;
room_dimensions (int x, int y)
{
inX = x;
inY = y;
}
}
...
room(int size_x, int size_y) : object_id(OT_ROOM)
{
vector<two_d> sample_grid
int increment_x = 0
int increment_y = 0
if (increment_x == 0, increment_x == size_x, increment_x++)
{
if (increment_y == 0, increment_y == size_y, increment_y++)
{ sample_grid.push_back(increment_x, increment_y) }
}
would make a list of
0x0, 0x1...
but is that really the best way to do it if there is stuff going in the rooms? the way I see it I would have to some way turn 0x0 into a container of some sort, im thinking there may be a way to make it a container when its initially created rather than figuring out the extra code after the fact?
#11
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 12:27 PM
EDIT: theres a typo in my code.
vector<two_d> sample_grid
is actually
vector<room_dimensions> sample_grid
sry I dont know how to modify a post on these boards
vector<two_d> sample_grid
is actually
vector<room_dimensions> sample_grid
#12
Re: Seeking information on multidimensional vectors
Posted 10 January 2013 - 12:58 PM
That could work. Here's something I threw together:
It's just a start, but hopefully it will make things a little clearer.
#include <iostream>
#include <vector>
using namespace std;
struct tools {
string tool_name;
tools(string tl) {
tool_name = tl;
}
string get_tool() {
return tool_name;
}
void use_tool() {
// for later use.
}
};
struct rooms {
vector < vector <tools> > thetools;
int row, col;
rooms() {
vector<tools> tl;
tl.push_back(tools("tool1"));
tl.push_back(tools("tool2"));
tl.push_back(tools("tool3"));
thetools.push_back(tl);
thetools.push_back(tl);
thetools.push_back(tl); // I'm lazy, so I'm filling the vector with the same tools.
}
void set_tool(int rw, int cl, string toolname) {
thetools[rw][cl].tool_name = toolname;
}
void show_room() {
for (long row = 0; row < thetools.size(); row++) {
for (long col = 0; col < thetools[row].size(); col++) {
cout << row <<", "<< col << ": " << thetools[row][col].tool_name << " \t";
}
cout << endl;
}
}
};
int main(int argc, const char * argv[])
{
vector <rooms> classrooms;
classrooms.push_back(rooms());
classrooms.push_back(rooms());
for (long room_number = 0; room_number < classrooms.size(); room_number++) {
cout <<"Room number: "<< room_number << endl;
classrooms[room_number].show_room();
cout << endl;
}
return 0;
}
It's just a start, but hopefully it will make things a little clearer.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|