3

我有这两个数组:

const char *face[] =
{"Deuce", "Three", "Four", "Five",
 "Six", "Seven", "Eight", "Nine", "Ten",
 "Jack", "Queen", "King", "Ace", "\0"};

const char *suit[] = { " of Hearts", " of Clubs", " of Diamonds", " of Spades", "\0" };    

实际上,由于我在 C++ 中什至不是那么好,所以我什至不知道您何时在数组或其他任何地方使用星号......如果有人也能解释一下,我将不胜感激。

但无论如何,问题是我试图用他们的西装打印出所有可能的卡片,如下所示:

for (int n = 0; n<strlen(*suit); n++){ //where strlen(*suit) should be 4
for(int i = 0; i<strlen(*face); i++){ //where strlen(*face) should be 13
        cout << endl << face[i] << suit[n] << endl;
    }
}

使用该代码,我的程序崩溃了。我究竟做错了什么?(它在使用 n<4 和 i<13 时有效,但如果我从数组中添加或删除项目,我希望它实际工作)

4

7 回答 7

2

该函数strlen传递一个const char*指向以空字符结尾的字符数组的指针。您不能使用它来计算字符串数组的长度。

相反,我建议你这样做:

const char *face[] =
    {"Deuce", "Three", "Four", "Five",
     "Six", "Seven", "Eight", "Nine", "Ten",
     "Jack", "Queen", "King", "Ace", NULL};

所以,哨兵是空指针。像这样的循环:

for (int i=0; face[i]; i++)
    // do something with face[i]

当然,对于另一个阵列也是如此。


现在,说了这么多,对于 C++ 程序,你正在以错误的方式进行操作。

  • 而不是使用 C 字符串,指向字符数组的指针,使用std::string.
  • 不要使用原始数组来保存字符串,而是使用标准容器类。在您的情况下,您想要std::vector<std::string>.

我能给你的最好建议是忘记 C 的做事方式,并尝试学习惯用的 C++ 方式来编写代码。

于 2013-04-14T18:32:41.497 回答
1

一些东西!你可以做这个检查:

sizeof(suit)/sizeof(suit[0])

但它会比你需要的运行时间长一倍,因为你确实有一个空终止符。因此,要么从西装中删除空终止字符串并使用上述内容,要么将您的 for 更改为:

for (int n = 0; strlen(suit[n]); n++)

同样在面部阵列上。

于 2013-04-14T18:41:02.457 回答
0

您正在存储指针数组。* in font of a pointer 取消引用该指针并返回字符串。因此,您对 strlen(*suit) 的第一次调用返回 10,这将导致索引超出范围,从而导致程序崩溃。

于 2013-04-14T18:37:27.527 回答
0

数组的长度不是由 给出的strlen,因为它的元素不是char,而是char*。换句话说,您的数组不是字符串。

您需要将数组长度存储在一个单独的变量中,或者只使用std::vector<const char*> face;容器并使用face.size().

于 2013-04-14T18:31:58.793 回答
0

指针数组与 char 数组不同。strlen( *suit )返回数组第一个元素的长度face,它是一个字符串。该索引处的字符串长度为 11(包括终止字符),这就是它只会循环 11 次的原因。同样,内部循环只运行 5 次,因为第一个元素的字符串长度face为 6 个字符。

你应该使用一个向量来std::string代替:

std::vector<std::string> suit{"Deuce", "Three", "Four", "Five",
 "Six", "Seven", "Eight", "Nine", "Ten",
 "Jack", "Queen", "King", "Ace"};


std::vector<std::stirng> face{" of Hearts", " of Clubs", " of Diamonds", " of Spades"};

for (auto a : suit)
{
    for (auto b : face)
    {
        // ...
    }
}
于 2013-04-14T18:32:15.143 回答
0
 const char *face[] =
   {"Deuce", "Three", "Four", "Five",
     "Six", "Seven", "Eight", "Nine", "Ten",
      "Jack", "Queen", "King", "Ace", "\0"};

是一个字符串字面量数组。你最好声明face如下:

string face[13]= {"Deuce", "Three", "Four", "Five",
     "Six", "Seven", "Eight", "Nine", "Ten",
     "Jack", "Queen", "King", "Ace"};

您不再需要“\0”,因为您不处理 c-string(char 数组)。

你可以为suit.

 string suit[4] = { " of Hearts", " of Clubs", " of Diamonds", " of Spades"};

您可以按如下方式打印这两个字符串数组:

  for (int n = 0; n<13; n++){ //where strlen(*face) should be 13
      for(int j = 0; j< 4; j++){ //where strlen(*suit) should be 4
          cout << endl << face[n] << suit[j] << endl;
      }
  }
于 2013-04-14T18:32:33.740 回答
0

你的计数器是错误的

 for (int n = 0; n<4; n++){ 
    for(int i = 0; i<13; i++){ 
        cout << endl << face[i] << suit[n] << endl;
    }
 }

你也可以这样做

 for (int n = 0; strlen(suit[n]) > 0; n++){
    for(int i = 0; strlen(face[i]) > 0; i++){
        cout << endl << face[i] << suit[n] << endl;
    }
 }
于 2013-04-14T18:33:30.373 回答