c++stringc++11irrlicht

Cannot convert from stringw to string / char


I have been trying to convert from a irr::stringw (irrlicht game engine core string) so that I can simply explode the string at a delimiter to create an array / list.

However, I cannot convert from stringw, which is not compatible with any normal functions.

So, to split the stringw into a list I have the following:

vector<stringw> split(stringw str, char delimiter){
    vector<stringw> internal;
    std::string tok;
    std::string std_s(str.c_str(), str.size());

    while (getline(std_s, tok, delimiter)) {
        internal.push_back(tok);
    }
    return internal;
}

However errors here are: str.c_str() says No instance of constructor "std::basic::string matches argument list. Argument types are (const wchar_t*, irr:u32)

On the getline method the error: No instance of overloaded function "getline" matches the argument list. Argument types are: (std::string, std::string, char).

I have been trying for hours a number of ways to just split a stringw at a delimiter of ";" (semi colon) but nothing is working. Any advice?


Solution

  • There are two main issues here, the first is that stringw is a string of wchar_t characters. So we need to use the std::wstring facilities to avoid having to convert wchar_t to/from char.

    We also need to note that std::getline requires a type derived from std::basic_istream as it's first argument. We can therefore modify your code as follows:

    vector<stringw> split( stringw str, wchar_t delimiter )
    {
        vector<stringw> internal;
        std::wstring tok;
        std::wistringstream std_s( str.c_str() );
    
        while( getline(std_s, tok, delimiter) ) {
            internal.push_back(tok);
        }
        return internal;
    }