繁体   English   中英

打印2D std :: string数组

[英]Printing a 2D std::string array

我试图用c ++初始化一个tic-tac-toe板,但输出总是给我六进制值。 有没有办法将它们转换为实际的字符串值?

#include <iostream>
#include<string>

using namespace std;

int main()
{
    string tab[5][5] = { "1","|","2","|","3",
                         "-","+","-","+","-",
                         "4","|","5","|","6",
                         "-","+","-","+","-",
                         "7","|","8","|","9" };

    for(int i = 0; i <= 24; i++)
    {
        cout << tab[i] << endl;
    }
}

你将tab[i]的值发送到cout ,所以你得到了内存地址。

您可能希望更深层次地嵌套项目,例如tab[i][j]

tab[i]是一个std::string[] - 也就是std::string的数组,而不是std::string本身。

使用ranged- for ,而不是你的输出。 与标准库容器一样,它与内置数组一起使用:

for (const auto &row : tab) {
  for (const auto &c : row) {
    cout << c;
  }
  cout << endl;
}

当然,在输出位置提出循环的答案是正确的。 如果您碰巧要在应用程序的许多不同位置输出您的tic tac toe字段,您可能更愿意将其封装起来。 一个可能的解决方案是有一个tic tac toe类:

struct TicTacToe : public std::array<std::array<int, 3>, 3> {
    TicTacToe() :
        // not sure, if the initialization will work like that
        std::array<std::array<int, 3>, 3>{{0,0,0},{0,0,0},{0,0,0}}
    {};
};

然后为它定义一个输出运算符:

auto operator << (std::ostream& out, const TicTacToe& field)
    -> std::ostream& {
  return
      out << std::accumulate(std::begin(field),
                             std::end(field),
                             std::string{},
                             [](const std::string& a,
                                const std::array<int, 3> b)
                                 -> std::string {
                               return 
                                   std::accumulate(std::begin(b),
                                                   std::end(b),
                                                   std::string{},
                                                   [](const std::string& a, int b)
                                                       -> std::string {
                                                     return std::string{b < 0 ?
                                                                          "O" :
                                                                          (b > 0 ? "X" : " ")} +
                                                            (a.empty() ? "" : "|")
                                                   }) +
                                   (a.empty() ? "" : "\n-+-+-\n");
                             });

}

请注意,我还没有测试过这段代码。 它旨在为您提供一个想法,而不是复制粘贴的来源。

这更像是非常感谢!

#include <iostream>
#include<string>
using namespace std;

int main()
{
    string tab[5][5] = { "1","|","2","|","3",
                         "-","+","-","+","-",
                         "4","|","5","|","6",
                         "-","+","-","+","-",
                         "7","|","8","|","9" };

    for(int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            cout << tab[i][j];

            if (j == 4)
            cout << endl;
        }   
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM