初始化指针数组和矩阵
initialize array of pointer and matrix
我想初始化指针数组。(不是普通数组)但这不起作用。
int* arr = new int [5];
arr = {1,2,3,4,5};
我也不想这样做:(因为如果尺寸改变我必须改变代码)
arr[0] = 1; arr[1] = 2; ...
有没有简单的方法来做到这一点?矩阵呢?
int** mat = ...
mat = { {1,2} , {3,4} }
而且我也不想这样初始化:(因为当我想将矩阵传递给函数时有一些限制(例如:如果大小改变,我必须改变函数定义))
int mat[2][2] = { {1,2} , {3,4} };
你可以这样写
int* arr = new int [5] { 1, 2, 3, 4, 5 };
或者例如您可以使用 std::iota
算法,例如
int* arr = new int [5];
std::iota( arr, arr + 5, 1 );
或其他一些算法,例如 std::fill
或 std::generate
。
如果要重新分配数组,那么在这种情况下最好使用标准容器 std::vector<int>
。
(For example: If size changes, I have to change function defenition))
您可以将函数定义为模板函数,其中数组的大小将是模板非类型参数。
如果你真的想自己动态创建一个数组,那么按照来自莫斯科的@Vlad 的建议去做:
int* arr = new int [5] {1, 2, 3, 4, 5};
或:
int* arr = new int [5];
std::iota( arr, arr + 5, 1 ); // also std::fill or std::generate
但是,在 99% 的情况下,使用 std::vector
几乎所有方面都更好。
您的代码如下所示:
std::vector<int> arr{1, 2, 3, 4, 5};
// if you know the size of the array at runtime, then do this
arr.resize(5 /* size of the array at runtime */)
更好的是,如果您在编译时知道数组的大小,那么 std::array
是您最好的朋友。
std::array<int, 5 /* size of the array at compile time */> arr{1, 2, 3, 4, 5};
这是一个使用 std::make_unique 来避免 new/delete 的例子。
但是,如您所见,数组大小必须手动维护。
所以你最好还是使用 std::vector 或 std::array
#include <algorithm>
#include <iostream>
#include <memory>
// allocate with make_unqiue and initialize from list
template<typename type_t, std::size_t N>
auto make_array(const type_t (&values)[N])
{
std::unique_ptr<type_t[]> array_ptr = std::make_unique<type_t[]>(N);
for (std::size_t n = 0; n < N; ++n) array_ptr[n] = values[n];
return array_ptr;
}
int main()
{
auto array_ptr = make_array({ 1,2,3,4,5 });
for (std::size_t n = 0; n < 5; ++n)
{
std::cout << array_ptr[n] << " ";
}
// std::unique_ptr will take care of deleting the memory
}
我想初始化指针数组。(不是普通数组)但这不起作用。
int* arr = new int [5];
arr = {1,2,3,4,5};
我也不想这样做:(因为如果尺寸改变我必须改变代码)
arr[0] = 1; arr[1] = 2; ...
有没有简单的方法来做到这一点?矩阵呢?
int** mat = ...
mat = { {1,2} , {3,4} }
而且我也不想这样初始化:(因为当我想将矩阵传递给函数时有一些限制(例如:如果大小改变,我必须改变函数定义))
int mat[2][2] = { {1,2} , {3,4} };
你可以这样写
int* arr = new int [5] { 1, 2, 3, 4, 5 };
或者例如您可以使用 std::iota
算法,例如
int* arr = new int [5];
std::iota( arr, arr + 5, 1 );
或其他一些算法,例如 std::fill
或 std::generate
。
如果要重新分配数组,那么在这种情况下最好使用标准容器 std::vector<int>
。
(For example: If size changes, I have to change function defenition))
您可以将函数定义为模板函数,其中数组的大小将是模板非类型参数。
如果你真的想自己动态创建一个数组,那么按照来自莫斯科的@Vlad 的建议去做:
int* arr = new int [5] {1, 2, 3, 4, 5};
或:
int* arr = new int [5];
std::iota( arr, arr + 5, 1 ); // also std::fill or std::generate
但是,在 99% 的情况下,使用 std::vector
几乎所有方面都更好。
您的代码如下所示:
std::vector<int> arr{1, 2, 3, 4, 5};
// if you know the size of the array at runtime, then do this
arr.resize(5 /* size of the array at runtime */)
更好的是,如果您在编译时知道数组的大小,那么 std::array
是您最好的朋友。
std::array<int, 5 /* size of the array at compile time */> arr{1, 2, 3, 4, 5};
这是一个使用 std::make_unique 来避免 new/delete 的例子。 但是,如您所见,数组大小必须手动维护。 所以你最好还是使用 std::vector 或 std::array
#include <algorithm>
#include <iostream>
#include <memory>
// allocate with make_unqiue and initialize from list
template<typename type_t, std::size_t N>
auto make_array(const type_t (&values)[N])
{
std::unique_ptr<type_t[]> array_ptr = std::make_unique<type_t[]>(N);
for (std::size_t n = 0; n < N; ++n) array_ptr[n] = values[n];
return array_ptr;
}
int main()
{
auto array_ptr = make_array({ 1,2,3,4,5 });
for (std::size_t n = 0; n < 5; ++n)
{
std::cout << array_ptr[n] << " ";
}
// std::unique_ptr will take care of deleting the memory
}