Thread: vector of 2d array elements?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    14

    vector of 2d array elements?

    Hello,

    I want to make a vector of 2d arrays. How do I do that. I tried different things... like

    Code:
    vector <char *> vector_x;
    vector_x.pushback(new char[13][2]);
    but that did not work.

    Also, how would specific elements be utilized? For the code above I tried using:
    Code:
    b_file>>*vector_x[index_i][index_y][0];
    I hope my n00bness does not make you laugh too hard.

    thanks
    J

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You need to use a double pointer, as I call it, or a pointer to a pointer. However I would much rather use a vector of a vector of a string. Pretty much the same job but much simpler.

    Code:
    std::vector<char**> vector_x;
    std::vector<std::vector<std::string> > vector_y;

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    14
    Desolation,

    Thank for the help.

    The pushback did not work though. Got the same compiler error. Here is the actual code I am using.

    Code:
    vector <char **> HAND;
    while (!b_file.eof())
                    {
                     HAND.push_back(new char[13][2]);

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    14
    Desolation,

    Another question. For your vector of a vector suggestion, how would I utilize it, e.g. how would I store a string?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends on what you're trying to do. Here's one example.
    Code:
    vector<vector<string> > HAND;
    string word1;
    string word2;
    while (b_file >> word1 >> word2)
    {
      vector<string> item;
      item.push_back(word1);
      item.push_back(word2);
      HAND.push_back(item);
    }
    Notice how I removed the eof() check in your code. That is generally bad practice and will often lead to incorrect input. If you're using operator>> or getline you can put that call inside the while control instead.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    1. Can you be more precise than "doesn't work" because that doesn't mean anything to me. You're lucky that I know the answer though. You need to do something like this:
    Code:
    std::vector<char**> HAND;
    while(!b_file.eof() )
    {
        char** tmp = new char*[13];
        for(int i = 0; i < 13; i++)
            tmp[i] = new char[2];
        HAND.push_back(tmp);
    }
    Of course, you need to delete all those arrays somewhere in your application as well.

    Code:
    std::vector< std::vector< std::string > > HAND;
    while (!b_file.eof())
    {
        HAND.push_back(std::vector< std::string > ( ));
        // HAND.back().push_back("foo")
    }
    Added that comment bit so you can see how to add a string but you don't need to initialize memory or whatever. =)

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    14
    Thank you very much for all the information. You are a gentleman and a scholar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM