Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 398
bytecode77 Avatar asked Apr 23 '16 14:04

bytecode77


People also ask

How to split a string into a vector in C++?

This post will discuss how to split a string into a vector in C++. 1. Using String Stream A simple solution to split a space-separated std::string into a std::vector<string> is using string streams. This can be implemented as follows in C++. To split a string using a delimiter, we can invoke the getline () function with delimiter: 2.

How to split a string into a range of substrings?

To address this, std::split () will move ownership of rvalues into the Range object that is returned from std::split (). The function called to split an input string into a range of substrings. text — a std::string_view referring to the input string to be split. Delimiter — an object that implements the Delimiter concept.

How does the STD::split() algorithm work?

The std::split () algorithm takes a std::string_view and a Delimiter as arguments, and it returns a Range of std::string_view objects as output. The std::string_view objects in the returned Range will refer to substrings of the input text. The Delimiter object defines the boundaries between the returned substrings.

Does wchar_t have any effect on string splitting?

And that since output takes place after the string is split , it can't have any effect on the splitting. @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.


1 Answers

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

like image 135
NathanOliver Avatar answered Oct 18 '22 19:10

NathanOliver