Thread: Boost.Array vs std::vector

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Boost.Array vs std::vector

    Read the following:
    Boost.Array
    This library is a wrapper around ordinary C-style arrays, augmenting them with the functions and typedefs from the Standard Library containers. In effect, this makes it possible to treat ordinary arrays as Standard Library containers. This is useful because it adds safety without impeding efficiency and it enables uniform syntax for Standard Library containers and ordinary arrays. The latter means that it enables the use of ordinary arrays with most functions that require a container type to operate on. Array is typically used when performance issues mandate that ordinary arrays be used rather than std::vector.
    I'm quoting it from the book "Beyond the Standard Library: An Introduction to Boost"
    How does augmenting arrays with functions and typedefs ..make them a better performer than std::vector. What is the difference ?

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    make them a better performer than std::vector.
    Short answer, vector is more responsible so vector will always be slower...

    It seem that boost only is adding a lay of security when accessing arrays. Sounds like a vector want-to-be but they claim very little speed lost. Kind of makes since. But still with vector you get even more. You get Maximum security for your data and automatic resizing of your array at an expense of running a little slower. That makes even better since.

    As far as speed, It may be possible since it is only a small go-between when using boost on arrays. But I still rather have resizing and security than just a function that runs a few clocks faster. That's the point of vector and it can grow forever or until you run out of RAM.

    I like the boost regular expression library and I have a big job just for him, but that's about it so far because everywhere I go, I read where people say it's sooooo slow when it comes to handling large amount of data and with more and more layers to come it makes you wonder how many "layers" of bricks can you put inside a race car and than still claim it to be just as fast???

    C++ STL Tutorial

    CS201: Introducing C++ Vectors and Arrays

    For the future:
    binary-trees benchmark | Computer..Language..Benchmarks..Game
    Last edited by sharris; 04-17-2011 at 09:20 AM.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The difference is that std::vector is dynamic where Boost.Array is static.
    I would guess that the internal array of Boost.Array is allocated on the stack, where std::vector is allocated on the heap. But I am not sure for the first.

    So the comparison is not valid. It is Boost.Array vs standard array.

    What Boost.Array offers more are some operators/functions. For example size().

    But std::vector is not safe!! You can go out of bounds for example with no exceptions.
    If you want an array on the heap, then std::vector shouldn't lose performance compared to a standard array. At least it won't be something noticeable.

    EDIT: In C++0x there is std::array which has the same role as boost::array
    Last edited by C_ntua; 04-17-2011 at 09:45 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest using std::array or std::tr1::array instead of Boost.Array: they are more or less the same thing, but you might as well get used to std::array since it will be in the 2011 edition of the C++ standard.

    As for security: for accessing elements by index, both std::vector and std::array allow a mechanism to detect an out of bounds access (via the at member function), but this is not provided by using a normal array. Of course, you don't have to use this member function as operator[] is also available without such a check.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    std::tr1::array
    What is "tr1" ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    What is "tr1" ?
    Technical Report 1. Basically, a document that describes extensions to the standard library in preparation for additions to the standard library. It is currently available for several compilers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    //#I'm using gcc-4.5#
    Code:
    #include<array>
     /*...works fine..compiler recognizes it and shows some warnings about the support being experimental*/
    .
    .
    std::tr1::array<int> x;//does not work.. tells that tr1 is not declared..and when omitted..array is not declared..
    What is the correct way of using them in gcc?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You also need to specify the array size, e.g., std::array<int, 20>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ..it is working...
    also..
    How do I dynamically allocate the size ?..
    I can't think of a way other than putting a variable in the second argument....which as expected...fails to work..!
    Are the "tr1" documents public? ...if 'yes'...how 'technical' are they ?..

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    How do I dynamically allocate the size ?
    If you want to dynamically allocate the size at run time, use std::vector instead

    Quote Originally Posted by manasij7479
    Are the "tr1" documents public? ...if 'yes'...how 'technical' are they ?
    Yes. It is a typical standards document: TR1 Draft (PDF)

    That said, you might as well read the relevant section in the Final Draft of C++11.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ..thanks..though most of the TR1 document does not make much sense for me...YET...

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    laserlight wrote:::Of course, you don't have to use this member function as operator[] is also available without such a check.
    WoW! And I was just reading into this:
    c++ - ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster! - Stack Overflow

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    boost::array and std::array also adds iterators to arrays. While pointers work like iterators in some cases, they don't always. Hence this is a welcome addition.
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    While pointers work like iterators in some cases, they don't always.
    This sounds wrong. Pointers are random access iterators. The difference is a consistency in using begin and end member functions as opposed to say, manually performing pointer arithmetic to get a one past the end pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I agree that as concepts they are the same thing. Nevertheless, a true iterator also contains some mandatory typedefs describing its characteristics. And some function rely on those typedefs, which will break them when using raw pointers.
    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. Boost tuple and Vector problem
    By LuckyPierre in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2009, 01:26 AM
  2. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  3. Array from vector.
    By Hulag in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2006, 11:55 AM
  4. erasing elements from a boost::ptr_vector (boost n00b)
    By Marcos in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2006, 12:54 PM
  5. Array trying to be a vector
    By Chaplin27 in forum C++ Programming
    Replies: 18
    Last Post: 03-03-2005, 09:48 PM