如何为动态数组设置特定值?

时间:2017-12-09 14:08:08

标签: c++ arrays memory

显然我误解了这一点,但我试图简单地将特定数字设置为动态内存阵列。

#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;
}

2 个答案:

答案 0 :(得分:1)

对多维数组的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;
}

答案 1 :(得分:1)

使用operator new很棘手。您的程序在使用new和delete方面存在许多错误。有一个动态数组的标准实现,它隐藏了所有的技巧并在它自身之后进行清理,即std :: vector。另外,避免使用&#34;使用命名空间std;&#34;你可以通过这种方式获得令人费解的名称冲突。

效果很好 -

#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 } };
相关问题