Domanda

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?

È stato utile?

Soluzione

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()))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top