【问题标题】:Looping over values in dynamically allocated arrays in c++ [duplicate]在c ++中循环动态分配数组中的值[重复]
【发布时间】:2020-08-06 00:20:04
【问题描述】:

我来自 python,现在我正在尝试学习 c++,我正在尝试在 c++ 中打印一些看起来像 python 的 list = [1, 2, 3] 的东西。我让用户选择数组的大小并在其中插入一个数字,然后我想cout以上述方式得到结果,但我无法循环array中的值,因为变量指向pointer 不是 array 本身。有人可以解释一下解决方法吗?

int main() {


    int i, n;
    cout << "How many values?";
    cin >> i;

    int * foo;
    foo = new (nothrow) int [i];

    if (foo == nullptr) {
        cout << " Could Not allocate so much memory";
    }
    else {
        for (n=0; n<i; n++) {
            cout << "Enter Number: " << endl;
            cin >> foo[n];
        }
    }

    
    cout << "[";
    for (int *n: foo){
        cout << n << ", ";
    }
    cout << "]" << endl;
    return 0;
}

错误信息

error: ‘begin’ was not declared in this scope; did you mean ‘std::begin’?
   31 |     for (int *n: foo){
      |                  ^~~
      |                  std::begin
In file included from /usr/include/c++/9/string:54,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from study.cpp:1:
/usr/include/c++/9/bits/range_access.h:105:37: note: ‘std::begin’ declared here
  105 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
      |                                     ^~~~~
study.cpp:31:18: error: ‘end’ was not declared in this scope; did you mean ‘std::end’?
   31 |     for (int *n: foo){
      |                  ^~~
      |                  std::end
In file included from /usr/include/c++/9/string:54,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from study.cpp:1:
/usr/include/c++/9/bits/range_access.h:107:37: note: ‘std::end’ declared here
  107 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
      |  

【问题讨论】:

  • 谢谢,当我想使用 C 风格的数组时,这看起来最合适,但是我很难理解重载>。
  • 只需使用常规的for 循环,就像填充数组时一样,例如:for (n=0; n&lt;i; n++) { cout &lt;&lt; foo[n] &lt;&lt; ", "; }

标签: c++ arrays


【解决方案1】:

如果您使用 std::vector 而不是 C 样式的数组,则可以使用 range-based for loop 遍历它,如下所示:

#include <vector>
#include <iostream>

int main() {
    int i;
    std::cout << "How many values?";
    std::cin >> i;

    std::vector <int> v;

    for (int n = 0; n < i; n++) {
        std::cout << "Enter number: " << std::endl;
        int x;
        std::cin >> x;
        v.push_back (x);
    }
    
    std::cout << "[";
    for (auto e : v)
        std::cout << e << ", ";
    std::cout << "]" << std::endl;
}

【讨论】:

  • 是的,忘记 C 风格的数组(至少对于动态数组)。
  • @PaulSanders 即使​​对于固定大小的数组,您也应该忘记 C 样式的数组并改用 C++ std::array
【解决方案2】:

new c++11 for loop causes: "error: ‘begin’ was not declared in this scope"

我相信这个问题的答案就是您正在寻找的。​​p>

【讨论】:

    猜你喜欢
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2021-05-14
    • 2011-07-04
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多