Why can’t the terminal display Chinese normally in CPP?

  Kiến thức lập trình

This my code:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    std::locale::global(std::locale("en_US.UTF-8"));
    vector<string> msg{"Hello", "C++", "发多少", "from", "VS Code", "and the C++ extension!"};

    for (const string &word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

When I have std::locale::global(std::locale("en_US.UTF-8")); It will display normally otherwise it will be garbled

When I added std::locale::global(std::locale("en_US.UTF-8"));, it shows right in vscode, otherwise no, even I have already used utf-8 encoding.

LEAVE A COMMENT