2D Vectors

This is a discussion on 2D Vectors within the C++ Programming forums, part of the General Programming Boards category; I have a usual vector that I have declared like this: size_t size = 100000; std::vector<int> Test(size); Now I want ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    2D Vectors

    I have a usual vector that I have declared like this:

    size_t size = 100000;
    std::vector<int> Test(size);


    Now I want to have a 2D vector instead of this.
    Like an ordinary array it would look like for example:
    Test[5][5];


    But now if I want to create a 2D for the vector with the same size(100000).
    How would the syntax look like for this ?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Code:
    std::vector< std::vector<int> > v;
    Also, you don't need to specify a size for vector. It's dynamic, so it will resize itself if needed.
    But also need to do:
    Code:
    v.push_back( std::vector() );
    To create the first dimension so you can access the second dimension:
    Code:
    v[0].push_back(0);
    int n = v[0][0]
    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.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A 2D vector would be a vector of vectors (of int).

    Code:
    std::vector<std::vector<int> > vec2d(100, std::vector<int>(1000));
    Typedefs can reduce some typing:
    Code:
    typedef std::vector<int> IntVector
    typedef std::vector<IntVector> IntVector2D
    IntVector2D vec2d(100, IntVector(1000));
    Now vec2d holds 100 vectors each holding 1000 integers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I think I understand. To be sure, if I take the example for:

    std::vector<std::vector<int> > vec2d(100, std::vector<int>(1000));

    Will I access the 2D vector like this:

    vec2d[100][1000];

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Yes, but again, with a vector, you do no have to specify a size! Unless you use it as a buffer, you never need to specify size.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    more like vec2d[99][999];

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Quote Originally Posted by robwhit View Post
    vec2d[99][999];
    How do you figure that?
    Sure, the highest index will be 99 and 999, but the declaration or definition should be vec2d[100][1000].
    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.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes I will change my code to not specify size.
    If I dont specify size, I am not really sure how it should be written.

    Should it be written like this if you dont specify size:
    std::vector<std::vector<int> > vec2d((), std::vector<int>());

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    I already showed you:
    2D Vectors
    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.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes ofcourse, now I understand and ofcourse when you access the 2D-array with size 100 and 1000 it should be written like this instead:
    vec2d[99][999];
    As the first position starts at 0.
    Last edited by Coding; 01-12-2008 at 06:13 PM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    This:
    Code:
    std::vector<std::vector<int> > vec2d(100, std::vector<int>(1000));
    is equivalent to this:
    Code:
    int arr2d[100][1000];
    You should specify the size if you know what it is before hand. If you don't know, then you can use push_back to add elements.

    To access existing elements in a 2-D array or vector, you use:
    Code:
    std::cout << vec2d[0][0];
    std::cout << vec2d[99][999];
    std::cout << arr2d[0][0];
    std::cout << arr2d[99][999];
    But remember that you can only access the elements in the vector(s) if they exist. So if you don't specify a size you can't access those elements until you've push_back'd enough items.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Quote Originally Posted by Daved View Post
    You should specify the size if you know what it is before hand. If you don't know, then you can use push_back to add elements.
    Kind of depends on the performance of your application. In typical programs, it won't matter, since it's lightning fast anyway,
    You would get what some call a memory hog
    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.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It also depends on what it is meant for. The dimensions can have a known fixed size and you may want to be able to start accessing things right away.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >>vec2d[99][999];
    >How do you figure that?
    the op asked how to access it, not how to declare/define it.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    I see, so you were basing it on access and not declaration/definition.
    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

Similar Threads

  1. Velocity in rotation using 2D vectors???
    By blackCat in forum Game Programming
    Replies: 1
    Last Post: 04-10-2009, 09:16 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 06:31 PM
  4. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  5. 2d [STL] Vectors
    By cozman in forum C++ Programming
    Replies: 2
    Last Post: 05-29-2002, 11:50 AM

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