【C++14算法】make_unique_c++ make_unique-CSDN博客
c++20智能指针支持数组创建_make_shared 数组-CSDN博客
#include <iostream>
#include <memory>
int main() {
std::size_t size = 5;
std::unique_ptr<int[]> ptr = std::make_unique<int[]>(size);
for (std::size_t i = 0; i < size; ++i) {
ptr[i] = i + 1;
}
std::cout << "Array: ";
for (std::size_t i = 0; i < size; ++i) {
std::cout << ptr[i] << " ";
}
std::cout << std::endl;
return 0;
}
智能指针make_unique初始化数组的3种方法_make_unique 数组-CSDN博客
void TestArray()
{
auto pa1 = std::make_shared<int[]>(5);
auto pa2 = std::make_shared<int[5]>();
std::cout << pa1[0] << "-" << pa1[4] << std::endl;
std::cout << pa2[1] << "-" << pa1[3] << std::endl;
pa1[0] = 8;
pa1[1] = 9;
pa2[4] = 10;
pa2[2] = 3;
auto ptemp = pa1.get();
std::cout << ptemp << std::endl;
std::cout << pa1[0] << "---" << pa2[2] << std::endl;
}
int main()
{
TestArray();
}