2 Dimensional Vectors, I think

This is a discussion on 2 Dimensional Vectors, I think within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm working on a simple chat bot program that has bots that chat with each other. Currently, each bot ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Question 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.
    Last edited by bengreenwood; 12-25-2007 at 02:47 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    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);

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    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?

    Thanks for your help.

  4. #4
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by bengreenwood View Post
    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?

    Thanks for your help.
    define the numbers in an array, then run the 'for' loop and push them back.
    Hello, testing testing. Everthing is running perfectly...for now

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    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.
    Last edited by bengreenwood; 12-26-2007 at 05:45 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    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.

    Assuming you start with allreceivedmoves an empty (e.g. newly constructed) vector, if you do
    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.
    Last edited by grumpy; 12-26-2007 at 02:36 PM.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Hey, thanks for that Grumpy, that's a really good explanation of exactly what I was wondering about vectors of vectors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  5. multi dimensional vectors
    By r0bbb in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2005, 07:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21