当前位置:首页 >> 编程开发 >> Visual C++ >> 内容

C++:scoped_array详解

时间:2014/11/14 作者:平凡之路 来源:xuhantao.com 浏览:

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

更多精彩内容:http://www.bianceng.cn/Programming/cplus/

相关文章
  • 没有相关文章
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 徐汉涛(www.xuhantao.com) © 2024 版权所有 All Rights Reserved.
  • 部分内容来自网络,如有侵权请联系站长尽快处理 站长QQ:965898558(广告及站内业务受理) 网站备案号:蒙ICP备15000590号-1