0

I am trying to split a string the fastest way possible in C++. I am getting an error here:

#include <bitset>
#include <iostream>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/timer.hpp>

using namespace std;
size_t const N = 10000000;
template<typename C>
void test_strpbrk(string const& s, char const* delims, C& ret)
{
    C output;

    char const* p = s.c_str();
    char const* q = strpbrk(p + 1, delims);
    for (; q != NULL; q = strpbrk(p, delims))
    {
        output.push_back(typename C::value_type(p, q));
        p = q + 1;
    }

    output.swap(ret);
}

int main()
{
    typedef string::const_iterator iter;
    typedef boost::iterator_range<iter> string_view;

    vector<string_view> vsv;
    test_custom(text, delims, vsv);
}

The Visual Studio says: cannot convert from 'const char *' to 'const std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>'

Can you please help me, should I enable any option in visual studio for this to work?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
SkyRipper
  • 155
  • 5
  • 15

1 Answers1

1

It seems you are trying to convert a char const* to a std::string::const_iterator, e.g., when passing arguments to functions creating a range: although std::string::const_iterator may be a typedef for char const* there is no requirement that this is the case. I don't think your code give enough context to pin-point the exact problem, though. You might want to look for locations where you pass a string literal or take the address of a section of a std::string (i.e. something along the lines of &str[index] or &*it).

Based on you edit, what I suspected above actually got confirmed! You'll need to translate the char const*s your are playing with back to std::string::const_iterators, e.g., by adding suitable offsets:

typename C::value_type(s.begin() + (p - s.c_str()), s.begin() + (q - s.c_str()))
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Please see the answer here: http://stackoverflow.com/questions/5505965/fast-string-splitting-with-multiple-delimiters – SkyRipper Dec 07 '13 at 20:47
  • @SkyRipper: why? The referenced question and answers seem unrelated to your problem! _You_ should provide an [SSCCE](http://sscce.org/), though! ... and the update to the article you just made **exactly** confirms what I have guessed: you try to constructor a `std::string::cont_iterator` from a `char const*`! – Dietmar Kühl Dec 07 '13 at 20:59
  • I did what you told me – SkyRipper Dec 07 '13 at 21:06
  • @SkyRipper: You seem behind the time, though: I already updated the answer with the solution (although I'm not in a position to compile it easily to verify that I didn't make a typo; conceptually it will work, though). – Dietmar Kühl Dec 07 '13 at 21:07
  • how to use the typename please? – SkyRipper Dec 07 '13 at 21:08
  • @SkyRipper: You replace the obvious expression where you are currently using it, i.e., it is the argument to `output.push_back(...)` in your updated code (don't expect others to write all your code). – Dietmar Kühl Dec 07 '13 at 21:09
  • Thanks for everything! – SkyRipper Dec 07 '13 at 21:11