在 c++ 中初始化 const 多维数组

发布于 2024-08-09 12:14:54 字数 459 浏览 3 评论 0原文

我目前正在一本 C++ 书中做一些练习,该书使用基于文本的游戏作为教学工具。我所坚持的练习包括让电脑从常量单词(字符串)数组中选择一个单词,将字母混合起来并要求玩家猜测该单词。这很简单,但是作为本书的后续要求添加选项来向玩家提供提示以帮助他们猜测,首先作为并行数组(同样,没问题),然后作为二维数组。这就是我被困住的地方。我的(缩短的)单词数组如下:

const string WORDS[NUM_WORDS] = {"wall", "glasses"};

我需要为每个单词提供提示,但不确定如何进行。从手机上发布此信息如此广泛的谷歌搜索是不可能的!

我的并行数组如下:

const string HINTS[NUM_WORDS] = "brick...", "worn on head"};

只需将两者结合起来即可。

提前致谢, 巴里

I'm currently working through some exercises in a c++ book, which uses text based games as its teaching tool. The exercise I am stuck on involves getting the pc to select a word from a const array of words (strings), mixing the letters up and asking the player to guess the word. This was easy, but as a follow on the book asks to add the option to provide a hint to the player to help them guess, firstly as a parallel array (again, no problem) and then as a 2 dimensional array. This is where I'm stuck. My (shortened) array of words is as follows:

const string WORDS[NUM_WORDS] = {"wall", "glasses"};

I need to provide a hint for each of these words but not sure how to go about it. Posting this from phone so extensive googling is not an option!

My parallel array was as follows:

const string HINTS[NUM_WORDS] = "brick...", "worn on head"};

Just need to combine the two.

Thanks in advance,
Barry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枯寂 2024-08-16 12:14:54

你可以制作二维数组吗?

string aArray[][2] = {{"wall", "brick..."}, 
                               {"glasses", "corrective eye tool thingymajig"},
                               {"calculator", "number cruncher"}};

不确定语法是否正确,但我希望你明白这个概念。

它是另一个数组中的一个数组。

编辑:我从第一个方括号中删除了 NUM_WORDS 。你不能从变量声明多维数组..抱歉我忘了这一点。我刚刚测试了它并且它有效。

you could make 2d array?

string aArray[][2] = {{"wall", "brick..."}, 
                               {"glasses", "corrective eye tool thingymajig"},
                               {"calculator", "number cruncher"}};

not sure if the syntax is right but i hope you get the concept.

it's an array inside another array.

EDITED: i removed the NUM_WORDS from the first square bracket. you can't declare multi dimentional array from variables.. sorry i forgot about that. i just tested it and it works.

背叛残局 2024-08-16 12:14:54

使用结构数组:

typedef struct
{
    string Word;
    string Hint;
}
WORDDEF;

const WORDDEF WordList[] =
{
    {"wall", "brick..."},
    {"glasses", "worn on head"},
    ...
};

然后将它们称为:

printf ("For entry %d, Word is %s, Hint is %s\n", 1, WordList[1].Word, WordList[1].Hint);

Use a struct array:

typedef struct
{
    string Word;
    string Hint;
}
WORDDEF;

const WORDDEF WordList[] =
{
    {"wall", "brick..."},
    {"glasses", "worn on head"},
    ...
};

Then you refer to them as:

printf ("For entry %d, Word is %s, Hint is %s\n", 1, WordList[1].Word, WordList[1].Hint);
你好,陌生人 2024-08-16 12:14:54

呵呵...
也许你应该使用pair结构?

std::pair<std::string, std::string> WORDS[10] = { make_pair("wall", "brick"), 
                                                  make_pair("glasses, worn") }

现在,您可以使用 WORDS[i].first 作为未知单词

,并使用 WORDS[i].second 作为提示。

huh...
maybe you should use pair struct?

std::pair<std::string, std::string> WORDS[10] = { make_pair("wall", "brick"), 
                                                  make_pair("glasses, worn") }

now, you can use WORDS[i].first -- as the unknown word

and WORDS[i].second as its hint.

眼波传意 2024-08-16 12:14:54

这是一个简单的示例,它将从您已经拥有的其他两个数组中为您提供一个二维数组 [2][2] ,您可以将其用作起点。

#include "stdafx.h"
#include "iostream"
#include "string"

#define NUM_WORDS 2
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    const string WORDS[NUM_WORDS] = {"wall", "glasses"};
    const string HINTS[NUM_WORDS] = {"brick", "worn on head"};

    string hw[NUM_WORDS][NUM_WORDS];

    for(int i=0; i < NUM_WORDS; i++)
        for(int j=0; j < NUM_WORDS; j++)
        {
            if(i > 0)
                hw[i][j] = HINTS[j];
            else if(j > 0)
                hw[i][j] = WORDS[i+1];
            else
                hw[i][j] = WORDS[i];
        }
    for(int i=0; i < NUM_WORDS; i++)
        for(int j=0; j < NUM_WORDS; j++)
        {
            cout << "hw[" << i << "][" << j << "]= " << hw[i][j] << endl;
        }

    char c;
    cin.get(c);
    return 0;
}

由于某种原因,无论我做什么,该框都不允许我复制并粘贴代码以使其格式正确。对不起。

Here is a simple example That will give you a 2 dimensional array[2][2] from the other two arrays that you already have that you can use as a starting point.

#include "stdafx.h"
#include "iostream"
#include "string"

#define NUM_WORDS 2
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    const string WORDS[NUM_WORDS] = {"wall", "glasses"};
    const string HINTS[NUM_WORDS] = {"brick", "worn on head"};

    string hw[NUM_WORDS][NUM_WORDS];

    for(int i=0; i < NUM_WORDS; i++)
        for(int j=0; j < NUM_WORDS; j++)
        {
            if(i > 0)
                hw[i][j] = HINTS[j];
            else if(j > 0)
                hw[i][j] = WORDS[i+1];
            else
                hw[i][j] = WORDS[i];
        }
    for(int i=0; i < NUM_WORDS; i++)
        for(int j=0; j < NUM_WORDS; j++)
        {
            cout << "hw[" << i << "][" << j << "]= " << hw[i][j] << endl;
        }

    char c;
    cin.get(c);
    return 0;
}

For some reason no matter what I do the box will not let me copy and paste the code in so that it formats correctly. sorry.

百变从容 2024-08-16 12:14:54

尽管本书告诉您使用 2 个数组,但更好的方法是使用结构数组,以便您可以通过名称而不是整数索引访问单词和提示。临时结构也比成对结构更好,因为您可以命名成员。 C++ 中的另一个有用技巧是让编译器使用 sizeof 运算符计算数组中的元素数量。这减少了代码中的冗余,从而减少了出现错误的机会。

int main(){
  /////////////version 1 - the way I would do it
  struct element{
    string word;
    string hint;
  };

  const element array[] = {
    {"wall", "brick..."},
    {"glasses", "corrective eye tool thingymajig"},
    {"calculator", "number cruncher"}
  };    
  const size_t NUM_WORDS = sizeof(array)/sizeof(element);

  for(size_t i=0; i<NUM_WORDS; i++){
    cout << array[i].word << ": " << array[i].hint << endl;
  }

  /////////////version 2 - the way the book asks for it
  const string array2[][2] = {
    {"wall", "brick..."},
    {"glasses", "corrective eye tool thingymajig"},
    {"calculator", "number cruncher"}
  };    
  const size_t NUM_WORDS2 = sizeof(array2)/(sizeof(string)*2);

  for(size_t i=0; i<NUM_WORDS2; i++){
    cout << array2[i][0] << ": " << array2[0][1] << endl;
  }    
}

Though the book tells you to use 2 arrays, the better way to do this would be an array of structures so that you can access the words and hints by name rather than by an integer index. An ad hoc structure is also better than a pair since you can name the members. Another useful trick in C++ is to have the compiler calculate the number of elements in the array using the sizeof operator. This reduces redundancy in the code which reduces the chances of bugs.

int main(){
  /////////////version 1 - the way I would do it
  struct element{
    string word;
    string hint;
  };

  const element array[] = {
    {"wall", "brick..."},
    {"glasses", "corrective eye tool thingymajig"},
    {"calculator", "number cruncher"}
  };    
  const size_t NUM_WORDS = sizeof(array)/sizeof(element);

  for(size_t i=0; i<NUM_WORDS; i++){
    cout << array[i].word << ": " << array[i].hint << endl;
  }

  /////////////version 2 - the way the book asks for it
  const string array2[][2] = {
    {"wall", "brick..."},
    {"glasses", "corrective eye tool thingymajig"},
    {"calculator", "number cruncher"}
  };    
  const size_t NUM_WORDS2 = sizeof(array2)/(sizeof(string)*2);

  for(size_t i=0; i<NUM_WORDS2; i++){
    cout << array2[i][0] << ": " << array2[0][1] << endl;
  }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文