【问题标题】:How do you set specific values to a dynamic array?如何为动态数组设置特定值?
【发布时间】:2017-12-09 14:08:08
【问题描述】:

显然我对此有误解,但我试图简单地将特定数字设置为动态内存数组。

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

【问题讨论】:

  • 显然,我什至不知道如何正确发布代码。
  • 你想用这段代码做什么?您正在使用new,这使一切变得非常复杂。你delete 内存然后访问它,然后delete 再次访问它。很破。然后你写了return;,它不应该编译,因为你需要returnint,或者完全忽略return
  • 欢迎来到 Stack Overflow。请阅读What topics can I ask about here?How do I ask a good question?
  • @user9076567 正如@klutt 所说。尤其是您应该在帖子中提出实际问题的部分。
  • 第一个delete x显然是错误的。在delete 之后仍然使用xx 也被删除两次,即 UB(未定义行为)。此外,您不需要在这个简单的程序中进行动态分配。

标签: c++ arrays memory


【解决方案1】:

C++ 对多维数组的支持是通过库完成的(例如 boost)。如果没有类,它实际上只能理解一维数组,尤其是当您使用指针时,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;
}

或者,您可以将二维数组投影到一维数组上:

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

【讨论】:

  • PS:endl 插入流的flush,这会使您的代码可能非常慢。请改用\n
  • 干得好。你能告诫一下使用 operator new 吗?
  • 哇!非常感谢,这让我发疯了。我知道有些事情我不明白。我什至没有考虑为此定义一个新类型。事实证明,自学 C++ 是一个相当大的挑战,你摇滚!
  • @Jive Dadson,其他人评论了使用new,我想给出对原始代码进行最小更改的示例。
【解决方案2】:

使用 operator new 很棘手。您的程序在使用 new 和 delete 方面存在许多错误。有一个动态数组的标准实现,它隐藏了所有的技巧并自行清理,即 std::vector。另外,避免使用“using namespace 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;
}

附:您使用 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 } };

【讨论】:

  • 我知道我没有正确地动态使用内存,当我遇到这个障碍时,我并没有在代码中走得那么远。老实说,我什至还没有了解向量是什么。使用命名空间 std 有什么问题; ?
  • @user9076567 问题不在于using namespace std 有什么问题,而是我们为什么要在 C++ 中引入命名空间。当您了解您将停止包含命名空间时,尤其是在全局范围内。 @Jive Dadson 的 using 单个函数的示例更安全。最好将using 语句放入main 函数中,以进一步缩小语句的范围。
  • 请注意,我在这里给出的int arr[4][5] 和使用vector&lt;vector&lt;int&gt;&gt;typedef ... row_t 的multi_array、int[] 示例之间可能存在显着差异。那是在内存中数据的内存布局。 multi_arry、int[] 和 int[][] 将具有单个连续的整数块。 vector&lt;vector&lt;int&gt;&gt; 将每一行放在一个单独的内存位置。 row_t 示例可能连续打包 row_t 对象或填充它们以进行内存对齐(这不太可能发生,但可能发生在字符数组中)。
猜你喜欢
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多