A clever way to name unknown amount of variables

This is a discussion on A clever way to name unknown amount of variables within the C++ Programming forums, part of the General Programming Boards category; Hi dee Ho again people! I have a lil problem. I'm trying to do a vector containing vectors... And in ...

  1. #1
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194

    A clever way to name unknown amount of variables

    Hi dee Ho again people!

    I have a lil problem. I'm trying to do a vector containing vectors... And in case some of you visit multiple boards as I do, yes I did this same post on one another board... Reason is that I need to solve the problem. asap.
    Well, basically there's no problem, but... I do not know how many vectors the vector containing vectors will contain

    Problem is that I do not have those vectors already created, so I should first create right amount of vectors inside a vector, before starting to feed values in them.

    I tried following:

    Code:
    vector< vector<string> > mainvector;
    vector<string> *col_na = new vector<string>[ncols];
    //I tried to do an array of string type vectors first, and there the ncols is int type variable telling how many of those I need.
    for(i=0;i<ncols;i++)
    {
            mainvector.push_back(*(col_na+i));
    }
    // there the mainvector should now contain those other vectors.

    This compiles ok with -Wall flag, but when I run it only thing happens is

    matti@linux:~> ./test
    Aborted
    matti@linux:~>

    Any ideas how to do this? Is there a way to use something stored in a stringvariable as a name for those vectors I wish to store inside the mainvector, instead of using array of pointers?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Compiles and runs fine for me. Your error is somewhere else.

    But your description is not very clear. What exactly is it that you want?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    isn't that the same as something like this?
    Code:
    typedef vector<string> COLS;
    
    vector<COLS> array;
    
    array.resize(n); // now array will contain n number of COLS vectors
    ;

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    There is no need to use new. This works:
    Code:
    vector< vector<string> > mainvector(ncols, vector<string>());
    If you don't know what ncols is at the time you construct mainvector, then you can use resize, or create vector<string>s on the stack and use push_back to add (copies of) them to the mainvector.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    A clever way to name an unknown amount of variables?

    That statement isn't clever in itself much less the method that I still haven't seen anywhere in this post.

    When do you have an unknown amount of variables?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  4. I need help badly
    By taz_jiggy in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 09:36 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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