vector as a Member Data of a Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stdq
    New Member
    • Apr 2013
    • 94

    #1

    vector as a Member Data of a Class

    Hi, everyone! If I want a class with a vector data member, can I specify it as follows?

    Code:
    std::vector< bool > integers( 101 )
    I'm having some problems when compiling my code. Thanks in advance!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are probably doing everything correctly.

    Here's some C++ dirty laundry: A vector<bool> is not a vector!

    Bits in C are usually handled using bitfields. So the vector<bool> uses bitfields, is not a template, has methods not in any real vector, is not an array.

    Other than that, it works great (so I hear).

    If you insist on bool, then bury the bool in a class and have a vector of that class.

    Comment

    • stdq
      New Member
      • Apr 2013
      • 94

      #3
      Actually, I had to initialize the vector in respect to its size using a member intializer. But thanks for the info!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You are wandering into a swamp.

        You will find that vector<bool> iterators can't be dereferenced nor an they be incremented.

        vector<bool> is not an array so operator[] won't compile.

        vector<bool> was a design error in C++ 1998 and is only kept for backwards compatibility. No new code is supposed to be written using this non-standard container.

        You are supposed to use bitset<> for managing at the bit level.

        Comment

        Working...