CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Vector list

  1. #1
    Join Date
    Feb 2004
    Posts
    138

    Vector list

    I would like to use vector or list (or anything like that) as a multidimentional array, is it possible ? If so, how can I do that ?
    For example, how can I change the following code into "something similar" but use vector/list/...
    PHP Code:
    string a[100][100][100]; 
    Thanks

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vector list

    Originally posted by Charleston
    I would like to use vector or list (or anything like that) as a multidimentional array, is it possible ? If so, how can I do that ?
    For example, how can I change the following code into "something similar" but use vector/list/...
    PHP Code:
    string a[100][100][100]; 
    Thanks
    Code:
    #include <vector>
    #include <string>
    
    typedef std::vector<std::string>S1;
    typedef std::vector<S1> S2;
    typedef std::vector<S2> S3;
    
    int main()
    {
        S3 a(100, S2(100, S1(100)));
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2004
    Posts
    138
    Originally posted by Paul McKenzie
    Code:
    #include <vector>
    #include <string>
    
    typedef std::vector<std::string>S1;
    typedef std::vector<S1> S2;
    typedef std::vector<S2> S3;
    
    int main()
    {
        S3 a(100, S2(100, S1(100)));
    }
    Regards,

    Paul McKenzie
    Thanks Paul very much,
    But I still have another question that, I do not know how to use that array a, if it is declared as what you have taught me, would you please spend some time explaining a little more ??
    Thank You,

    Regards,

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    Use it just like any other 3-dimensional array:
    Code:
    a[0][0][0] = "Test";
    You also have the benefit of the std::vector functions to resize any dimension, etc.

    Basically, I created a vector of a vector of a vector of strings. This is equivalent in concept to your three dimensional array of strings.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2004
    Posts
    138
    Originally posted by Paul McKenzie
    Use it just like any other 3-dimensional array:
    Code:
    a[0][0][0] = "Test";
    You also have the benefit of the std::vector functions to resize any dimension, etc.

    Basically, I created a vector of a vector of a vector of strings. This is equivalent in concept to your three dimensional array of strings.

    Regards,

    Paul McKenzie
    Thanks Paul so much, CG is really lucky to have a guru like You, and I am also lucky to be helped...True.

    Regards,

  6. #6
    Join Date
    Feb 2004
    Posts
    138
    I would like to have another question, would Paul or anyone please tell me how I can use push_back in this case ?

    Thanks,

  7. #7
    Join Date
    Feb 2004
    Posts
    138
    What I mean is like this, for example, I have a 2d array, and if I just use an C-style array, I would do something like
    PHP Code:
    for(int i=0;i<n;i++)
        for(
    int j=0;j<n;j++)
            
    a[i][j]=i+j
    But since I have just been taught about using vector of vector of string, I would like to use push_back and even some std functions like transform, I dont know how I can make it now
    If it is just a normal 1d vector I can try to handle it, but now it gets doubled (vector<vector>),I am stuck,
    I can do as what Paul told me but I would like to know a little more, would anyone help me ?

    Thanks

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    multi-dimensional vectors are tricky to work with. The
    important thing to remember is that they are "vector
    or vectors".

    for example :

    Code:
    typedef std::vector<int>I1;
    typedef std::vector<I1> I2;
    
    I2 v;
    V[i][j] is an int

    but each indivdual element is a vector<int>

    v[i] is a vector<int>

    likewise, when using iterators in one of the algorithms,
    the elements are vetcor<int>. See example below:

    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    
    using namespace std;
    
    typedef std::vector<int>I1;
    typedef std::vector<I1> I2;
    
    template <typename T>
    struct MultiplyVector
    {
        MultiplyVector(T factor) : m_factor(factor) {}
    
        void operator () (I1 & v) // v is a vector<int>
        {
            std::transform(v.begin(),v.end(),v.begin(),std::bind2nd(std::multiplies<T>(),m_factor));
        }
    
        T m_factor;
    };
    
    int main()
    {
        I2 v;
    
        I1 va;
        va.push_back(0);
        va.push_back(1);
    
        v.push_back(va);
    
        I1 vb;
        vb.push_back(5);
        vb.push_back(6);
        vb.push_back(7);
    
        v.push_back(vb);
    
        int i,j;
    
        for (i=0; i<v.size(); ++i)
        {
            for (j=0; j<v[i].size(); ++j)
            {
                cout << "v[" << i << "][" << j << "] = " << v[i][j] << endl;
            }
        }
    
    
        // multiply everything by 2 using for_each
    
        for_each(v.begin(),v.end(),MultiplyVector<int>(2));
    
        cout << "\n\nafter for_each\n\n";
    
        for (i=0; i<v.size(); ++i)
        {
            for (j=0; j<v[i].size(); ++j)
            {
                cout << "v[" << i << "][" << j << "] = " << v[i][j] << endl;
            }
        }
    
        return 0;
    }

  9. #9
    Join Date
    Feb 2004
    Posts
    138
    I thought Philip didnot like me, thats why Philip didnot help me,

    Now many many thanks to You,
    You are really very kind, True ! I see it in the way You behave to people on boards.

    Once again, Thank Philip so very much,

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    In general, a basic understanding of the 'vector' class should help...take a look at the following introduction...

  11. #11
    Join Date
    Feb 2004
    Posts
    138
    Originally posted by Andreas Masur
    In general, a basic understanding of the 'vector' class should help...take a look at the following introduction...
    Thank Andreas a lot, I didnot pay attention to that link, I didnot know there is one out there,
    But I think Philip's post is great and specific especially to the example I gave out, it may be a cake to many gurus like Andreas, but to a newb like me, it is not at all, and I like his solutions, True !

    Once again, Thank Andreas a lot, True

  12. #12
    Join Date
    Feb 2004
    Posts
    138
    Philip,
    I would like to ask for another favor,
    Philip gave me that solution but it is just for matrix 2xN, how about MxN matrix,I donot know how to do that, would Philip be nice to help me again ???

    Thanks a lot

  13. #13
    Join Date
    Feb 2004
    Posts
    138
    Anyone is Okay, please help me...

    Thanks

  14. #14
    Join Date
    Feb 2003
    Posts
    377
    Philip's code works fine for MxN matrices. In his example, M just happens to be 2 because he only pushed back twice.

    You could use a for loop to initialize each entry to a specific value:
    Code:
        const int M = 26;
        const int N = 38;
    
        I2 v;
        for (i=0; i<M; ++i)
        {
            I1 va;
            for (j=0; j<N; ++j)
            {
                va.push_back(i);
            }
            v.push_back(va);
        }
    or if you can initialize them all to the same value then:
    Code:
        const int M = 26;
        const int N = 38;
    
        // Initialize all to 17.
        I2 v(M, I1(N, 17));
    
        // Initialize all to 0.
        I2 v2(M, I1(N));

  15. #15
    Join Date
    Feb 2004
    Posts
    138
    Originally posted by jlou
    Philip's code works fine for MxN matrices. In his example, M just happens to be 2 because he only pushed back twice.

    You could use a for loop to initialize each entry to a specific value:
    Code:
        const int M = 26;
        const int N = 38;
    
        I2 v;
        for (i=0; i<M; ++i)
        {
            I1 va;
            for (j=0; j<N; ++j)
            {
                va.push_back(i);
            }
            v.push_back(va);
        }
    or if you can initialize them all to the same value then:
    Code:
        const int M = 26;
        const int N = 38;
    
        // Initialize all to 17.
        I2 v(M, I1(N, 17));
    
        // Initialize all to 0.
        I2 v2(M, I1(N));
    Thanks jlou a lot, I was trying to make it work, but I couldnot, I didnot understand his code could work for NxM matrix,
    Thanks jlou a lot, you helped me out...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured