#include <boost/smart_ptr.hpp> using namespace boost; using namespace std; void main() { boost::shared_array<int> pArrayDatas(new int[20]); for (int i = 10; i < 30; ++i) { pArrayDatas[i-10] = i; } for (int j = 0; j < 20; ++j) { cout << pArrayDatas[j] << ...
void TestSharedArray(boost::shared_array<Simple> memory) { // 注意:无需使用 reference (或 const reference) std::cout << "TestSharedArray UseCount: " << memory.use_count() << std::endl; } void TestSharedArray2() { boost::shared_array<Simple> my_memory(new Simple[2]); if (my_m...
1. AI检测代码解析 #include<boost/array.hpp>#include<iostream>using namespace boost;using namespace std;template<typename T>void printArray(const T& t){ typename T::const_iterator iter; for(iter = t.begin();iter!=t.end();++iter){ cout << *iter << " "; } cout << endl;}int mai...
当离开作用域时,shared_ptr自动调用fclose来关闭文件。 4.5 shared_array 用法如同scoped_ptr,可以考虑更为安全的vecto,这里不做详细介绍。 5. weak_ptr 5.1 基本介绍 协助shared_ptr使用,像旁观者一样观测资源的使用情况。 weak_ptr不影响引用计数。 5.2 获得this的shared_ptr 获取this指针的shared_ptr,使对象产生...
共享数组对应的类型是 boost::shared_array,它的定义在 boost/shared_array.hpp 里。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 #include "boost/version.hpp"#include "boost/config.hpp"#include "boost/scoped_ptr.hpp" // 作用域指针#include "boost/scoped_array.hpp"#include "boost/shared_...
2.3、auto_ptr很好用,但是没有覆盖智能指针的全部领域,尤其是引用计数型的智能指针;boost.smart_ptr库是对C++98的一个绝佳补充,它提供了六种智能指针:scoped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr、intrusive_ptr,库中的两个类shared_ptr和weak_ptr已经加入到C++的新标准中TR1库中。
namespace boost { template class shared_array { public:typedef T element_type;explicit shared_array(T * p = 0);template shared_array(T * p, D d);
#include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> #include <boost/scoped_array.hpp> #include <boost/scoped_ptr.hpp> #include <iostream> using namespace std; class X { public: X() { cout << "X ..." << endl;
(socket, endpoints); //socket同步的读写,buffer对象可以接受string、array、vector socket.write_some(asio::buffer(msg), error); size_t len = socket.read_some(asio::buffer(buf), error); //socket异步的读写 socket.async_write_some(asio::buffer(msg), &callback); socket.async_read_some(asio...
scoped_array 轻巧方便,没有给程序增加额外负担,但是 scoped_array 功能有限,不能动态增长,也没有迭代器支持,不能搭配 STL 算法,仅有一个纯粹的“裸”数组接口。在需要动态数组的情况下我们应该使用 std::vector 。例如:boost::shared_ptr 主要特点 boost.smart_ptr 库中最有价值,最重要的组成部分。支持...