Question

I am trying to pass AnsiString(someStr).c_str() to boost::split() second argument but it denies showing argument mismatch!!

here is the code snippet

vector<std::string> sVec;
boost::split(sVec,AnsiString(response).c_str(),boost::is_any_of(" "));//err in this line
ShowMessage(sVec[1].c_str());

however

boost::split(sVec,"This is a test",boost::is_any_of(" "));

works well!

Am I doing right converting AnsiString to c string type???

Was it helpful?

Solution

Since sVec is a vector<std::string> and not a vector<char *>, the second argument passed to split() has to be somehow converted into a std::string instance.

There is an implicit constructor in the std::string class that can transparently create an instance from a const char * (which is why your second example succeeds), but AnsiString::c_str() returns a char *, not a const char *, so this constructor does not apply.

Performing the conversion yourself should solve your problem:

boost::split(sVec, (const char *) AnsiString(response).c_str(),
    boost::is_any_of(" "));

Or, more explicitly:

boost::split(sVec, std::string((const char *) AnsiString(response).c_str()),
    boost::is_any_of(" "));

OTHER TIPS

I did it in this way since boost::split(sVec, (const char *) AnsiString(response).c_str(), boost::is_any_of(" ")); gives error (unfortunately)

AnsiString response="This is a test";
    vector<std::string> sVec;
    const char * cStr=AnsiString(response).c_str();
    boost::split(sVec, cStr,boost::is_any_of(" "));

    for (int i = 0; i < sVec.size(); i++) {
            ShowMessage(sVec[i].c_str());
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top