您如何将特定值设置为动态数组
How do you set specific values to a dynamic array?
显然我正在误解这一点,但是我试图将特定的数字设置为动态内存数组。
#include <iostream>
#include <cstdlib>
using namespace std;
int main () {
int *arr = new int [4][5]
{{3, 6, 9, 23, 16},
{24, 12, 9, 13, 5},
{37, 19, 43, 17, 11},
{71, 32, 8, 4, 7}};
cout<< arr [1][3]<< endl<< endl;
int *x = new int;
*x = arr [2][1];
cout<< x<< endl;
cout<< *x<< endl << endl;
delete x;
*x = arr [0][3];
cout<< x<< endl;
cout<< *x<< endl;
delete x;
delete [] arr;
return;
}
c 对多维数组的支持是通过库(例如Boost)完成的。如果没有类,它实际上只能理解1D数组,尤其是当您使用指针时,C/C 确实将其视为指向数组的第一个元素的指针。要使您的示例在没有类的情况下工作,您需要定义一种容纳一行的类型,然后创建该类型的数组,您可以在显示时分配值。
#include <iostream>
#include <cstdlib>
using namespace std;
int main () {
typedef int row_t[5];
row_t *arr = new row_t[4] {{3, 6, 9, 23, 16},
{24, 12, 9, 13, 5},
{37, 19, 43, 17, 11},
{71, 32, 8, 4, 7}};
cout<< arr[1][3] << endl<< endl;
int *x = new int;
*x = arr [2][1];
cout<< x<< endl;
cout<< *x<< endl << endl;
*x = arr [0][3];
cout<< x<< endl;
cout<< *x<< endl;
delete x;
delete [] arr;
return 0;
}
另外,您可以将2D数组投射到1D数组上:
#include <iostream>
#include <cstdlib>
using namespace std;
int main () {
int *arr = new int[20] {3, 6, 9, 23, 16,
24, 12, 9, 13, 5,
37, 19, 43, 17, 11,
71, 32, 8, 4, 7};
cout<< arr[5*1+3] << endl<< endl;
int *x = new int;
*x = arr [5*2+1];
cout<< x<< endl;
cout<< *x<< endl << endl;
*x = arr [5*0+3];
cout<< x<< endl;
cout<< *x<< endl;
delete x;
delete [] arr;
return 0;
}
使用动态数据获得2D索引,使用boost :: multi_array
#include <iostream>
#include <boost/multi_array.hpp>
using namespace std;
int main () {
boost::multi_array< int, 2 >arr(boost::extents[4][5]);
int tmp[] { 3, 6, 9, 23, 16,
24, 12, 9, 13, 5,
37, 19, 43, 17, 11,
71, 32, 8, 4, 7 };
arr = boost::multi_array_ref< int, 2 >( &tmp[0], boost::extents[4][5] );
cout<< arr [1][3]<< endl << endl;
int *x = new int;
*x = arr [2][1];
cout<< x<< endl;
cout<< *x<< endl << endl;
*x = arr [0][3];
cout<< x<< endl;
cout<< *x<< endl;
delete x;
// delete [] arr;
return 0;
}
使用操作员新的是棘手。您的程序在使用新和删除方面存在许多错误。动态阵列的标准实现可以隐藏所有棘手的棘手并自行清理,即std :: vector。另外,避免使用"使用命名空间std";您可以以这种方式感到困惑的名字冲突。
效果很好 -
#include <iostream>
#include <cstdlib>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main () {
vector<vector<int>> arr
{ { 3, 6, 9, 23, 16 },
{ 24, 12, 9, 13, 5 },
{ 37, 19, 43, 17, 11 },
{ 71, 32, 8, 4, 7 } };
cout<< arr[1][3]<< endl<< endl;
int x = arr[2][1];
cout<< x<< endl;
cout<< x<< endl << endl;
x = arr[0][3];
cout<< x<< endl;
return 0;
}
P.S。您使用ARR的方式没有什么动态的。您可以这样声明:
int arr[4][5]
{ { 3, 6, 9, 23, 16 },
{ 24, 12, 9, 13, 5 },
{ 37, 19, 43, 17, 11 },
{ 71, 32, 8, 4, 7 } };
相关文章:
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- Vulkan 中的动态顶点缓冲区格式设置
- 如何设置动态创建的CMFCToolbar的原始状态?
- 如何在win32的窗口类中设置动态菜单?
- 如何在进入动态打开的共享库时设置断点?
- 基于 std::vector <string>的内容动态设置顺序的模式
- 如何在C++生成器中动态设置任务数(_di_ITask)?
- 如何在C++中只打印小数点而不设置限制,但动态设置精度
- 如何使用cmake设置OpenCV以启用动态和静态构建
- text_multifile_backend如何设置文件名的动态日期
- 在 QTabWidget 中动态设置单个选项卡的样式
- 当其他链接库链接 stdc++ 为动态时,如何将 libstdc++ 链接设置为静态?
- 无法设置动态数组底部 c++
- 内存堆,动态设置阵列
- 如何使用 CMake 正确设置动态库路径
- 如何设置动态 QFont 大小
- 如何设置动态分配数组的内容
- c++ |设置动态内存为变量
- 从外部函数/类设置动态数组的新大小
- 如何在对话框中设置动态数量的小部件