开发者

how can I use L prefix to unquoted string? [duplicate]

开发者 https://www.devze.com 2023-03-29 17:59 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Compare std::wstring and std::string
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Compare std::wstring and std::string

I have silly question. I know I can use L prefix before a string to use it as wchar_t* (for unicode strings) but I dont know how to use this prefix before variable. I mean

std::wstring str = L"hello";

I know the code above, but how about this one:

string somefunction();

std::wstring str1 = L(somfunction()) 

this say that 'L' identifier not found

the problem is how to apply L prefix to unquoted string?

void wordNet::extractWordIds(wstring targetWord)
{

    pugi::xml_document doc;
    std::ifstream stream("words0.xml");
    pugi::xml_parse_result result = doc.load(stream);
    pugi::xml_node words = doc.child("Words");

    for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
    {       
        std::string wordValue =  as_utf8(it->child("WORDVALUE").child_value());
        std::wstring result (wordValue.size (), L' ');
        std::copy (wordValue.begin (), wordValue.end (), result.begin ()); 
        if(!result.compare(targetWord))
            cout << "found!" << endl; 
    }


}

actully I want to compare targetWord with wordValue. you see that I convert wordValue to wstring but still dont get the right resul开发者_如何学运维t by comparision.


You cannot, it's a part of the string-literal itself. It's not an operator.

string-literal:
    encoding-prefixopt "s-char-sequenceopt"
    encoding-prefixoptR raw-string

encoding-prefix:
    u8
    u
    U
    L

Also I recommend you to avoid using std::wstrings, unless you make a low-level windows API call.

EDIT:

If you compiled pugixml with PUGIXML_WCHAR_MODE use:

    if(it->child("WORDVALUE").child_value() == targetWord)
        cout << "found!" << endl; 

Otherwise use:

    if(it->child("WORDVALUE").child_value() == pugi::as_utf8(targetWord))
        cout << "found!" << endl; 

I recommend compiling without PUGIXML_WCHAR_MODE and changing the function to:

void wordNet::extractWordIds(std::string targetWord)
{
    // ...
    for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
        if(it->child("WORDVALUE").child_value() == targetWord)
            cout << "found!" << endl; 
}

And let the caller worry about passing a UTF-8 targetWord.


You have to make somfunction return either a std::wstring or a wchar_t*.

If you cannot change the function return type, you'll need a conversion from string to wstring which is not something that can be done at compile time - you'll need to call a function to do it. The question has been asked many times with many different variations, here's one example: C++ Convert string (or char*) to wstring (or wchar_t*)


You can't.

You should copy the result of the string in the wstring, for instance:

std::string tmp = somefunction ();
std::wstring result (tmp.size (), L' ');
std::copy (tmp.begin (), tmp.end (), result.begin ());

From pugixml documentation:

There are cases when you'll have to convert string data between UTF-8 and wchar_t encodings; the following helper functions are provided for such purposes:

std::string as_utf8(const wchar_t* str);
std::wstring as_wide(const char* str);
0

精彩评论

暂无评论...
验证码 换一张
取 消