C++ Non ASCII letters

前端 未结 2 1464
無奈伤痛
無奈伤痛 2021-01-26 11:40

How do i loop through the letters of a string when it has non ASCII charaters? This works on Windows!

for (int i = 0; i < text.length(); i++)
{
    std::cout          


        
2条回答
  •  余生分开走
    2021-01-26 12:19

    Try using std::wstring. The encoding used isn't supported by the standard as far as I know, so I wouldn't save these contents to a file without a library that handles a specific format. of some sort. It supports multi-byte characters so you can use letters and symbols not supported by ASCII.

    #include 
    #include 
    
    int main()
    {
        std::wstring text = L"áéíóú";
    
        for (int i = 0; i < text.length(); i++)
            std::wcout << text[i];
    
        std::wcout << text.length() << std::endl;
    }
    

提交回复
热议问题