11

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 ";"?

Andreas
  • 5,393
  • 9
  • 44
  • 53
bytecode77
  • 14,163
  • 30
  • 110
  • 141

1 Answers1

18

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

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • This method works on `std::string` using ";" though. So it working only with a white space is incorrect. But I will try your example right now. – bytecode77 Apr 23 '16 at 15:02
  • @bytecode77 The code you show in your example is not using the second method that can split based on other characters. If you are using that version then you should update your example. – NathanOliver Apr 23 '16 at 15:04
  • Might be worth an edit to beat home that in `ostream_iterator(cout, ";")` in OP's sample, all that ';' is doing is putting a ';' after each string when outputting the vector. And that since output takes place after the string is split , it can't have any effect on the splitting. – user4581301 Apr 23 '16 at 15:16
  • @bytecode77 No problem. I just added a blurb to my answer why the second example in the linked to question will not work with `wchar_t`. You may want to as the answerer if he knows of a way to do this with wide characters and strings. – NathanOliver Apr 23 '16 at 15:26
  • One more thing: Could it be that `getline()` adds something to the end of the string, like '\n' or '\0'? I've noticed that further code processes this string incorrectly. – bytecode77 Apr 23 '16 at 16:11
  • @bytecode77 `getline` doesn't add anything to the stream. it reads in a string until it reaches the delimiter or EOF. When it encounters the delimiter it reads it in and then discards it. When it reaches the EOF it sets the EOF flag. The obove code should handle and `;` seperated string and even cases like `text;;;;text;`. That would produce 5 strings. 2 that contain "text" and 3 empty strings. – NathanOliver Apr 23 '16 at 16:21
  • You're right, I'm sorry. There was an error elsewhere in my application. C++ is great... until you have to deal with strings, then C# is **far** superior. But for some tasks you can't use .NET. Well... – bytecode77 Apr 23 '16 at 17:10
  • @bytecode77 Yeah C++ doesn't do strings as well as some of the higher level languages. – NathanOliver Apr 23 '16 at 17:28