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 ";"
?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With