Max array size question

This is a discussion on Max array size question within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by legit Really? All of the tutorials and books that i've read say that vectors are advanced and ...

  1. #16
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by legit View Post
    Really? All of the tutorials and books that i've read say that vectors are advanced and powerful tools :S
    Doesn't mean they're hard to use or implement. I found it to be a glorious day when I first learned of vectors.

    Although, I use both vectors and dynamic arrays interchangeably. Depends on what I'm doing/using it for.

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,638
    >> Doesn't mean they're hard to use or implement.

    Implementing a full-fledged vector is not as trivial as you may think. Give it a try if you doubt me.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #18
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Sebastiani View Post
    Implementing a full-fledged vector is not as trivial as you may think. Give it a try if you doubt me.
    Never said full-fledged vectors were trivial
    But something like...

    Code:
    //may the code gods have mercy if this is wrong :)
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<int> MyVector;
        int i;
        for(i = 0; i < 10; i++)
            MyVector.push_back(i);
        for(i = 0; i < 10; i++)
            cout<< MyVector[i] << endl;
    
        return 0;
    }
    ... is simple.

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    58
    I have a follow up question.

    Say, I have the 1G array of doubles. In my application I know for sure that all doubles will always be non-negative.

    Now, if I needed another array of the same length containing either 1 or 0, I would have to declare another array of integers. Could I save memory by using the sign-bit (leading bit?) of the double to store the bits? Or would this end in a mess?

    SK

  5. #20
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It would seem a bit messy.

    A smaller datatype for holding ones and zeros would be char. Even smaller should be std::bitset, but then the number of ones and zeros to store should be known at compile time.

    If that is unknown the most compact representation would be the std::vector<bool> specialization (but that is not recommended since it works differently from other types of vector).

    If you don't mind getting boost, you could also try boost::dynamic_bitset.
    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).

  6. #21
    Registered User
    Join Date
    May 2008
    Posts
    58
    Quote Originally Posted by anon View Post
    It would seem a bit messy.

    A smaller datatype for holding ones and zeros would be char. Even smaller should be std::bitset, but then the number of ones and zeros to store should be known at compile time.

    If that is unknown the most compact representation would be the std::vector<bool> specialization (but that is not recommended since it works differently from other types of vector).

    If you don't mind getting boost, you could also try boost::dynamic_bitset.
    Yes, good idea to store such an array as an integer. I am already using something like this. The code would be:

    Code:
    // BIT MANIPULATIONS
    #define BOOL(x) ( !(!(x)) )
    #define BIT_TEST( arg , pos ) BOOL( (arg)&(1L << (pos)) )
    #define BIT_FLIP( arg , pos ) ( (arg)^(1L << (pos)) )
    
    // suppose you want an array with 1's in the 3rd and 10th positions
    
    unsigned int n = 0;
    
    n = BIT_FLIP( n , 3 );
    n = BIT_FLIP( n , 10 );
    
    // read the digits using BIT_TEST, e.g.
    
    for ( i = 10; i--; ) { printf( "%u\n" , BIT_TEST( n , i ) ); }
    EDIT: maybe one has to reverse the order in BIT_TEST
    Last edited by serge; 06-16-2009 at 04:12 AM.

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And with bitset, the same code without any macros would be

    Code:
    #include <bitset>
    #include <iostream>
    
    int main()
    {
        std::bitset<32> n;
        n.flip(3);
        n.flip(10);
        for ( int i = 11; i--; ) {
            std::cout << n.test(i) << '\n';
        }
    }
    The difference is that you are not limited with the size of one unsigned (bitset uses an array of unsigneds for storage).
    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).

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  2. Replies: 2
    Last Post: 11-24-2005, 12:30 AM
  3. Replies: 42
    Last Post: 12-19-2004, 07:59 AM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 02:04 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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