Thread: Vector Nesting

  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
    733
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613

  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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