My split function doesnt work, error C2664,

110 Views Asked by At

So this is supposed to split string and turn it into list:

std::list<std::string> strSplit(std::string::iterator begin, std::string::iterator end, char c) {
    while(std::find(begin, end, c) != end) *std::find(begin, end, c) = '\0';
    return std::list<std::string>(begin, end);
}

But compiler throws this error:

Error: C2664    
'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string
(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)':
 cannot convert argument 1 from 'char' to 'std::initializer_list<_Elem>'

And double click redirects to xmemory0 line: 881

...
template<class _Objty,
class... _Types>
static void construct(_Alloc&, _Objty * const _Ptr, _Types&&... _Args)
{   // construct _Objty(_Types...) at _Ptr
::new (const_cast<void *>(static_cast<const volatile void *>(_Ptr)))
    _Objty(_STD forward<_Types>(_Args)...);
}
...
2

There are 2 best solutions below

0
Ted Lyngmo On

You need to use the returned iterator you get from std::find and actually create a std::string from it (and the begin iterator). Replacing c with \0 would only make the original string have \0 in it and so would the one and only string in the resulting std::list<std::string> also look like.

On that topic - you should probably use std::string::const_iterator for this since you probably shouldn't make changes to the original string.

Example:

#include <iterator>

std::list<std::string> strSplit(std::string::const_iterator begin,
                                std::string::const_iterator end, char c) {
    std::list<std::string> retval;
    if(begin == end) return retval; // nothing to do here

    while(true) {
        auto fit = std::find(begin, end, c);
        retval.emplace_back(begin, fit); // creates a string in-place using begin and fit
        if(fit == end) return retval;    // if end was found, return what we got        
        begin = std::next(fit);          // else, go another round
    }
}
0
Aykhan Hagverdili On

Your function has multiple issues. Besides not compiling. It tries to mutate the string it's trying to copy into a list anyway, which doesn't make sense. It also uses std::find multiple times for no reason instead of storing its result and reusing it. Here's my version:

std::list<std::string> strSplit(std::string::const_iterator const begin,
                                std::string::const_iterator const end,
                                char const c) {
  std::list<std::string> res;
  if (end != begin) {
    auto it = begin, prev = begin;
    while ((it = std::find(it, end, c)) != end) {
      res.emplace_back(prev, it);
      prev = ++it;
    }
    res.emplace_back(prev, end);
  }

  return res;
}