将静态数组复制到动态数组C ++
copying static array to dynamic array c++
当我注意到这一点时,我正在研究 C++ 的一些基础知识:
int * ptr = new int[3], arr[3] = { 11,13,15 };
for (int i = 0; i < 3; i++)
ptr[i] = arr[i];
cout << ptr[0] << ' ' << ptr[1] << ' ' << ptr[2] << endl;
此代码将完美地插入并打印出值:11 13 15。
而当我这样写时:
int size=0, * ptr = new int[size], arr[3] = { 11,13,15 };
for (int i = 0; i < 3; i++)
{
ptr = new int[++size];
ptr[i] = arr[i];
}
cout << ptr[0] << ' ' << ptr[1] << ' ' << ptr[2] << endl;
当我一次增加一个值时,打印出来:
-842150451 -842150451 15
为什么我一开始定义总大小时,值插入正确,但是当我一次增加一步时却没有插入?另外,请问有什么方法可以编写第二个代码,但请以它可以工作的方式编写?
正如@Ch3esteR 在评论中指出的那样。问题是你正在重新分配你的
数组在每次迭代中,从而归零(或者在你的情况下,捕捉一些随机的
堆上的值)。
我想说更大的解决方案可能是使用标准容器来处理内存。 Here is a solution 用于修复您的原始代码 original()
以及关于如何使用 std::vector.
处理动态数组的建议替代方案
#include <vector>
#include <iostream>
using namespace std;
void original() {
const int arr[3] = { 11,13,15 };
const int size=3;
auto ptr = new int[size]; // this is what you want to do
std::copy(arr, arr + size, ptr); // Standard function for copying
cout << ptr[0] << ' ' << ptr[1] << ' ' << ptr[2] << endl;
}
int main() {
original();
const int arr[3] = { 11,13,15 }; // You might want to make this a std::vector too
std::vector<int> result{arr, arr + 3};
// std::vector<int> result; // Or if the question was how to do it dynamically
// for (auto i: arr)
// {
// result.push_back(i); // Dynamic add
// }
cout << result[0] << ' ' << result[1] << ' ' << result[2] << endl;
}
根据要求:如何用指针来做(不要这样做,除非你有一些外部约束迫使你这样做,比如老师)
#include <iostream>
using namespace std;
// Reallocate memory and delete the old pointer
int *resize(int *arr, size_t oldSize, size_t newSize) {
auto newArr = new int[newSize];
auto copySize = std::min(oldSize, newSize); // Handle both growing and shrinking resize
std::copy(arr, arr + copySize, newArr); // Copy your old data
// Or do it like this
// for (size_t i = 0; i < copySize, ++i) {
// newArray[i] = arr[i]
// }
delete [] arr;
return newArr;
}
int main() {
auto arr = new int[2] { 11,13 }; // Observe 'new' here
size_t size = 3;
// "resize" the array arr (in practice you are just creating a new one)
arr = resize(arr, size, size + 1);
// Assign your new value
arr[2] = 14;
cout << arr[0] << ' ' << arr[1] << ' ' << arr[2] << endl;
delete [] arr; // Remember, try to not manual memory management,
// only use it in class when you are learning the basics, otherwise use
// std::unique_ptr or std::shared_ptr
}
当我注意到这一点时,我正在研究 C++ 的一些基础知识:
int * ptr = new int[3], arr[3] = { 11,13,15 };
for (int i = 0; i < 3; i++)
ptr[i] = arr[i];
cout << ptr[0] << ' ' << ptr[1] << ' ' << ptr[2] << endl;
此代码将完美地插入并打印出值:11 13 15。
而当我这样写时:
int size=0, * ptr = new int[size], arr[3] = { 11,13,15 };
for (int i = 0; i < 3; i++)
{
ptr = new int[++size];
ptr[i] = arr[i];
}
cout << ptr[0] << ' ' << ptr[1] << ' ' << ptr[2] << endl;
当我一次增加一个值时,打印出来:
-842150451 -842150451 15
为什么我一开始定义总大小时,值插入正确,但是当我一次增加一步时却没有插入?另外,请问有什么方法可以编写第二个代码,但请以它可以工作的方式编写?
正如@Ch3esteR 在评论中指出的那样。问题是你正在重新分配你的 数组在每次迭代中,从而归零(或者在你的情况下,捕捉一些随机的 堆上的值)。
我想说更大的解决方案可能是使用标准容器来处理内存。 Here is a solution 用于修复您的原始代码 original()
以及关于如何使用 std::vector.
#include <vector>
#include <iostream>
using namespace std;
void original() {
const int arr[3] = { 11,13,15 };
const int size=3;
auto ptr = new int[size]; // this is what you want to do
std::copy(arr, arr + size, ptr); // Standard function for copying
cout << ptr[0] << ' ' << ptr[1] << ' ' << ptr[2] << endl;
}
int main() {
original();
const int arr[3] = { 11,13,15 }; // You might want to make this a std::vector too
std::vector<int> result{arr, arr + 3};
// std::vector<int> result; // Or if the question was how to do it dynamically
// for (auto i: arr)
// {
// result.push_back(i); // Dynamic add
// }
cout << result[0] << ' ' << result[1] << ' ' << result[2] << endl;
}
根据要求:如何用指针来做(不要这样做,除非你有一些外部约束迫使你这样做,比如老师)
#include <iostream>
using namespace std;
// Reallocate memory and delete the old pointer
int *resize(int *arr, size_t oldSize, size_t newSize) {
auto newArr = new int[newSize];
auto copySize = std::min(oldSize, newSize); // Handle both growing and shrinking resize
std::copy(arr, arr + copySize, newArr); // Copy your old data
// Or do it like this
// for (size_t i = 0; i < copySize, ++i) {
// newArray[i] = arr[i]
// }
delete [] arr;
return newArr;
}
int main() {
auto arr = new int[2] { 11,13 }; // Observe 'new' here
size_t size = 3;
// "resize" the array arr (in practice you are just creating a new one)
arr = resize(arr, size, size + 1);
// Assign your new value
arr[2] = 14;
cout << arr[0] << ' ' << arr[1] << ' ' << arr[2] << endl;
delete [] arr; // Remember, try to not manual memory management,
// only use it in class when you are learning the basics, otherwise use
// std::unique_ptr or std::shared_ptr
}