#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    6
    Rep Power
    0

    array declaration


    I was wondering if there was a way to declare a 3 dimensional array of strings in c++ similar to the way you would in php.
    myarray = (
    "1234"
    "23232",
    (
    "1234"
    "23232",
    (
    "1234"
    "23232",
    )
    )
    )

    Im just learning c++ so bear with me here. :D
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    I don't know of an easy way to do so by statically allocating memory. In C++ there is the vector class and you can have vectors of vectors, so you could have multidimensional arrays that way and not have to endure the tedium of allocating your own memory. For instance:

    Code:
    #include <stdio.h>
    #include <vector>
    
    using namespace std;
    
    int main(){
        vector < vector < string > > myVect;
        int i, j;
    
        //stick some data in...
        for (i=0; i<2; i++){
            vector < string > innerVect;
            for (j=0; j<2; j++){
                innerVect.push_back("Inner Inner vector");
            }
            innerVect.push_back("Inner vector");
            myVect.push_back(innerVect);
        }
    
        printf("read the data out (using iterators)\n\n");
        vector < vector < string > >::iterator outterIter = myVect.begin();
        while(outterIter != myVect.end()){
            vector < string >::iterator innerIter = outterIter->begin();
            while(innerIter != outterIter->end()){
                printf("\t%s\n", innerIter->c_str());
                innerIter++;
            }
            outterIter++;
        }
    
        printf("\n\nread the data out (using indexing)\n\n");
        for (i=0; i<myVect.size(); i++){
            for (j=0; j<myVect[i].size(); j++){
                printf("\t%s\n", myVect[i][j].c_str());
            }
        }
    
        return 0;
    }
    Output:

    Code:
    read the data out (using iterators)
    
            Inner Inner vector
            Inner Inner vector
            Inner vector
            Inner Inner vector
            Inner Inner vector
            Inner vector
    
    
    read the data out (using indexing)
    
            Inner Inner vector
            Inner Inner vector
            Inner vector
            Inner Inner vector
            Inner Inner vector
            Inner vector

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11

    Re: array declaration


    Originally posted by Kefka.com
    I was wondering if there was a way to declare a 3 dimensional array of strings in c++ similar to the way you would in php.
    myarray = (
    "1234"
    "23232",
    (
    "1234"
    "23232",
    (
    "1234"
    "23232",
    )
    )
    )

    Im just learning c++ so bear with me here. :D
    Yes there is. It's very easy, in fact, but for anything other than a very small 3 dimensional array it becomes rather unmanageable. Here's an example:

    Code:
    #include <iostream>
    
    int main()
    {
        char array3d[2][2][2][10] = {{{"One","Two"},{"Three","Four"}},{{"Five","Six"},{"Seven","Eight"}}};
        
        for(int x=0; x < 2; ++x){
            for(int y=0; y < 2; ++y){
                for(int z=0; z < 2 ; ++z){
                    std::cout << array3d[x][y][z] << std::endl;
                }
            }
        }
    }
    Output is:

    One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight

IMN logo majestic logo threadwatch logo seochat tools logo