Boost.Array vs std::vector

This is a discussion on Boost.Array vs std::vector within the C++ Programming forums, part of the General Programming Boards category; Read the following: Boost.Array This library is a wrapper around ordinary C-style arrays, augmenting them with the functions and typedefs ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    std::tr1::array
    What is "tr1" ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    //#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?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    You also need to specify the array size, e.g., std::array<int, 20>
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ..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 ?..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ..thanks..though most of the TR1 document does not make much sense for me...YET...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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.

Page 1 of 2 12 LastLast
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, 08:48 PM

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