### Boost Flat Set and Flat Map Example Source: https://github.com/boostorg/container/blob/develop/_autodocs/flat_containers.md Demonstrates the usage of boost::container::flat_set for unique sorted integers and boost::container::flat_map for key-value pairs. Includes examples of insertion, iteration, lookup, and deletion. ```cpp #include #include int main() { using namespace boost::container; // Flat set for unique integers flat_set s = {3, 1, 2, 5, 4}; // Stored as sorted: [1, 2, 3, 4, 5] // Fast iteration and lookup for (int x : s) { std::cout << x << " "; } // Efficient for read-heavy operations auto it = s.lower_bound(3); // O(log n) // Flat map for key-value pairs flat_map prices; prices["apple"] = 10; prices["banana"] = 5; prices["cherry"] = 8; // Direct access std::cout << "Apple price: " << prices["apple"] << std::endl; // Erase is O(n) but unordered erase is faster auto pos = prices.find("banana"); if (pos != prices.end()) { prices.erase(pos); } return 0; } ``` -------------------------------- ### Custom Deque Options Example Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Illustrates creating a deque with custom block and segment sizes. This example uses a larger block size and segment size compared to the defaults. ```cpp typedef boost::container::deque_options<1024, 64> LargeBlockDeque; boost::container::deque d; ``` -------------------------------- ### Example usage of default_init_t Source: https://github.com/boostorg/container/blob/develop/_autodocs/types_and_utilities.md Demonstrates creating a vector with uninitialized integers using default_init. Contrast this with default zero-initialized integers. ```cpp vector v(10, default_init); // 10 uninitialized ints vector v(10); // 10 zero-initialized ints ``` -------------------------------- ### Boost Container Deque Example Usage Source: https://github.com/boostorg/container/blob/develop/_autodocs/deque.md Demonstrates common deque operations including construction, element addition/access at ends and middle, iteration, removal, and resizing. Requires including the boost/container/deque.hpp header. ```cpp #include #include int main() { using namespace boost::container; // Default construction deque d; // Add elements at both ends d.push_back(5); d.push_back(6); d.push_front(1); d.push_front(0); // Element access std::cout << "Front: " << d.front() << std::endl; // Output: 0 std::cout << "Back: " << d.back() << std::endl; // Output: 6 std::cout << "Element [2]: " << d[2] << std::endl; // Output: 5 // Insert in the middle d.insert(d.begin() + 2, 99); // Iteration for (int x : d) { std::cout << x << " "; } std::cout << std::endl; // Remove from both ends d.pop_front(); d.pop_back(); // Resize d.resize(3, 0); std::cout << "Final size: " << d.size() << std::endl; return 0; } ``` -------------------------------- ### C++ Allocator Initialization Example Source: https://github.com/boostorg/container/blob/develop/doc/allocplus/index.html Demonstrates the initialization of different allocator types for use with STL containers. Includes standard allocator, DLmalloc-based allocator, and allocators with expansion capabilities. ```cpp 1. **StdAllocator**: equivalent to `std::allocator`, the standard allocator that comes with the C++ compiler 2. **AllocatorPlusV1**: equivalent to `ba::allocator`, a STL conformant allocator around `dlmalloc` and `dlfree`. 3. **AllocatorPlusV2Mask**: equivalent to `ba::allocator`, an allocator with capabilities to specify a limit size the preferred size and real size of a buffer, but with expansion capabilities disabled. 4. **AllocatorPlusV2**: equivalent to `ba::allocator`, a fully equipped improved allocator, that avoids forward expansion (forward expansion has priority over backwards expansion, so we need to disable it to only measure backwards expansion). ``` -------------------------------- ### Type Introspection Example Source: https://github.com/boostorg/container/blob/develop/_autodocs/types_and_utilities.md Demonstrates how to use `boost::container::allocator_traits` to get allocator information and `boost::container::uses_allocator` to check if a type utilizes an allocator. This is useful for understanding and interacting with custom allocators. ```cpp #include #include int main() { using namespace boost::container; typedef vector Vec; typedef allocator_traits Traits; // Get allocator information auto max = Traits::max_size(Vec().get_allocator()); // Check if type uses allocator if (uses_allocator>::value) { std::cout << "Uses allocator" << std::endl; } return 0; } ``` -------------------------------- ### Custom Vector Options Example Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Demonstrates how to define and use custom options for a Boost.Container vector, specifying a growth factor and a 32-bit size type. ```cpp #include #include typedef boost::container::vector_options CustomOptions; boost::container::vector v; ``` -------------------------------- ### Boost.Container static_vector Example Source: https://github.com/boostorg/container/blob/develop/_autodocs/special_containers.md Demonstrates the basic usage of boost::container::static_vector, including initialization, adding elements, and accessing size and capacity. This container is suitable for scenarios where fixed capacity and stack allocation are desired. ```cpp #include #include int main() { using namespace boost::container; // Fixed capacity of 10 elements static_vector v; v.push_back(1); v.push_back(2); v.push_back(3); // This would throw if v.size() == 10 before the push // v.push_back(4); // OK if capacity allows // All vector operations available std::cout << v.size() << std::endl; // 3 std::cout << v.capacity() << std::endl; // 10 return 0; } ``` -------------------------------- ### Basic Boost Container Vector Example Source: https://github.com/boostorg/container/blob/develop/_autodocs/README.md Demonstrates the creation and basic usage of boost::container::vector, including adding elements and iterating through them. This is suitable for general-purpose dynamic arrays. ```cpp #include #include int main() { boost::container::vector v; v.push_back(1); v.push_back(2); v.push_back(3); for (int x : v) { std::cout << x << " "; // Output: 1 2 3 } std::cout << std::endl; return 0; } ``` -------------------------------- ### Example usage of node_handle Source: https://github.com/boostorg/container/blob/develop/_autodocs/types_and_utilities.md Shows how to extract a node from a map into a node_handle and check if the handle is valid or access its allocator. ```cpp auto nh = map.extract(it); // Extract node if (nh) { std::cout << nh.get_allocator() << std::endl; } ``` -------------------------------- ### Boost Container List with Custom Allocator Source: https://github.com/boostorg/container/blob/develop/_autodocs/README.md Shows how to use boost::container::list with a custom allocator, specifically boost::container::node_allocator. This example demonstrates advanced memory management for lists. ```cpp #include #include #include int main() { typedef boost::container::list> List; List l; for (int i = 0; i < 10; ++i) { l.push_back(i); } for (int x : l) { std::cout << x << " "; } std::cout << std::endl; return 0; } ``` -------------------------------- ### flat_set Size and Capacity Management Source: https://github.com/boostorg/container/blob/develop/_autodocs/flat_containers.md Methods for querying the size, checking if empty, getting max size, capacity, and managing capacity with reserve and shrink_to_fit. ```cpp size_type size() const; bool empty() const; size_type max_size() const; size_type capacity() const; void reserve(size_type n); void shrink_to_fit(); ``` -------------------------------- ### List and Slist Example Usage Source: https://github.com/boostorg/container/blob/develop/_autodocs/list_and_slist.md Demonstrates the basic usage of both boost::container::list (doubly-linked) and boost::container::slist (singly-linked), including insertion, deletion, merging, and sorting operations. This snippet highlights the differences in their interfaces and typical use cases. ```cpp #include #include int main() { using namespace boost::container; // Doubly-linked list list l; l.push_back(1); l.push_back(2); l.push_front(0); // Efficient removal from anywhere for (auto it = l.begin(); it != l.end(); ++it) { if (*it == 1) { l.erase(it); break; } } // Merge and sort list l2; l2.push_back(3); l2.push_back(4); l.merge(l2); l.sort(); // Singly-linked list for forward-only use slist sl; sl.push_front(1); sl.push_front(0); // Only forward iteration for (auto x : sl) { std::cout << x << " "; } return 0; } ``` -------------------------------- ### Configure Vector for High Performance Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Example of configuring a Boost.Container vector with an aggressive 2x growth factor for vectorized access. It also demonstrates pre-allocating capacity for a known large size. ```cpp #include #include // 2x growth factor for vectorized access typedef boost::container::vector_options< boost::container::growth_factor_2x, // Aggressive growth std::size_t // Full size_t > HighPerformanceOptions; boost::container::vector v; v.reserve(1000000); // Pre-allocate for known size ``` -------------------------------- ### Boost Container Vector Example Usage Source: https://github.com/boostorg/container/blob/develop/_autodocs/vector.md Demonstrates common operations with Boost Container vector, including default and range construction, element manipulation (push_back, front, back, insert, erase, clear), iterator-based traversal, and capacity management (reserve). ```cpp #include #include int main() { using namespace boost::container; // Default construction vector v; // Fill from constructor vector v2(5, 10); // 5 copies of 10 // Range construction int arr[] = {1, 2, 3, 4, 5}; vector v3(arr, arr + 5); // Push and pop v.push_back(1); v.push_back(2); v.push_back(3); // Element access std::cout << "First element: " << v.front() << std::endl; std::cout << "Last element: " << v.back() << std::endl; // Iterator-based loop for (auto it = v.begin(); it != v.end(); ++it) { std::cout << *it << " "; } // Insert at position v.insert(v.begin() + 1, 99); // Erase elements v.erase(v.begin()); // Clear all v.clear(); // Capacity management v.reserve(100); std::cout << "Capacity: " << v.capacity() << std::endl; return 0; } ``` -------------------------------- ### Common Container Size Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Includes methods to check if a container is empty, get its current size, and determine its maximum possible size. ```cpp // Size bool empty() const; size_type size() const; size_type max_size() const; ``` -------------------------------- ### Boost Container Map Example Source: https://github.com/boostorg/container/blob/develop/_autodocs/README.md Illustrates the usage of boost::container::map for key-value storage. It shows how to insert elements and access them by key, as well as iterate through the map's contents. Useful for associative array needs. ```cpp #include #include #include int main() { boost::container::map m; m["apple"] = 5; m["banana"] = 3; std::cout << m["apple"] << std::endl; // 5 for (const auto& p : m) { std::cout << p.first << ": " << p.second << std::endl; } return 0; } ``` -------------------------------- ### Boost Small Vector with Stack Optimization Source: https://github.com/boostorg/container/blob/develop/_autodocs/README.md Demonstrates boost::container::small_vector, which optimizes for small sizes by storing elements on the stack. This example shows how to initialize a small_vector with a specified stack capacity and add elements without heap allocation. ```cpp #include #include int main() { // First 10 elements stored on stack boost::container::small_vector v; for (int i = 0; i < 5; ++i) { v.push_back(i); // No heap allocation needed } for (int x : v) { std::cout << x << " "; } std::cout << std::endl; return 0; } ``` -------------------------------- ### Iterators: begin(), end(), cbegin(), cend() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Provides access to iterators for traversing the string container. `begin()` and `end()` return iterators to the start and end of the string, respectively. `cbegin()` and `cend()` provide const iterators. ```APIDOC ## Iterators: begin(), end(), cbegin(), cend() ### Description Provides access to iterators for traversing the string container. `begin()` and `end()` return iterators to the start and end of the string, respectively. `cbegin()` and `cend()` provide const iterators. ### Method Signatures ```cpp iterator begin(); const_iterator begin() const; const_iterator cbegin() const; iterator end(); const_iterator end() const; const_iterator cend() const; ``` ### Complexity Constant ### Exception Safety No throw ``` -------------------------------- ### Build Boost.Container Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Shows the command to build Boost.Container if the full Boost build is required. Header-only components do not need a separate build. ```bash # Header-only: no build needed # Just include headers in your project # If full boost build required: ./b2 --with-container link=shared|static runtime-link=shared|static ``` -------------------------------- ### substr() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Extracts a substring from the string. It can take a starting position and an optional number of characters. If the number of characters is omitted, it extracts to the end of the string. Throws std::out_of_range if the starting position is invalid. ```APIDOC ## substr() ### Description Extracts a substring from the string. ### Method Multiple overloads available for extracting substrings. ### Parameters - `pos` (size_type) - Optional - Starting position of the substring. Defaults to 0. - `n` (size_type) - Optional - Number of characters to include in the substring. If omitted, extracts to the end of the string. ### Returns - basic_string: A new string containing the extracted substring. ### Throws - `std::out_of_range`: If `pos` is greater than the string's size. ``` -------------------------------- ### Build Boost.Container with Boost Build Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Use this command to build the Boost.Container library using the Boost Build system. Ensure you are in the Boost root directory. ```bash cd boost ./bootstrap.sh ./b2 --with-container stage ``` -------------------------------- ### Substring Constructor for basic_string Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Creates a string from a portion of another string, specified by a starting position and an optional number of characters. An allocator can also be provided. Throws std::out_of_range if the starting position is invalid. ```cpp basic_string(const basic_string& x, size_type pos); basic_string(const basic_string& x, size_type pos, const allocator_type& a); basic_string(const basic_string& x, size_type pos, size_type n); basic_string(const basic_string& x, size_type pos, size_type n, const allocator_type& a); ``` -------------------------------- ### Get Allocator Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Retrieves the allocator instance used by the container. ```cpp allocator_type get_allocator() const; ``` -------------------------------- ### Common Container Construction Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Demonstrates various ways to construct container objects, including default, copy, move, and initializer list constructors. ```cpp // Construction Container(); Container(const allocator_type& a); Container(size_type n); Container(size_type n, const T& value); template Container(InputIterator first, InputIterator last); Container(const Container& x); Container(Container&& x); Container(std::initializer_list il); ``` -------------------------------- ### Get Allocator Source: https://github.com/boostorg/container/blob/develop/_autodocs/deque.md Returns the allocator used by the deque. This operation has constant complexity and is exception-safe. ```cpp allocator_type get_allocator() const; const allocator_type& get_allocator() const; ``` -------------------------------- ### Using Boost Container Set and Map Source: https://github.com/boostorg/container/blob/develop/_autodocs/associative_containers.md Demonstrates the basic usage of Boost.Container's set for unique elements and map for key-value associations. Includes insertion, lookup, and iteration. ```cpp #include #include int main() { using namespace boost::container; // Set of unique integers set s; s.insert(3); s.insert(1); s.insert(2); // Lookup if (s.find(1) != s.end()) { std::cout << "Found 1" << std::endl; } // Map for key-value associations map m; m.insert({"apple", 5}); m.insert({"banana", 3}); // Access via key std::cout << "Apples: " << m["apple"] << std::endl; // Iterate in sorted order for (const auto& p : m) { std::cout << p.first << ": " << p.second << std::endl; } // Range operations auto range = m.equal_range("apple"); // range.first points to first "apple" // range.second points to first element > "apple" return 0; } ``` -------------------------------- ### find_last_not_of() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the last character in a string that does not match any character from a given set. The search starts from a specified position. ```APIDOC ## find_last_not_of() ### Description Finds the last character in a string that does not match any character from a given set. The search starts from a specified position. ### Method const ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response - **return value** (size_type) - Position of last non-match, or `npos` if not found #### Response Example None ``` -------------------------------- ### Using boost::container Namespace Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Demonstrates how to use the boost::container namespace to access its types and functions, such as creating a vector. ```cpp using namespace boost::container; vector v; ``` -------------------------------- ### find_first_not_of() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the first character in a string that does not match any character from a given set. The search starts from a specified position. ```APIDOC ## find_first_not_of() ### Description Finds the first character in a string that does not match any character from a given set. The search starts from a specified position. ### Method const ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response - **return value** (size_type) - Position of first non-match, or `npos` if not found #### Response Example None ``` -------------------------------- ### find_last_of() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the last character in a string that matches any character from a given set. The search starts from a specified position. ```APIDOC ## find_last_of() ### Description Finds the last character in a string that matches any character from a given set. The search starts from a specified position. ### Method const ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response - **return value** (size_type) - Position of last match, or `npos` if not found #### Response Example None ``` -------------------------------- ### find_first_of() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the first character in a string that matches any character from a given set. The search starts from a specified position. ```APIDOC ## find_first_of() ### Description Finds the first character in a string that matches any character from a given set. The search starts from a specified position. ### Method const ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response - **return value** (size_type) - Position of first match, or `npos` if not found #### Response Example None ``` -------------------------------- ### Substring Extraction Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Extracts a portion of the string as a new `basic_string`. Can specify the starting position and length of the substring. ```cpp basic_string substr(size_type pos = 0) const; basic_string substr(size_type pos, size_type n) const; ``` -------------------------------- ### Boost Container Devector Basic Usage Source: https://github.com/boostorg/container/blob/develop/_autodocs/special_containers.md Demonstrates the basic usage of boost::container::devector, including efficient insertion and deletion at both ends, and random access. Include the header. ```cpp #include #include int main() { using namespace boost::container; devector dv; // Efficient at both ends dv.push_back(3); dv.push_front(1); dv.push_back(4); dv.push_front(0); // Random access like vector std::cout << dv[2] << std::endl; // 3 // Efficient removal from both ends dv.pop_front(); dv.pop_back(); return 0; } ``` -------------------------------- ### Get Block Size Source: https://github.com/boostorg/container/blob/develop/_autodocs/deque.md Returns the internal block size used by the deque. This is a static function with constant complexity. ```cpp static size_type get_block_size(); ``` -------------------------------- ### Constructors for boost::container::set Source: https://github.com/boostorg/container/blob/develop/_autodocs/associative_containers.md Demonstrates various ways to construct a boost::container::set, including default construction, construction with a custom comparator, with an allocator, from iterator ranges, copy/move construction, and from initializer lists. ```cpp set(); explicit set(const Compare& comp); explicit set(const Compare& comp, const allocator_type& a); explicit set(const allocator_type& a); template set(InputIterator first, InputIterator last); template set(InputIterator first, InputIterator last, const Compare& comp); template set(InputIterator first, InputIterator last, const Compare& comp, const allocator_type& a); set(const set& x); set(set&& x); set(std::initializer_list il); set(std::initializer_list il, const Compare& comp); set(std::initializer_list il, const Compare& comp, const allocator_type& a); ``` -------------------------------- ### Get String Size or Length Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Returns the number of characters currently stored in the string. Both `size()` and `length()` provide the same information. ```cpp size_type size() const; size_type length() const; ``` -------------------------------- ### Get Segment Size Source: https://github.com/boostorg/container/blob/develop/_autodocs/deque.md Returns the segment size for the deque's internal map. This is a static function with constant complexity. ```cpp static size_type get_segment_size(); ``` -------------------------------- ### Algorithm Compatibility with Containers Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Demonstrates how standard C++ algorithms can be used with Boost.Container's sequence containers by utilizing their begin() and end() iterators. ```cpp std::sort(c.begin(), c.end()); std::find(c.begin(), c.end(), value); std::count_if(c.begin(), c.end(), predicate); std::for_each(c.begin(), c.end(), function); std::transform(c.begin(), c.end(), dest.begin(), function); // ... and all other standard algorithms ``` -------------------------------- ### Get Current Capacity Source: https://github.com/boostorg/container/blob/develop/_autodocs/vector.md Returns the number of elements that can be stored without needing to reallocate memory. This operation has constant complexity. ```cpp size_type capacity() const; ``` -------------------------------- ### list Constructors Source: https://github.com/boostorg/container/blob/develop/_autodocs/list_and_slist.md Demonstrates various ways to construct a boost::container::list, including default, size-based, range-based, copy, move, and initializer list constructors. ```cpp list(); explicit list(const allocator_type& a); explicit list(size_type n); list(size_type n, const T& value); list(size_type n, const T& value, const allocator_type& a); template list(InputIterator first, InputIterator last); list(const list& x); list(list&& x); list(std::initializer_list il); ``` -------------------------------- ### Prepare Ranges for Erasure Source: https://github.com/boostorg/container/blob/develop/doc/allocplus/index.html Preprocesses iterator ranges to be erased from a list. This setup is used to measure burst deallocation performance. ```cpp //Now preprocess ranges to erase std::vector ::iterator> ranges_to_erase; ranges_to_erase.push_back(l.begin()); for(unsigned int r = 0; r != num_iterations; ++r){ typename bi::list::iterator next_pos(ranges_to_erase[r]); std::size_t n = num_elements; while(n--){ ++next_pos; } ranges_to_erase.push_back(next_pos); } ``` -------------------------------- ### Boost.Container Utility Headers Source: https://github.com/boostorg/container/blob/develop/_autodocs/README.md Outlines the utility header files in Boost.Container, providing forward declarations, container options, and initialization tags. ```text boost/container/ ├── container_fwd.hpp # Forward declarations ├── options.hpp # Container options ├── node_handle.hpp # node_handle<> ├── allocator_traits.hpp # Type introspection └── default_init.hpp # Initialization tags ``` -------------------------------- ### Map Equal Range Operation Source: https://github.com/boostorg/container/blob/develop/_autodocs/associative_containers.md Provides a method to get a range of all elements with a specific key using `equal_range`, returning a pair of iterators. ```cpp std::pair equal_range(const key_type& x); std::pair equal_range(const key_type& x) const; ``` -------------------------------- ### Compile with Boost.Container Header-Only Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Include the Boost directory in your compiler's include path for header-only usage. This is the recommended approach for most use cases. ```bash g++ -I/path/to/boost myapp.cpp ``` -------------------------------- ### Map Insert Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/associative_containers.md Demonstrates various ways to insert elements into a map, including single element insertion, insertion with a hint, insertion from a range of iterators, and using initializer lists. ```cpp std::pair insert(const value_type& x); std::pair insert(value_type&& x); iterator insert(const value_type& x); // multimap iterator insert(value_type&& x); // multimap iterator insert(const_iterator hint, const value_type& x); iterator insert(const_iterator hint, value_type&& x); template void insert(InputIterator first, InputIterator last); void insert(std::initializer_list il); ``` -------------------------------- ### Configure Vector for Memory Constraints Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Example of configuring a Boost.Container vector to use 16-bit size storage for memory-constrained environments. This limits the maximum number of elements to 65535. ```cpp #include #include // Use 16-bit size storage typedef boost::container::vector_options< boost::container::growth_factor_1_5x, // More conservative growth std::uint16_t // 16-bit sizes (max 65535 elements) > ConstrainedOptions; boost::container::vector v; ``` -------------------------------- ### rfind() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the last occurrence of a substring or character within a string. It supports searching for another string, a C-style string, or a single character, starting from a specified position. ```APIDOC ## rfind() ### Description Finds the last occurrence of a substring or character within a string. It supports searching for another string, a C-style string, or a single character, starting from a specified position. ### Method const ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response - **return value** (size_type) - Position of last match, or `npos` if not found #### Response Example None ``` -------------------------------- ### Map Constructors Source: https://github.com/boostorg/container/blob/develop/_autodocs/associative_containers.md Demonstrates various ways to construct a boost::container::map, including default construction, construction with a custom comparator, from iterators, and using initializer lists. ```cpp map(); explicit map(const Compare& comp); explicit map(const Compare& comp, const allocator_type& a); explicit map(const allocator_type& a); template map(InputIterator first, InputIterator last); template map(InputIterator first, InputIterator last, const Compare& comp); template map(InputIterator first, InputIterator last, const Compare& comp, const allocator_type& a); map(const map& x); map(map&& x); map(std::initializer_list il); map(std::initializer_list il, const Compare& comp); map(std::initializer_list il, const Compare& comp, const allocator_type& a); ``` -------------------------------- ### find() Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the first occurrence of a substring or character within a string. It supports searching for another string, a C-style string, or a single character, starting from a specified position. ```APIDOC ## find() ### Description Finds the first occurrence of a substring or character within a string. It supports searching for another string, a C-style string, or a single character, starting from a specified position. ### Method const ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response - **return value** (size_type) - Position of first match, or `npos` if not found #### Response Example None ### Complexity O(size() * substring_size()) in worst case ``` -------------------------------- ### C++ String rfind() Operation Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the last occurrence of a substring within a string. Supports searching for substrings, C-strings, or individual characters starting from a specified position. ```cpp size_type rfind(const basic_string& x, size_type pos = npos) const; size_type rfind(const CharT* s, size_type pos = npos) const; size_type rfind(const CharT* s, size_type pos, size_type n) const; size_type rfind(CharT c, size_type pos = npos) const; ``` -------------------------------- ### Constructor from C-String for basic_string Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Initializes a string from a null-terminated C-string, optionally with a specified allocator. The complexity is linear to the string length, and it may throw if memory allocation fails. ```cpp basic_string(const CharT* s); basic_string(const CharT* s, const allocator_type& a); ``` -------------------------------- ### C++ String find() Operation Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Finds the first occurrence of a substring within a string. Supports searching for substrings, C-strings, or individual characters starting from a specified position. ```cpp size_type find(const basic_string& x, size_type pos = 0) const; size_type find(const CharT* s, size_type pos = 0) const; size_type find(const CharT* s, size_type pos, size_type n) const; size_type find(CharT c, size_type pos = 0) const; ``` -------------------------------- ### C++ Shrink-to-fit Test Setup Source: https://github.com/boostorg/container/blob/develop/doc/allocplus/index.html Sets up a test for the 'shrink_to_fit' idiom in C++ vectors, utilizing the 'shrink_in_place' option. This code snippet defines the step size for the test. ```cpp const std::size_t Step = 5; ptime tini = microsec_clock::universal_time(); ``` -------------------------------- ### flat_map Constructors Source: https://github.com/boostorg/container/blob/develop/_autodocs/flat_containers.md Demonstrates various ways to construct a `flat_map` object, including default construction, construction with a custom comparator, construction with an allocator, and construction from iterator ranges or initializer lists. It also shows construction from a sorted range for efficiency. ```cpp flat_map(); explicit flat_map(const Compare& comp); explicit flat_map(const Compare& comp, const allocator_type& a); explicit flat_map(const allocator_type& a); template flat_map(InputIterator first, InputIterator last); flat_map(InputIterator first, InputIterator last, const Compare& comp); flat_map(InputIterator first, InputIterator last, const Compare& comp, const allocator_type& a); flat_map(const flat_map& x); flat_map(flat_map&& x); flat_map(std::initializer_list il); flat_map(std::initializer_list il, const Compare& comp); flat_map(std::initializer_list il, const Compare& comp, const allocator_type& a); // Construct from sorted range template flat_map(sorted_unique_t, InputIterator first, InputIterator last); flat_map(sorted_unique_t, std::initializer_list il); ``` -------------------------------- ### Constructor from Buffer for basic_string Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Constructs a string from a character buffer of a specified size, which is not necessarily null-terminated. An optional allocator can be provided. This constructor has linear complexity with respect to the buffer size and may throw on allocation failure. ```cpp basic_string(const CharT* s, size_type n); basic_string(const CharT* s, size_type n, const allocator_type& a); ``` -------------------------------- ### push_front() Source: https://github.com/boostorg/container/blob/develop/_autodocs/deque.md Prepends a new element to the beginning of the deque. ```APIDOC ## push_front() ### Description Prepends an element to the beginning of the deque. ### Parameters - `x`: Value to prepend. ### Complexity Constant (amortized) ### Throws If allocation or element construction throws. ``` -------------------------------- ### push_front() - Prepend an element Source: https://github.com/boostorg/container/blob/develop/_autodocs/deque.md Prepends a new element to the beginning of the deque. Complexity is constant amortized. May throw if allocation or element construction fails. ```cpp void push_front(const T& x); ``` ```cpp void push_front(T&& x); ``` -------------------------------- ### Configure Deque for Cache Optimization Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Example of configuring a Boost.Container deque with larger block sizes (4096 elements per block, 64 blocks per chunk) for improved cache efficiency. ```cpp #include #include // Larger blocks for better cache efficiency typedef boost::container::deque_options<4096, 64> CacheOptimizedOptions; boost::container::deque d; ``` -------------------------------- ### Using small_vector with Inline Storage Source: https://github.com/boostorg/container/blob/develop/_autodocs/special_containers.md Demonstrates the basic usage of small_vector, showing how elements are stored inline up to the specified capacity (N=5) and how allocation occurs when this capacity is exceeded. Include . ```cpp #include #include int main() { using namespace boost::container; // Inline storage for 5 elements small_vector v; // No dynamic allocation for first 5 elements for (int i = 0; i < 5; ++i) { v.push_back(i); } // Allocation happens here (exceeds inline capacity) v.push_back(5); // But still efficient for the common case of small collections std::cout << v.size() << std::endl; return 0; } ``` -------------------------------- ### begin(), end(), cbegin(), cend() Source: https://github.com/boostorg/container/blob/develop/_autodocs/vector.md Provides iterators to traverse the elements of the vector. `begin()` and `end()` return iterators, while `cbegin()` and `cend()` return const iterators. ```APIDOC ## Iterators ### begin() and end() ```cpp iterator begin(); const_iterator begin() const; const_iterator cbegin() const; iterator end(); const_iterator end() const; const_iterator cend() const; ``` Returns iterators to the beginning and end of the vector. - **Complexity:** Constant - **Exception Safety:** No throw ``` -------------------------------- ### Constructor with Allocator for basic_string Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md Creates an empty string using a specified allocator. This operation is constant time and exception-safe. ```cpp explicit basic_string(const allocator_type& a); ``` -------------------------------- ### Size Source: https://github.com/boostorg/container/blob/develop/_autodocs/flat_containers.md Query the current size, maximum possible size, and capacity of the container. `reserve()` can be used to pre-allocate memory. ```APIDOC ## Size `size_type size() const;` `bool empty() const;` `size_type max_size() const;` `size_type capacity() const;` `void reserve(size_type n);` `void shrink_to_fit();` ``` -------------------------------- ### Efficient Insertion into Boost.Container Map Source: https://github.com/boostorg/container/blob/develop/_autodocs/README.md Shows how to efficiently insert elements into a boost::container::map, including checking if an insertion occurred and using hints for potentially faster insertion. ```cpp boost::container::map m; // Check and insert auto result = m.insert({"key", 10}); if (result.second) { std::cout << "Inserted" << std::endl; } // Insert with hint auto it = m.insert(m.lower_bound("key"), {"key", 10}); ``` -------------------------------- ### Custom Allocator Usage with Boost Containers Source: https://github.com/boostorg/container/blob/develop/_autodocs/allocators.md Demonstrates using different allocators like node_allocator and adaptive_pool for vector and list, and then using scoped_allocator_adaptor for nested container allocation. ```cpp #include #include #include int main() { using namespace boost::container; // Vector with node_allocator for node-based operations typedef vector> VectorWithNodeAlloc; VectorWithNodeAlloc v; v.push_back(1); v.push_back(2); // List with adaptive_pool for dynamic allocation patterns typedef list> ListWithAdaptivePool; ListWithAdaptivePool l; l.push_back(10); l.push_back(20); // Scoped allocator for nested containers typedef scoped_allocator_adaptor> ScopedAlloc; ScopedAlloc alloc; vector sv(alloc); sv.push_back(100); return 0; } ``` -------------------------------- ### Boost.Container Main Container Headers Source: https://github.com/boostorg/container/blob/develop/_autodocs/README.md Lists the primary header files for sequence and associative containers in Boost.Container, including their respective functionalities. ```text boost/container/ ├── vector.hpp # vector ├── deque.hpp # deque ├── list.hpp # list ├── slist.hpp # slist ├── set.hpp # set, multiset ├── map.hpp # map, multimap ├── flat_set.hpp # flat_set, flat_multiset ├── flat_map.hpp # flat_map, flat_multimap ├── string.hpp # string, basic_string ├── static_vector.hpp # static_vector ├── small_vector.hpp # small_vector ├── devector.hpp # devector ├── segtor.hpp # segtor └── hub.hpp # hub ``` -------------------------------- ### Common Container Assignment Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Shows how to assign values to container objects using copy, move, and initializer list assignment operators. ```cpp // Assignment Container& operator=(const Container& x); Container& operator=(Container&& x); Container& operator=(std::initializer_list il); ``` -------------------------------- ### Sequence Container Capacity Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Provides methods to query the current capacity and manage memory allocation, such as reserving space or shrinking to fit. ```cpp // Capacity (not on list/slist) size_type capacity() const; void reserve(size_type n); void shrink_to_fit(); ``` -------------------------------- ### Sorting and Unique with Boost.Container Vector Source: https://github.com/boostorg/container/blob/develop/_autodocs/configuration.md Demonstrates using standard algorithms like std::sort and std::unique with boost::container::vector. Ensure you include the necessary headers for both the container and the algorithms. ```cpp #include #include int main() { boost::container::vector v = {3, 1, 4, 1, 5}; std::sort(v.begin(), v.end()); std::unique(v.begin(), v.end()); return 0; } ``` -------------------------------- ### Constructors for boost::container::basic_string Source: https://github.com/boostorg/container/blob/develop/_autodocs/string_containers.md This section covers the various ways to construct a boost::container::basic_string object, including default construction, from C-strings, buffers, fill characters, ranges, substrings, and other strings. ```APIDOC ## Constructors for boost::container::basic_string ### Default Constructor ```cpp basic_string(); ``` Creates an empty string. - **Complexity:** Constant - **Exception Safety:** No throw ### Constructor with Allocator ```cpp explicit basic_string(const allocator_type& a); ``` Creates an empty string with specified allocator. - **Complexity:** Constant - **Exception Safety:** No throw ### Constructor from C-String ```cpp basic_string(const CharT* s); basic_string(const CharT* s, const allocator_type& a); ``` Creates a string from a null-terminated C-string. - **Parameters:** - `s`: Pointer to null-terminated string - `a`: Optional allocator - **Complexity:** Linear to string length - **Throws:** If allocation fails ### Constructor from Buffer ```cpp basic_string(const CharT* s, size_type n); basic_string(const CharT* s, size_type n, const allocator_type& a); ``` Creates a string from a buffer of `n` characters (not necessarily null-terminated). - **Parameters:** - `s`: Pointer to character buffer - `n`: Number of characters to copy - `a`: Optional allocator - **Complexity:** Linear to `n` - **Throws:** If allocation fails ### Fill Constructor ```cpp basic_string(size_type n, CharT c); basic_string(size_type n, CharT c, const allocator_type& a); ``` Creates a string with `n` copies of character `c`. - **Parameters:** - `n`: Number of characters - `c`: Character to fill with - `a`: Optional allocator - **Complexity:** Linear to `n` - **Throws:** If allocation fails ### Range Constructor ```cpp template basic_string(InputIterator first, InputIterator last); template basic_string(InputIterator first, InputIterator last, const allocator_type& a); ``` Creates a string from a character range. - **Parameters:** - `first, last`: Input iterators defining range [first, last) - `a`: Optional allocator - **Complexity:** Linear to range size - **Throws:** If allocation fails ### Substring Constructor ```cpp basic_string(const basic_string& x, size_type pos); basic_string(const basic_string& x, size_type pos, const allocator_type& a); basic_string(const basic_string& x, size_type pos, size_type n); basic_string(const basic_string& x, size_type pos, size_type n, const allocator_type& a); ``` Creates a string from a substring of another string. - **Parameters:** - `x`: Source string - `pos`: Starting position - `n`: Number of characters (if omitted, takes all from `pos` to end) - `a`: Optional allocator - **Throws:** `std::out_of_range` if `pos > x.size()` ### Copy Constructor ```cpp basic_string(const basic_string& x); basic_string(const basic_string& x, const allocator_type& a); ``` Copies another string. - **Complexity:** Linear to source string size - **Throws:** If allocation fails ### Move Constructor ```cpp basic_string(basic_string&& x); basic_string(basic_string&& x, const allocator_type& a); ``` Transfers ownership from another string. - **Complexity:** Constant if allocators compare equal; linear otherwise - **Exception Safety:** No throw if allocators compare equal ### Initializer List Constructor ```cpp basic_string(std::initializer_list il); basic_string(std::initializer_list il, const allocator_type& a); ``` Constructs from an initializer list. - **Complexity:** Linear to list size - **Note:** Requires C++11 ``` -------------------------------- ### boost::container::list Size and Capacity Source: https://github.com/boostorg/container/blob/develop/_autodocs/list_and_slist.md Provides methods to check the current size, maximum possible size, and whether the list is empty. ```APIDOC ## Size and Capacity ```cpp size_type size() const; bool empty() const; size_type max_size() const; ``` ``` -------------------------------- ### Sequence Container Assignment Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/api_index.md Shows how to assign a range of elements or a specified number of elements to a sequence container. ```cpp void assign(size_type n, const T& value); template void assign(InputIterator first, InputIterator last); void assign(std::initializer_list il); ``` -------------------------------- ### Map Emplace Methods Source: https://github.com/boostorg/container/blob/develop/_autodocs/associative_containers.md Shows how to construct elements in-place within the map using `emplace` and `emplace_hint`. These methods can be more efficient than `insert` by avoiding unnecessary copies. ```cpp template std::pair emplace(Args&&... args); // map template iterator emplace(Args&&... args); // multimap template iterator emplace_hint(const_iterator hint, Args&&... args); ```