pointer to 2d array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    pointer to 2d array

    put most simply, what is the c++ equivalent to this java code?

    int map[][];
    map = new int[16][16];
    map[5][5] = 3;
    System.out.prin tln(map[5][5]);

    thanks in advance.

  • Jonathan Mcdougall

    #2
    Re: pointer to 2d array

    Mark wrote:[color=blue]
    > put most simply, what is the c++ equivalent to this java code?
    >
    > int map[][];
    > map = new int[16][16];
    > map[5][5] = 3;
    > System.out.prin tln(map[5][5]);[/color]

    # include <iostream>

    int main()
    {
    int map[16][16] = {0};
    map[5][5] = 3;
    std::cout << map[5][5] << "\n";
    }


    Jonathan

    Comment

    • Axter

      #3
      Re: pointer to 2d array


      Mark wrote:[color=blue]
      > put most simply, what is the c++ equivalent to this java code?
      >
      > int map[][];
      > map = new int[16][16];
      > map[5][5] = 3;
      > System.out.prin tln(map[5][5]);
      >
      > thanks in advance.[/color]

      You can use vector to simulate this dynamically.
      #include <iostream>
      #include <vector>
      using namspace std;
      .....

      vector<vector<i nt> > MyMap(16, vector<int>(16) );
      MyMap[5][5] = 3;
      cout << MyMap[5][5] << endl;

      Comment

      • James Juno

        #4
        Re: pointer to 2d array

        Mark wrote:[color=blue]
        > put most simply, what is the c++ equivalent to this java code?
        >
        > int map[][];
        > map = new int[16][16];
        > map[5][5] = 3;
        > System.out.prin tln(map[5][5]);
        >
        > thanks in advance.
        >[/color]

        Personally, I would

        vector< vector<int> > map;
        map.resize( 16 );
        for ( int i = 0; i < 16; ++i )
        map[i].resize( 16 );
        map[5][5] = 3;
        cout << map[5][5] << endl;

        But that might not be "most simply" since I'm using STL.

        -JJ

        Comment

        • Mark

          #5
          Re: pointer to 2d array

          Jonathan Mcdougall, the idea was so that i could reassign map to
          another matrix of a different size. perhaps i should have specified
          that

          thanks Axter. i'll try that.

          Comment

          • James Juno

            #6
            Re: pointer to 2d array

            Axter wrote:[color=blue]
            > Mark wrote:[color=green]
            >> put most simply, what is the c++ equivalent to this java code?
            >>
            >> int map[][];
            >> map = new int[16][16];
            >> map[5][5] = 3;
            >> System.out.prin tln(map[5][5]);
            >>
            >> thanks in advance.[/color]
            >
            > You can use vector to simulate this dynamically.
            > #include <iostream>
            > #include <vector>
            > using namspace std;
            > ....
            >
            > vector<vector<i nt> > MyMap(16, vector<int>(16) );[/color]

            Nice way to initialize! Amazing how you can program for years and not
            notice something obvious like this. Thanks!

            -JJ

            Comment

            • Axter

              #7
              Re: pointer to 2d array


              James Juno wrote:[color=blue]
              > Mark wrote:[color=green]
              > > put most simply, what is the c++ equivalent to this java code?
              > >
              > > int map[][];
              > > map = new int[16][16];
              > > map[5][5] = 3;
              > > System.out.prin tln(map[5][5]);
              > >
              > > thanks in advance.
              > >[/color]
              >
              > Personally, I would
              >
              > vector< vector<int> > map;
              > map.resize( 16 );
              > for ( int i = 0; i < 16; ++i )
              > map[i].resize( 16 );
              > map[5][5] = 3;
              > cout << map[5][5] << endl;
              >
              > But that might not be "most simply" since I'm using STL.
              >[/color]

              You can resize after the vector has been initialize by using following
              method:
              map.resize(16, vector<int>(16) );

              Also, to the original questioner, I would advise avoiding naming
              variables that have the same name as objects in the STL library. map
              is one of the containers in the C++ library.
              Using the same name can be confusing to any one maintaining the code.
              Also check out methods in the following links:

              http://www.codeguru.com/forum/showthread.php?t=231046
              http://www.codeguru.com/forum/showth...hreadid=297838

              C-Style compatible method:



              Comment

              • Mark

                #8
                Re: pointer to 2d array

                i actually prefer your way James, at least for this scenario, since I
                don't know what size I'll be initializing it to anyway.

                Now quick question,

                void map_resize(int x, int y)
                {
                map.resize(y);
                for(int i=0; i<y; i++)
                map[i].resize(x);
                }

                have I got my x and y correct, or should they be switched? It's hard
                to visualize.

                I tried running

                map_resize(10,2 0);
                map[15][5] = 5;
                printf("%i", map[15][5]);

                which works, but 15 > 10 should be out of bounds, if I wrote my
                function correctly. however it is a container class and may be
                resizing my map...

                Comment

                • Jonathan Mcdougall

                  #9
                  Re: pointer to 2d array

                  Mark wrote:[color=blue]
                  > Jonathan Mcdougall, the idea was so that i could reassign map to
                  > another matrix of a different size. perhaps i should have specified
                  > that[/color]

                  Please 1) answer to the message you are answering to.. and 2) quote it.

                  You can assign arrays by hand:

                  void f(int source[10], int target[10])
                  {
                  for (int i=0; i<10; ++i)
                  target[i] = source[i];
                  }

                  But arrays are not value-types, meaning they cannot be assigned
                  directly:

                  target = source; // invalid;

                  You should use standard containers in this case, such as std::vector.
                  You've already been proposed good solutions.

                  You could also define a class yourself with the corresponding
                  operators. Search for matrix classes on google for some free
                  implementations .

                  Jonathan

                  Comment

                  • James Juno

                    #10
                    Re: pointer to 2d array

                    Mark wrote:[color=blue]
                    > i actually prefer your way James, at least for this scenario, since I
                    > don't know what size I'll be initializing it to anyway.
                    >
                    > Now quick question,
                    >
                    > void map_resize(int x, int y)
                    > {
                    > map.resize(y);
                    > for(int i=0; i<y; i++)
                    > map[i].resize(x);
                    > }
                    >
                    > have I got my x and y correct, or should they be switched? It's hard
                    > to visualize.
                    >
                    > I tried running
                    >
                    > map_resize(10,2 0);
                    > map[15][5] = 5;
                    > printf("%i", map[15][5]);
                    >
                    > which works, but 15 > 10 should be out of bounds, if I wrote my
                    > function correctly. however it is a container class and may be
                    > resizing my map...
                    >[/color]

                    I like to visualize it this way: In your function, the first resize
                    creates y columns of empty vectors. The loop then goes through the
                    columns and sets the length of the vectors to x rows. So, map[15][5] is
                    the 15th column (zero-based), 5th row, which is fine by the way you've
                    written your function. If you try to address a non-existent element
                    (for instance, map[5][15]), you'll have a problem. It won't resize
                    itself automatically.

                    -JJ

                    Comment

                    • BobR

                      #11
                      Re: pointer to 2d array


                      James Juno wrote in message ...[color=blue]
                      >Mark wrote:[color=green]
                      >> put most simply, what is the c++ equivalent to this java code?
                      >>
                      >> int map[][];
                      >> map = new int[16][16];
                      >> map[5][5] = 3;
                      >> System.out.prin tln(map[5][5]);
                      >>
                      >> thanks in advance.
                      >>[/color]
                      >
                      >Personally, I would
                      >
                      > vector< vector<int> > map;
                      > map.resize( 16 );
                      > for ( int i = 0; i < 16; ++i )
                      > map[i].resize( 16 );
                      > map[5][5] = 3;
                      > cout << map[5][5] << endl;
                      >
                      >But that might not be "most simply" since I'm using STL.
                      >-JJ[/color]

                      Try this :

                      // int main(){
                      std::vector<std ::vector<int> > Vvmap(16, 16);
                      std::cout << Vvmap.size()<<s td::endl;
                      std::cout << Vvmap.at(0).siz e()<<std::endl;
                      Vvmap.at(5).at( 5) = 3;
                      Vvmap.at(5).pus h_back(5);
                      std::cout <<Vvmap.at(5).a t(5)<<std::endl ;
                      for( size_t i(0); i < Vvmap.size(); ++i ){
                      std::cout<<Vvma p.at( i ).size()<<std:: endl;;
                      }
                      // return 0;
                      // } // main() end
                      --
                      Bob R
                      POVrookie


                      Comment

                      • BobR

                        #12
                        Re: pointer to 2d array


                        James Juno wrote in message ...[color=blue]
                        >[snip]
                        >I like to visualize it this way: In your function, the first resize
                        >creates y columns of empty vectors. The loop then goes through the
                        >columns and sets the length of the vectors to x rows.
                        >** So, map[15][5] is the 15th column (zero-based), 5th row, **[/color]

                        No. It's the 16th col, 6th row. <G>
                        [color=blue]
                        >which is fine by the way you've
                        >written your function. If you try to address a non-existent element
                        >(for instance, map[5][15]), you'll have a problem. It won't resize
                        >itself automatically.[/color]

                        So, use the at() to access. That way you'll get an out_of_range exception
                        (which you can 'catch'):
                        map.at(5).at(15 ) = 7;

                        / -----------------
                        size_t rows(7);
                        size_t cols(7);
                        std::vector<std ::vector<int> > Vvmap( rows, cols);
                        try{
                        Vvmap.at(6).at( 26) = 32;
                        }
                        catch(std::out_ of_range &Oor){
                        std::cout<<"cau ght "<<Oor.what()<< std::endl;
                        }
                        catch(...){
                        std::cout<<"cau ght something"<<std ::endl;
                        }
                        // output: caught vector [] access out of range
                        / -----------------

                        Only use the [] operator if you are *positive* the index is in range(...and
                        actually need the *tiny* speed increase). Like in this for loop:
                        for(size_t i(0); i < map.size(); ++i){
                        map[ i ].push_back( i );
                        }

                        [corrections always welcome ]
                        --
                        Bob R
                        POVrookie


                        Comment

                        Working...