gpt4 book ai didi

c++ - 使用 forrange 循环打印 argv

转载 作者:行者123 更新时间:2023-12-02 01:14:10 24 4
gpt4 key购买 nike

int main(int argc, const char** argv) {

std::cout << "Hello" << std::endl;

char arr2d[][4] = {"ABC", "DEF"};

for (char *i : arr2d)
{
std::cout << i << std::endl;
}

在这里,我评估 forrange 的工作如下:“对于 arr2d 中的每个字符数组,将其打印到控制台”。这是可行的,所以,至少我的理解应该是正确的。上述代码片段的输出是,

muyustan@mint:~/Desktop/C_Files/oop$ g++ main.cpp -o main && ./main
Hello
ABC
DEF

正如预期的那样。

但是,如果我尝试这个,

int main(int argc, const char** argv) {

std::cout << "Hello" << std::endl;

char arr2d[][4] = {"ABC", "DEF"};

for (const char *i : argv)
{
std::cout << i << std::endl;
}

首先 IDE 警告我,

this range-based 'for' statement requires a suitable "begin" function and none was found

如果我尝试编译,我会得到:

muyustan@mint:~/Desktop/C_Files/oop$ g++ main.cpp -o main && ./main
main.cpp: In function ‘int main(int, const char**)’:
main.cpp:30:26: error: ‘begin’ was not declared in this scope
for (const char *i : argv)
^~~~
main.cpp:30:26: note: suggested alternative:
In file included from /usr/include/c++/7/string:51:0,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from main.cpp:1:
/usr/include/c++/7/bits/range_access.h:105:37: note: ‘std::begin’
template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
^~~~~
main.cpp:30:26: error: ‘end’ was not declared in this scope
for (const char *i : argv)
^~~~
main.cpp:30:26: note: suggested alternative:
In file included from /usr/include/c++/7/string:51:0,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from main.cpp:1:
/usr/include/c++/7/bits/range_access.h:107:37: note: ‘std::end’
template<typename _Tp> const _Tp* end(const valarray<_Tp>&);

那么,为什么 argv 的行为与我的 arr2d[][4] 不同?它们不都是 char 指针(char 数组或字符串(?))的指针吗?

如果我的理解有问题,用 forrange 打印 argv 成分的结构应该是什么?

最佳答案

this is something I have been told wrong then, when I was dealing with C back then, at somewhere I have read that "arrays are also pointers!".

关于该声明,我们必须理解一些更细微的要点。

  1. 在大多数情况下,数组都会退化为指针,但数组仍然与指针不同。

    • 当用作 sizeof 的参数时,以下两个将导致不同的答案。

      char const* ptr = "Some text.";
      char array[] = "some text.";

      std::cout << sizeof(ptr) << std::endl; // prints sizeof the pointer.
      std::cout << sizeof(array) << std::endl; // prints sizeof the array.
    • 当用作 addressof 运算符的参数时。

      char const* ptr1 = "Some text.";
      char array[] = "some text.";

      char const** ptr2 = &ptr1; // OK.
      char** ptr3 = &array; // Error. Type mismatch.
      char (*ptr4}[11] = &array; // OK.
  2. 二维数组可以衰减为指向一维数组的指针,但它们不会衰减为指向指针的指针。

     int array1[10];
    int* ptr1 = array1; // OK. Array decays to a pointer


    int array2[10][20];
    int (*ptr2)[20] = array2; // OK. 2D array decays to a pointer to 1D array.
    int** ptr3 = array2; // Error. 2D array does not decay to a pointer to a pointer.

关于c++ - 使用 forrange 循环打印 argv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61064049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com