Vector Nesting

This is a discussion on Vector Nesting within the C++ Programming forums, part of the General Programming Boards category; Hello Is there a function (call it nest for this example) that nests vectors? Like this Code: nest("char",3); // Result: ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile Vector Nesting

    Hello

    Is there a function (call it nest for this example) that nests vectors?

    Like this

    Code:
    nest("char",3); //Result: vector< vector< vector<char> > >
    nest("string",2); //Result: vector< vector<string> >
    nest("int",4); //Result: vector< vector< vector< vector<int> > > >
    nest(string type, int amount)
    Or a method like it?


    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    No... functions do not return variable types (c++ is static typed)

    Do you even know what you want to achieve with this?

    #define NEST_2(container, type) container< container< type > >
    #define NEST_3(container, type) container< container< container < type > > >

    #define NEST_VECTOR_2(type) NEST_2(vector, type)

    etc.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    Quote Originally Posted by kmdv
    Do you even know what you want to achieve with this?
    Yes, you should consider the above question. Boost.Multi-Array may have been mentioned to you before, but if not, you should take a look and see if it meets your requirements.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Post

    Is it possible to have jagged arrays using normal arrays?
    Something like

    Code:
    int i[2]; //makes i[0] and i[1]
    i[0] = new int[2]; //makes i[0][0], i[0][1]
    i[1] = new int[3]; //makes i[1][0],i[1][1],i[1][2]
    
    i[0][0] = 0;
    i[0][1] = 1;
    
    i[1][0] = 2;
    i[1][1] = 3;
    i[1][2] = 4;

    Thanks guys

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You use new on pointers, not arrays.
    Code:
    int* array[2];
    array[0] = new int[5];
    array[1] = new int[10];
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Is it possible to do something like this

    Code:
        string* str[2]; //allows str[0] & str[1]
        str[0] = new string * [2]; //allows str[0][0] & str[0][1]
        str[1] = new string * [3]; //allows str[1][0] & str[1][1] & str[1][2]
        str[0][0] = "0,0";
        str[0][1] = "0,1";
        str[1][0] = "1,0";
        str[1][1] = "1,1";
        str[1][2] = "1,2";
    Thanks

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    You can use new as many times as there are stars in your variable definition.

    It's an array of string pointers.

    string *str[2];
    str[0] = new string ("0,0");
    str[1] = new string ("0,1");

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Oh okay. I tried with 2 stars but it doesn't compile for some reason.

    Code:
        string** a[2]; //visually : { a[0] , a[1] }
        a[0] = new string[2]; //visually : { {a[0][0],a[0][1]} , a[1] }
        a[1] = new new string[2]; // visually : { {a[0][0],a[0][1]} , {{a[1][0][0],a[1][0][1]}} }
        
        a[0][0] = "0,0"; cout << a[0][0] << endl;
        a[0][1] = "0,1"; cout << a[0][1] << endl;
        a[1][0][0] = "1,0,0"; cout << a[1][0][0] << endl;
        a[1][0][1] = "1,0,1"; cout << a[1][0][1] << endl;
    If the vector is visualized, I'm going for
    { {a[0][0], a[0][1]} , {{a[1][0][0], a[1][0][1]}} }

    Errors:

    cannot convert `std::string*' to `std::string**' in assignment
    expected identifier before "new"
    expected `;' before "new"
    cannot convert `const char[4]' to `std::string*' in assignment


    Thanks

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Unless you know the number of strings you want, setting up a normal array doesn't make sense. You new[] yourself some string pointers (to serve as an array-like structure) and then new strings for yourself. As I said before you can use new as many times as there are stars in the definition... The result: you should have a matrix str[x][y].

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Have you checked out Boost.Multiarray? You are doing stuff unnecessarily complicated and shooting yourself in the foot by trying to reach the stars.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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