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
作者:csdn博客 Spike_King