1. 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 << v.back() << std::endl; return 0; }
boost::ptr_vector knows that it stores dynamically allocated objects, member functions like back() return a reference to a dynamically allocated object and not a pointer. Thus, the example writes 2 to standard output.
2. boost:ptr_set 有顺序的容器
#include <boost/ptr_container/ptr_set.hpp> #include <iostream> int main() { boost::ptr_set<int> s; s.insert(new int(2)); s.insert(new int(1)); std::cout << *s.begin() << std::endl; return 0; }
输出: 1
3. additional containers include boost::ptr_deque, boost::ptr_list, boost::ptr_map, boost::ptr_unordered_set, boost::ptr_unordered_map