Hello,
I get a char array (out of another component) like this
abcd*xyz*mnop*
* is a seperator nullterm char. How can I seperate the char array
at the * and push back them to a std;;vector<std::string> ?
Thanks
There is a C function called strtok() that does this.
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
I have tried this with the delimiter \0 but it does not work. I will
get only the first part "abcd". On my example above I would like to
get a result vector in this way:
vec[0] = "abcd"
vec[1] = "xyz"
vec[2] = "mnop"
Thanks for help
The above is not perfect because if your first char is a '*' it would
create an empty string at v[0], but it may be something you can work
on.
The * should be a placeholder for \0, so my input char array has this
structure abcd\0xyz\0mnop\0 and I will seperate at the \0. In my opinion
I must iterate over all elements of the array to detect the seperators
and cut the array. Can I use a C or C++ function for seperating at \0?
There are not any standard C or C++ functions that can help you; you
will have to write your own.
If you are lazy you can use my tokenizer function(s):
template <typename C, typename FwdIter1, typename FwdIter2>
inline FwdIter1 do_tokens(FwdIter1 aFirst, FwdIter1 aLast, FwdIter2
aDelimeterFirst, FwdIter2 aDelimiterLast, C& aTokens, std::size_t
aMaxTokens = 0, bool aSkipEmptyTokens = true, bool
aDelimeterIsSubsequence = false)
{
if (aFirst >= aLast)
return aFirst;
typedef typename C::value_type value_type;
FwdIter1 b = aFirst;
FwdIter1 e = aDelimeterIsSubsequence ? std::search(b, aLast,
aDelimeterFirst, aDelimiterLast) : std::find_first_of(b, aLast,
aDelimeterFirst, aDelimiterLast);
std::size_t tokens = 0;
while(e != aLast && (aMaxTokens == 0 || tokens < aMaxTokens))
{
if (b == e && !aSkipEmptyTokens)
{
aTokens.push_back(value_type(b, b));
++tokens;
}
else if (b != e)
{
aTokens.push_back(value_type(b, e));
++tokens;
}
b = e;
for (std::size_t i = aDelimeterIsSubsequence ?
std::distance(aDelimeterFirst, aDelimiterLast) : 1; i > 0; --i)
++b;
e = aDelimeterIsSubsequence ? std::search(b, aLast, aDelimeterFirst,
aDelimiterLast) : std::find_first_of(b, aLast, aDelimeterFirst,
aDelimiterLast);
}
if (b != e && (aMaxTokens == 0 || tokens < aMaxTokens))
{
aTokens.push_back(value_type(b, e));
b = e;
}
return b;
}
template <typename C, typename FwdIter1, typename FwdIter2>
inline FwdIter1 tokens(FwdIter1 aFirst, FwdIter1 aLast, FwdIter2
aDelimeterFirst, FwdIter2 aDelimiterLast, C& aTokens, std::size_t
aMaxTokens = 0, bool aSkipEmptyTokens = true, bool
aDelimeterIsSubsequence = false)
{
return do_tokens(aFirst, aLast, aDelimeterFirst, aDelimiterLast,
aTokens, aMaxTokens, aSkipEmptyTokens, aDelimeterIsSubsequence);
}
template <typename CharT, typename Traits, typename Alloc, typename C>
inline void tokens(const std::basic_string<CharT, Traits, Alloc>& aLine,
const std::basic_string<CharT, Traits, Alloc>& aDelimeter, C& aTokens,
std::size_t aMaxTokens = 0, bool aSkipEmptyTokens = true, bool
aDelimeterIsSubsequence = false)
{
do_tokens(aLine.begin(), aLine.end(), aDelimeter.begin(),
aDelimeter.end(), aTokens, aMaxTokens, aSkipEmptyTokens,
aDelimeterIsSubsequence);
}