C++ - "scoped_array" 的 详解

"scoped_array" 的 详解


本文地址: http://blog.csdn.net/caroline_wendy/article/details/24174207


参考: http://www.boost.org/doc/libs/1_54_0/libs/smart_ptr/scoped_array.htm


The scoped_array class template stores a pointer to a dynamically allocated array.

scoped_array类模板存储了一个指向动态分配数组的指针, 可以自动析构堆(heap)上所使用的内存.

头文件: #include <boost/scoped_array.hpp>


代码:

/*
 * test.cpp
 *
 *  Created on: 2014.04.18
 *      Author: Spike
 */

/*eclipse cdt, gcc 4.8.1*/

#include <iostream>

#include <boost/scoped_array.hpp>

using namespace std;

int main() {
	const int n(3);
	boost::scoped_array<int> pData(new int[n*n]);

	int temp[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

	pData.reset(temp);

	for (std::size_t i=0; i<n*n; ++i) {
		std::cout << "pData[" << i << "] = " << pData[i] << std::endl;
	}

	return 0;
}

输出:

pData[0] = 0
pData[1] = 1
pData[2] = 2
pData[3] = 3
pData[4] = 4
pData[5] = 5
pData[6] = 6
pData[7] = 7
pData[8] = 8


C++ - "scoped_array" 的 详解_第1张图片


你可能感兴趣的:(C++,header,scoped_array,Mystra,自动析构)