创建相同字符的多维数组的最有效方法? [C++]

Most efficient way to create multidimensional array of same chars? [C++]

抱歉,如果此问题已在其他地方得到解答。我很新,不知道如何真正解释这样的问题。

现在,我正在寻求创建一个包含所有“+”字符的 [5]x[5] 数组。这是我拥有的:

#include <iostream>
using namespace std;

int main() {
char map[5][5] = {{'+','+','+','+','+'},{'+','+','+','+','+'},{'+','+','+','+','+'},{'+','+','+','+','+'},{'+','+','+','+','+'}};

for (int x = 0; x < 5; x++) {
    for(int y = 0; y < 5; y++) 
        cout << map[x][y] << " ";
    cout << endl;
}

return 0;
}

有没有一种方法可以重复这些“+”字符,而不必一遍又一遍地列出每个字符?

谢谢:)

在漫长的 运行 中,我希望创建一个 [n]x[n] 地图,玩家可以在其中四处走动并作为一个有趣的学习项目进行互动。

嗯,这很简单:只需做一个简单的循环即可:

char map[5][5];
for (int x = 0; x < 5; x++) {
    for(int y = 0; y < 5; y++) 
        map[x][y]='+';
}

如果你想要尽可能少的代码,怎么样:

#include <string.h>
memset(&map[0][0], 'x', sizeof(map));

std::vector 的构造函数提供了一种轻松构造对象的简单方法:

#include <vector>
//...
std::size_t n = 5;
std::vector<std::vector<char>> map(std::vector<char>('+', n), n);
//If using Visual Studio 2012 (or equivalent) or earlier:
std::vector<std::vector<char> > map(std::vector<char>('+', n), n);