doc_custom_small_vector.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2013-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //[doc_custom_small_vector
  11. #include <boost/container/small_vector.hpp>
  12. #include <boost/static_assert.hpp>
  13. //Make sure assertions are active
  14. #ifdef NDEBUG
  15. #undef NDEBUG
  16. #endif
  17. #include <cassert>
  18. int main ()
  19. {
  20. using namespace boost::container;
  21. //This option specifies the desired alignment for the internal value_type
  22. typedef small_vector_options< inplace_alignment<16u> >::type alignment_16_option_t;
  23. //Check 16 byte alignment option
  24. small_vector<int, 10, void, alignment_16_option_t > sv;
  25. assert(((std::size_t)sv.data() % 16u) == 0);
  26. //This option specifies that a vector will increase its capacity 50%
  27. //each time the previous capacity was exhausted.
  28. typedef small_vector_options< growth_factor<growth_factor_50> >::type growth_50_option_t;
  29. //Fill the vector until full capacity is reached
  30. small_vector<int, 10, void, growth_50_option_t > growth_50_vector(10, 0);
  31. const std::size_t old_cap = growth_50_vector.capacity();
  32. growth_50_vector.resize(old_cap);
  33. //Now insert an additional item and check the new buffer is 50% bigger
  34. growth_50_vector.push_back(1);
  35. assert(growth_50_vector.capacity() == old_cap*3/2);
  36. return 0;
  37. }
  38. //]