(C++) Container

January 25, 2018 · View on GitHub

 

 

 

 

 

(C++) Container

 

A container is a class type for containing zero, one or multiple instances of one or more data types.

 

Every container has its own advantages and disadvantages. For example a std::vector has random-access reading/writing, but new elements can only be added at the begin and end of the container. For a std::list, this is the other way around.

 

 

 

 

 

 

STL containers (incomplete list)

 

 

 

 

 

 

SGI extension containers (incomplete list)

 

  • bit_vector
  • hash_set
  • hash_map
  • hash_multiset
  • hash_multimap
  • hash
  • rope

 

 

 

 

 

Boost containers (incomplete list)

 

 

 

 

 

 

Container code snippets

 

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[8] If a class is a container, give it an initializer-list constructor'
  2. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[2] Use templates to express containers'
  3. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[1] An STL container defines a sequence'
  4. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[2] Use vector as your default container'
  5. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[3] Insertion operators, such as insert() and push_back() are often more efficient on a vector than on a list'
  6. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[7] STL containers are resource handles'
  7. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[13] Pass a container by reference and return a container by value'
  8. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[14] For a container, use the ()-syntax for sizes and the {}-initializer syntax for lists of elements'
  9. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[15] For simple traversal of a container, use a range-for-loop or a begin/end pair of iterators'
  10. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[20] Use push_back() or resize() on a container, rather than realloc() on an array'
  11. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 31.6. Advice. page 924: '[23] Do not assume that [] range checks'

Technical facts

 

 

 

 

 

 

./CppContainer/CppContainer.pri

 


INCLUDEPATH += \     ../../Classes/CppContainer SOURCES += \     ../../Classes/CppContainer/container.cpp HEADERS  += \     ../../Classes/CppContainer/container.h OTHER_FILES += \     ../../Classes/CppContainer/Licence.txt

 

 

 

 

 

./CppContainer/container.h

 


 

 

 

 

 

./CppContainer/container.cpp