2d array of booleans

This is a discussion on 2d array of booleans within the C++ Programming forums, part of the General Programming Boards category; Hi! Does any one know the best war to create a 2dimensional array of booleans. I know how to create ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    30

    2d array of booleans

    Hi!
    Does any one know the best war to create a 2dimensional array of booleans.
    I know how to create a 1D array using bitset but cant go any further.
    Cheers!
    PS:can u tell me a little about the bool datatype or any links where i could read up on it.
    Last edited by eklavya8; 06-27-2008 at 07:50 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    Code:
    #include <boost/array.hpp>
    boost::array<boost::array<bool, 10>, 10> my2darray;
    Or if you prefer the old, unsafe, C way:
    Code:
    bool my2darray[10][10];
    (PS: For the first, you need boost. You can download it from their website. It's free.)

    The bool datatype is simply a type which can be true or false. Nothing else.
    Last edited by Elysia; 06-27-2008 at 08:10 AM.
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    An array of bool is simply: bool oneD[10]; or bool twoD[10][10];
    That's fine if you know the exact size of arrays you need.
    If you need an array that can grow, you can use the boost::array as Elysia suggested. Just don't use std::vector<bool> because it's not exactly a vector of bools, and it has some limitations such as not being able to create pointers to individual elements of the vector (this only applies to vector<bool>, not vector<anything else>)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    boost::array cannot grow; it's static.
    You'll want to use, say, std::vector<uint8_t> or std::vector<char> instead if you need a dynamic array.
    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.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    boost::array cannot grow; it's static.
    You'll want to use, say, std::vector<uint8_t> or std::vector<char> instead if you need a dynamic array.
    Oops, you're right, boost::array doesn't grow.
    You could also use: std::deque<bool> or std::basic_string<bool> which both have [] operators...

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    or you could use bool** and create a growable array of bool*, that point to arrays of bools that can grow themselves. You can then access them the same as pBool[5][6]; and you avoid the whole stl nongrowability adn nondirectly addressable problem.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    However, this means the use of new and dynamic memory which should be avoided.
    There are tools for the job.
    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
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Elysia View Post
    However, this means the use of new and dynamic memory which should be avoided.
    There are tools for the job.
    Those tools dont solve the problem effectively. It doesnt require new, malloc() and realloc() work just fine, and either method (stl or bool**) will require dynamic memory. Sorry to break the news to you elysia, but STL uses dynamic memory.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    But you are not in charge of the memory. It is handled by STL, so you don't have to.
    std::vector<uint8_t> works well for a dynamic array of bool.
    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
    Jan 2008
    Posts
    30

    Smile thanks all!

    thank you all some really nice stuff learnt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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