【问题标题】:C++: Dynamically create array named after for loop iteratorC ++:动态创建以for循环迭代器命名的数组
【发布时间】:2013-03-02 18:54:32
【问题描述】:

嘿,所以我想创建 n 大小为 x(也关闭用户输入)的数组(基于用户输入)。我想这样做的方式是让 for 循环执行n 迭代,并在循环内向用户询问x。问题是我不确定如何使用变量n 来命名数组,我在想这样的事情:

cout << "Enter n: ";
cin >> n

for (i = 0; i < n; i++)
{
    cout << "Enter x: ";
    cin >> x;

    double*array+i;
    array+i = new double[x]
}

总结一下我的问题是:你能在 C++ 中使用变量创建/命名数组吗?

【问题讨论】:

  • 在 C++ 中无法做到这一点。您基本上需要创建一个数组数组(或者,因为这是 c++,所以 vectorvectors)
  • "以 for 循环迭代器命名的数组" - 为什么需要具有特定名称的数组?这听起来不像是一件正确的事情。只需使用std::vector&lt; std::vector&lt;double&gt; &gt;
  • 我不确定我是否理解您要执行的操作。如果您使用的是 C++,请选择 std::vector&lt;std::vector&lt;double&gt;&gt; myMatrix,它允许您访问内部数组中的项目,如下所示:myMatrix[x][y]。如果你想使用固定大小的数组,那么你可以选择std::array&lt;std::vector&lt;double, N&gt;, M&gt; myMatrix,它会创建一个 MxN 矩阵。请使用en.cppreference.com/w 了解如何使用这些容器。
  • @MihaiTodor 这是定义矩阵的糟糕方式!你的记忆将无处不在;请不要建议人们那样做!
  • 欢迎来到 Stack Exchange Henry!如果您在问题中提供尽可能多的信息,这将很有用。例如,你为什么要这样做?你想达到什么目的?

标签: c++ arrays variables naming


【解决方案1】:

不幸的是,您不能在 C++ 中执行此操作。试试这样的...

std::cout << "Enter n: ";
std::cin >> n

std::vector<std::vector<double> > arrays(n);

for (std::size_t i = 0; i < n; i++)
{
    std::cout << "Enter x: ";
    std::cin >> x;

    arrays[i].reserve(x);
}

reserve 只分配,但不构造std::vector 中的对象;如果您也想构建它们,请使用resize

PS 永远不要使用using namespace std;它使您的代码更难阅读和调试。

【讨论】:

  • +1,我会更改保留以调整大小。保留只改变容量,不改变大小
【解决方案2】:

由于您使用 C++ 进行编程,因此您应该使用 STL 容器(尤其是 std::vector)而不是 C 样式的数组。

如果您需要使用在运行时创建的字符串来访问数组,那么您可以使用std::map&lt; std::string, std::vector&lt;double&gt; &gt;,不过这是一个非常疯狂的想法:

typedef std::vector<double> MyVector;
std::map<std::string, MyVector> myVectors;

// create an array:
std::string arrayName;
arrayName = std::string("someArray");       // could be: std::cin >> arrayName;
myVectors[arrayName] = MyVector(10, 1.23);  // new vector of size 10

std::cout << myVectors["someArray"][4];     // prints 1.23

我不确定您想要实现的具体目标是什么,但很可能有更合适的解决方案。真的有必要通过它们的名字访问这些数组吗?我很确定普通的std::vector&lt; std::vector&lt;double&gt; &gt; 在这里就足够了。

【讨论】:

    【解决方案3】:

    这里有 3 个解决方案:第一个最接近您的示例代码,第二个是为了能够正确检索边界内的数组元素而进行的改进,第三个是您最好使用 vector 的原因秒。

    解决方案 1:

    看起来您希望您的数组具有可通过循环迭代器区分的名称。就像 Joe 说的那样,你可以有一个数组的数组,所以内部数组将被命名为 array[0]、array[1]、...、array[n - 1]。这将通过使用指向双精度指针的指针来实现。每个内部指针都将用于动态分配双精度数组。不要忘记删除动态分配的内存。

    #include <iostream>
    int main()
    {
        unsigned int n;
        std::cout << "Enter number of arrays: ";
        std::cin >> n;
        double** array = new double*[n];
    
        for (int i = 0; i < n; ++i)
        {
            unsigned int size;
            std::cout << "Enter size of array " << i << ": ";
            std::cin >> size;
            array[i] = new double[size];
            for (int j = 0; j < size; ++j)
            {
                int element;
                std::cout << "Enter element " << j << " of array " << i << ": ";
                std::cin >> element;
                array[i][j] = element;
            }
        }
    
        for (int i = 0; i < n; ++i)
        {
            delete [] array[i];
        }
        delete[] array;
        return 0;
    }
    

    解决方案 2:

    但是,使用上面的代码,您将无法访问每个内部数组的元素。除非您记住使用此创建的每个内部数组的大小,否则您可能会访问超出范围的内容。因此,对这段代码的更新将是添加另一个数组,我们称之为sizeOfInnerArrays,其中每个元素i 跟踪内部数组array[i] 的大小。这是更新:

        #include <iostream>
        int main()
        {
            unsigned int n;
            std::cout << "Enter number of arrays: ";
            std::cin >> n;
            double** array = new double*[n];
            unsigned int* sizeOfInnerArrays = new unsigned int[n];
    
            for (int i = 0; i < n; ++i)
            {
                std::cout << "Enter size of array " << i << ": ";
                std::cin >> sizeOfInnerArrays[i];
                array[i] = new double[sizeOfInnerArrays[i]];
                for (int j = 0; j < sizeOfInnerArrays[i]; ++j)
                {
                    int element;
                    std::cout << "Enter element " << j << " of array " << i << ": ";
                    std::cin >> element;
                    array[i][j] = element;
                }
            }
    
            //prints out each array as curly-brace enclosed sets of doubles
            for (int i = 0; i < n; ++i)
            {
                std::cout << "{";
                for (int j = 0; j < sizeOfInnerArrays[i] - 1; ++j)
                {
                    std::cout << array[i][j] << ", ";
                }
                std::cout << array[i][sizeOfInnerArrays[i] - 1] << "}" << std::endl;
            }
    
            // free dynamically allocated memory
            for (int i = 0; i < n; ++i)
            {
                delete [] array[i];
            }
            delete[] array;
            delete[] sizeOfInnerArrays;
    
            return 0;
        }
    

    解决方案 3:

    但是,这太复杂了,所以你最好使用一个容器,比如 vector,正如 Joe 建议的那样,它的数据成员会跟踪它的大小。

    #include <iostream>
    #include <vector>
    
    int main()
    {
        unsigned int n;
        std::cout << "Enter number of vectors: ";
        std::cin >> n;
        std::vector<std::vector<double> > myVec;
        // ^ space between closing angle brackets not needed
        //   if using C++11 conforming compiler
    
        for (int i = 0; i < n; ++i)
        {
            unsigned int size;
            std::cout << "Enter size of vector " << i << ": ";
            std::cin >> size;
            std::vector<double> temp;
            temp.reserve(size);
            for (int j = 0; j < size; ++j)
            {   
                double value;
                std::cout << "Enter next value of vector " << i << ": ";
                std::cin >> value;
                temp.push_back(value);
            }
            myVec.push_back(temp);
        }
    
        for (int i = 0; i < myVec.size(); ++i)
        {
            std::cout << "{";
            for (int j = 0; j < myVec.at(i).size() - 1; ++j)
            {
                std::cout << myVec.at(i).at(j) << ", ";
            }
            std::cout << myVec.at(i).back() << "}" << std::endl;
        }
    
        return 0;
    }
    

    【讨论】:

    • new[] 和 delete[] 都太上世纪了
    • 我首先使用了常规数组,因为它最接近 OP 的示例代码。我进行了编辑以添加额外的解决方案。在第二个解决方案中,我添加了一个数组来跟踪其他数组的大小,以便检索边界内的元素。这为在第三种解决方案中使用向量提供了动力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2013-12-14
    • 2012-01-05
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2015-07-25
    相关资源
    最近更新 更多