STL:动态内存分配

STL:动态内存分配,第1张

STL:动态内存分配 STL:动态内存分配

参考文献《大道至简:C++STL》

  • new/delete
#include 
#include 

using namespace std;

int main()
{
    int size;
    cin >> size;
    int * arr = new int[size];
    for(int i = 0; i < size; i++)
    {
        arr[i] = rand();
    }
    for(int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
    delete[] arr;
    return 0;
}
  • auto_ptr
#include 
#include 
#include 

using namespace std;

int main()
{
    auto_ptr str1(new string("okokok!"));
    auto_ptr str2(str1);
    auto_ptr str3(new string("str3"));
    cout << str1.get() << endl;     //0
    cout << str2.get() << endl;     //0x632270
    cout << str3.get() << endl;     //0x6322c0
    cout << *str2 << endl;
    cout << *str3 << endl;
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5099312.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存