STL:动态内存分配
参考文献《大道至简:C++STL》
- new/delete
#include <iostream>
#include <memory>
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 <iostream>
#include <memory>
#include <string>
using namespace std;
int main()
{
auto_ptr<string> str1(new string("okokok!"));
auto_ptr<string> str2(str1);
auto_ptr<string> 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;
}
2086

被折叠的 条评论
为什么被折叠?



