Wstring :: find () doesn't work with non-latin characters?

I have a wide character string (std :: wstring) in my code, and I need to look for a wide character in it.

I use the find () function for it:

    wcin >> str;
    wcout << ((str.find(L'') != wstring::npos)? L"EXIST":L"NONE");

L'' is a cyrillic letter.

But find () in the same call always returns npos. In the case of Latin letters, find () works correctly.

Is this a problem with this feature? Or did I do something wrong?

UPD

I use MinGW and save the source code in UTF-8. I also set the locale to setlocale(LC_ALL, "");. The same code wcout << L'';works together. But the same

wchar_t w;
wcin >> w;
wcout << w;

it works incorrectly.

This is strange. I had no coding problems before using setlocale ().

+5
source share
4 answers

. ++ . , :

std::wcout << std::hex << L"";

++ 11 ASCII , :

"\x05" "five"

++ 11 Unicode,

L"\u03A6"

++ 11 ( , UTF- *), char, char16_t char32_t :

const char* phi_utf8 = "\u03A6";
const char16_t* phi_utf16 = u"\u03A6";
const char32_t* phi_utf16 = U"\u03A6";
+3

.

:

#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>

using namespace std;

int main()
{       
    _setmode(_fileno(stdout), _O_U16TEXT);
    _setmode(_fileno(stdin), _O_U16TEXT);
    wstring str;
    wcin >> str;
    wcout << ((str.find(L'') != wstring::npos)? L"EXIST":L"NONE");
    system("pause");
    return 0;
}
+1

std::wstring::find() . .

Windows ( Unicode ReadConsoleW() Win32 API):

#include <exception>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <windows.h>
using namespace std;

class Win32Error : public runtime_error
{
public:
    Win32Error(const char* message, DWORD error)
        : runtime_error(message)
        , m_error(error)
    {}

    DWORD Error() const
    {
        return m_error;
    }

private:
    DWORD m_error;
};

void ThrowLastWin32(const char* message)
{
    const DWORD error = GetLastError();
    throw Win32Error(message, error);
}

void Test()
{
    const HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    if (hStdIn == INVALID_HANDLE_VALUE)
        ThrowLastWin32("GetStdHandle failed.");

    static const int kBufferLen = 200;
    wchar_t buffer[kBufferLen];
    DWORD numRead = 0;

    if (! ReadConsoleW(hStdIn, buffer, kBufferLen, &numRead, nullptr))
        ThrowLastWin32("ReadConsoleW failed.");

    const wstring str(buffer, numRead - 2);

    static const wchar_t kEf = 0x0444;
    wcout << ((str.find(kEf) != wstring::npos) ? L"EXIST" : L"NONE");
}

int main()
{
    static const int kExitOk = 0;
    static const int kExitError = 1;

    try
    {
        Test();
        return kExitOk;
    }    
    catch(const Win32Error& e)
    {
        cerr << "\n*** ERROR: " << e.what() << '\n';
        cerr << "    (GetLastError returned " << e.Error() << ")\n";
        return kExitError;
    }
    catch(const exception& e)
    {
        cerr << "\n*** ERROR: " << e.what() << '\n';
        return kExitError;
    }        
}

:

C:\TEMP>test.exe
abc
NONE
C:\TEMP>test.exe
abcabc
EXIST
+1

, . wcin , / . /wcin - . wcout - .

, , chchp , SetConsoleCP() () SetConsoleOutputCP() ().

/. , /. MSVC, : fooobar.com/questions/96568/...

0

All Articles