简体   繁体   中英

Splitting std::wstring into std::vector

This question shows how to split a string into a vector using a single character delimeter.

Question: Right way to split an std::string into a vector

However, applying this technique to wstring isn't as easy as I thought. Therefore this is definitely not a duplicate at all!

wstringstream stringStream(str);
istream_iterator<wstring> begin(stringStream);
istream_iterator<wstring> end;
List = vector<wstring>(begin, end);
copy(List.begin(), List.end(), ostream_iterator<wstring>(cout, ";"));

The second line can't be compiled using VS2015. And using istream_iterator<wstring, wchar_t> causes a compile error in iterator.h .

How can I split a std::wstring into a std::vector that is separated by ";" ?

You are not going to be able to use the method in your example. That method relies on the input being white space seperated. In your question you say your strings a ; separated like "the;quick;brown;fox" . In order to do that you can use std:getline with ';' as the delimiter to break up the string.

std::wstring str = L"the;quick;brown;fox", temp;
std::vector<std::wstring> parts;
std::wstringstream wss(str);
while(std::getline(wss, temp, L';'))
    parts.push_back(temp);

The above loads the string into the stream and then will keep calling std::getline breaking at the ';' 's until it reaches the end of stream.

I would also like to point out that had your second example split the data you would not have seen it as

copy(List.begin(), List.end(), ostream_iterator<wstring>(cout, ";"));

Will put the ';' right back into the string. It also needs std::wcout instead of std::cout as you are dealing with wide characters.

According to cppreference ctype has two different specializations for char and wchar_t and the have different function. You cannot just change all occurances of ctype<char> with ctype<wchar_t> and be done as ctype<wchar_t> is missing functions that the second example uses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM