SoFunction
Updated on 2025-03-02

Detailed explanation of C++ Boost PointerContainer smart pointer

1. Summary

In C++11, it is another smart pointer, which is generally used to generate collection data. This article explains the characteristics and usage of this pointer.

2. Smart pointer

Library Provides containers specifically for managing dynamic allocation of objects. For example, in C++11, you can create such a container using std::vector<std::unique_ptr<int>> . However, containers from can provide some additional convenience.

Example2.boost::ptr_vector

#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
int main()
{
  boost::ptr_vector<int> v;
  v.push_back(new int{1});
  v.push_back(new int{2});
  std::cout << () << '\n';
}

The class boost::ptr_vector basically works like std::vector<std::unique_ptr<int>> (see Example 2.1). However, because boost::ptr_vector knows that it stores dynamically allocated objects, member functions like back() return references to dynamically allocated objects, not pointers. Therefore, the example writes 2 to standard output.

Example 1.boost::ptr_setIn an intuitive and correct order

#include <boost/ptr_container/ptr_set.hpp>
#include <boost/ptr_container/indirect_fun.hpp>
#include <set>
#include <memory>
#include <functional>
#include <iostream>
int main()
{
  boost::ptr_set<int> s;
  (new int{2});
  (new int{1});
  std::cout << *() << '\n';
  std::set<std::unique_ptr<int>, boost::indirect_fun<std::less<int>>> v;
  (std::unique_ptr<int>(new int{2}));
  (std::unique_ptr<int>(new int{1}));
  std::cout << **() << '\n';
}

Example 1 illustrates another reason for using a dedicated container. This example stores dynamically allocated variables of type int in boost::ptr_set and std::set . std::set is used with std::unique_ptr.

With boost::ptr_set, the order of elements depends on the int value. std::set Compare pointers of type std::unique_ptr instead of variables referenced by pointers. To make std::set sort elements according to the int value, the container must be told how to compare elements. In Example 1, boost::indirect_fun (provided by) is used. Using boost::indirect_fun, std::set is told not to sort elements based on pointers of type std::unique_ptr, but rather based on the int value pointed by the pointer. This is why the example shows 1 twice.

In addition to boost::ptr_vector and boost::ptr_set , there are other containers that can be used to manage dynamically allocated objects. Examples of these additional containers include boost::ptr_deque, boost::ptr_list, boost::ptr_map, boost::ptr_unordered_set, and boost::ptr_unordered_map. These containers correspond to well-known containers in the standard library.

Example 2. Container Insert from

#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/ptr_container/ptr_inserter.hpp>
#include <array>
#include <algorithm>
#include <iostream>
int main()
{
  boost::ptr_vector<int> v;
  std::array<int, 3> a{{0, 1, 2}};
  std::copy((), (), boost::ptr_container::ptr_back_inserter(v));
  std::cout << () << '\n';
}

Provides an inserter for its container. They are defined in the namespace boost::ptr_container. To access the inserter, you must include the header file boost/ptr_container/ptr_inserter.hpp.

Example 2 Use the function boost::ptr_container::ptr_back_inserter(), which creates an inserter of type boost::ptr_container::ptr_back_insert_iterator. This inserter is passed to std::copy() to copy all numbers in array a to vector v. Because v is a container of type boost::ptr_vector, it requires the address of the int object that is dynamically allocated, so the inserter uses the new address on the heap and adds the address to the container.

In addition to boost::ptr_container::ptr_back_inserter() , the boost::ptr_container::ptr_front_inserter() and boost::ptr_container::ptr_inserter() functions are also provided to create corresponding inserters.

3. Practice

Use the member variables name, legs, and has_tail to create a program containing multiple animal-type objects. Stores objects in a container. Sort the containers in ascending order by the legs and write all elements to standard output.

This is the end of this article about the detailed explanation of C++ Boost PointerContainer smart pointer. For more related C++ Boost PointerContainer content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!