Search code examples
c++arraysstringstring-formatting

Is there a Python fstring or string formatting equivalent in C++?


I'm working on a project in C++ and I needed to make a string that has elements of an array in it. I know in python you have things like sting formatting and fstrings, but I don't know if C++ has any equivalent. I have no earthly idea as to whether or not that's a thing, so I figured this is the best place to ask. I'm making a tic-tac-toe game and I have the board made and I have the positions on the board made. All I'm trying to do is optimize the board so that I can call it from one function in another function and have the parent function return the board so I can work with it. My basic idea for how to do this was to take the board and turn it all into one big string with a bunch of newlines in it and the array elements in it. I also made it in a function so I can just call it wherever I need it and just have it there. Here is the board function I made:

void board(){
char board_pos[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
cout << "     |     |     " << endl;
cout << "  " << board_pos[0][0] << "  |  " << board_pos[0][1] << "  |  " << board_pos[0][2] << "  " << endl;
cout << "     |     |     " << endl;
cout << "-----------------" << endl;
cout << "     |     |     " << endl;
cout << "  " << board_pos[1][0] << "  |  " << board_pos[1][1] << "  |  " << board_pos[1][2] << "  " << endl;
cout << "     |     |     " << endl;
cout << "-----------------" << endl;
cout << "     |     |     " << endl;
cout << "  " << board_pos[2][0] << "  |  " << board_pos[2][1] << "  |  " << board_pos[2][2] << "  " << endl;
cout << "     |     |     " << endl;
}

Edit: I got it figured out thanks to the help of you guys, I really appreciate it. :)


Solution

  • I would just return the type that you use to hold the board. In your case you started with char[3][3].

    I would write that using the C++11 array:

    using Row   = std::array<char, 3>;
    using Board = std::array<Row, 3>;
    

    Now you can make all kinds of functions:

    void move(char player, Board const& b, int row, int col);
    bool is_game_over(Board const&);
    void print(Board const& b);
    

    Etc.

    Your print function could be:

    void print(Board const& b)
    {
        std::cout << "     |     |     \n";
        auto print_row = [](Row const& row) {
            std::cout << "  " << row[0] << "  |  " << row[1] << "  |  " << row[2]
                      << "  \n";
        };
        print_row(b[0]);
        std::cout << "     |     |     \n";
        std::cout << "-----------------\n";
        std::cout << "     |     |     \n";
        print_row(b[1]);
        std::cout << "     |     |     \n";
        std::cout << "-----------------\n";
        std::cout << "     |     |     \n";
        print_row(b[2]);
        std::cout << "     |     |     \n";
    }
    

    See it Live

    Prints

         |     |
      1  |  2  |  3
         |     |
    -----------------
         |     |
      4  |  5  |  6
         |     |
    -----------------
         |     |
      7  |  8  |  9
         |     |