# 2 Dimensional Vectors, I think

• 12-25-2007
bengreenwood
2 Dimensional Vectors, I think
Hi,

I'm working on a simple chat bot program that has bots that chat with each other. Currently, each bot can either insult or compliment other bots.

Now I want to make it so that each bot can remember all the moves it has received from the other bots. Say there are four bots. I'm thinking each bot will have a vector that will contain all the moves it receives from each of the other bots. So after say, three rounds, the vector will look something like this, with 5 representing a compliment and -5 an insult:

0, 0, 0, -5
0, 0, 0, 5
0, 5, 0, 5

I looked up some other threads on this board but I couldn't figure out what to do to get that kind of thing working from them. My current code is this, but it doesn't work:

Code:

```        typedef vector<int> receivedmoves(4, 0);         vector<receivedmoves> allreceivedmoves;         receivedmoves receivedmoves1;         receivedmoves1.push_back(0, 0, 0, -5);         allreceivedmoves.push_back(receivedmoves1);```
Any help would be greatly appreciated! Thanks.
• 12-25-2007
grumpy
If I'm understanding your question (and I offer no guarantees, as you have been pretty unclear), my guess at what you're trying to do is;
Code:

```//  include required headers, the following in a function somewhere;         typedef vector<int> receivedmoves;         vector<receivedmoves> allreceivedmoves;         //  create receivedmoves1 containing three 0's and one -5         receivedmoves receivedmoves1;         receivedmoves1.push_back(0);         receivedmoves1.push_back(0);         receivedmoves1.push_back(0);         receivedmoves1.push_back(-5);         // receivedmoves1.size() will have a value of 4 here         //  push receivedmoves1 as first element of allreceivedmoves         allreceivedmoves.push_back(receivedmoves1);```
• 12-25-2007
bengreenwood
Right. Thanks.

But then I also want to be able to put the other two sets of four values in there, too. I'm guessing I could just do that by clearing receivedmoves1 and putting some 0, 0, 0, and 5 in there like you did, then push_back-ing it into allreceivedmoves again. And again for 0, 5, 0, 5.

If I do that though how do I access each set of four elements inside allreceivedmoves? Say I want to access the 2nd element of the 2nd set of moves. How would I do that?

Also is there a quicker way to input the code than having to type out receivedmoves1.push_back four times for each set of four moves?

• 12-25-2007
hdragon
Quote:

Originally Posted by bengreenwood
Also is there a quicker way to input the code than having to type out receivedmoves1.push_back four times for each set of four moves?

define the numbers in an array, then run the 'for' loop and push them back.
• 12-26-2007
bengreenwood
Let's say I make another receivedmoves vector in the same way as this one:

Code:

```receivedmoves receivedmoves1;         receivedmoves1.push_back(0);         receivedmoves1.push_back(0);         receivedmoves1.push_back(0);         receivedmoves1.push_back(-5);```
except it would be receivedmoves2. How would I go about accessing the elements within each of these vectors? I want to be able to create and access a basically limitless number of new vectors within allreceivedmoves.

EDIT---

Actually this is dumb. I've just realised a far simpler way for me to do this is to just have one vector that is a list of the received moves, another that is a list of the id values of who performed those moves, and possibly a third for putting in when those moves were done, so that if a bot receives two moves at once it can remember.
• 12-26-2007
grumpy
It would have been helpful, for your learning purposes, if you'd started with simple vectors (rather than vectors of vectors) and understood those first. If you can't work with a simple std::vector<int> you have no hope of being able to work with a std::vector of std::vector's.

Code:

```  allreceivedmoves.push_back(receivedmoves1);   allreceivedmoves.push_back(receivedmoves2);```
then allreceivedmoves will contain two vector<int>'s. The first element of allreceivedmoves will be a copy of receivedmoves1 and the second will be a copy of receivedmoves2.

So;
Code:

`  allreceivedmoves[0]`
will access the first set of moves, and
Code:

`  allreceivedmoves[1]`
will access the second set of moves. To access elements within each set (eg to print them out);
Code:

```// assume <iostream> has been #include'd   for (int i = 0;  i<allreceivedmoves.size(); ++i)   {         std::cout << "Moves set " << i << ":"         for (int j = 0; j < allreceivedmoves[i].size(); ++j)         {             std::cout << ' ' << allreceivedmoves[i][j];         }         std::cout << std::endl;   }```
I'll leave out an example of doing this with iterators (a canonical way of working with vectors that can involve a little more code, but is invariably safer and more powerful).

The basic thing you need to get into your head is that you're working with vectors of vectors here rather than true multi-dimensional arrays.

EDIT: you may be right that there will be ways to optimise how your code works. But you're doing premature optimisation. If you can't get something working correctly, you will have no hope of optimising it.
• 12-26-2007
bengreenwood
Hey, thanks for that Grumpy, that's a really good explanation of exactly what I was wondering about vectors of vectors.