user765443
user765443

Reputation: 1892

split and parse string and convert into char* in c++

I have couple of question in below. I have taken this code from Stackoverflow only and trying to understand.

How Split code working be specific:I could not understand following code

std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
    elems.push_back(item);

I have added code for remove space but it is giving me compilation error.Again I have taken code stackoverflow and not able to figure out the error

split.cpp:34:88: error: no matching function for call to
‘remove_if(std::vector<std::basic_string<char> >::iterator,
std::vector<std::basic_string<char> >::iterator, <unresolved
overloaded function type>)’ split.cpp:34:88: note: candidate is: In
file included from
/linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
                 from split.cpp:5: /linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:1140:5:
note: template<class _FIter, class _Predicate> _FIter
std::remove_if(_FIter, _FIter, _Predicate)
/linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:1140:5:
note:   template argument deduction/substitution
failed:split.cpp:34:88: note:   couldn't deduce template parameter
‘_Predicate’

How can I convert vector<string> to char*

    #include<iostream>
    #include<string>
    #include<sstream>
    #include<vector>
    #include<algorithm>
    #include<cctype>
    using namespace std;

    std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
        std::stringstream ss(s);
        std::string item;
        while (std::getline(ss, item, delim)) {
            elems.push_back(item);
        }
        return elems;
    }

    std::vector<std::string> split(const std::string &s, char delim) {
        std::vector<std::string> elems;
        split(s, delim, elems);
        return elems;
    }

    int main()
    {
        std::vector<std::string> f_data;
        f_data.push_back("A=  99.58%");
        f_data.push_back("B= 78%");
        f_data.push_back("C= 90%");
        vector<string>::iterator t_data;
        for(t_data = f_data.begin(); t_data != f_data.end(); t_data++)
        {
            vector<string> temp_data = split(*t_data, '=');
            //temp_data.erase(std::remove_if(temp_data.begin(), temp_data.end(), std::isspace), temp_data.end());
        vector<string>::iterator data;
        for(data = temp_data.begin(); data != temp_data.end(); data++)
        {
            cout<<*data;
        }

    }
    return 0;
}

Upvotes: 0

Views: 258

Answers (2)

user2249683
user2249683

Reputation:

There are two std::isspace (one in < cctype > and the other in < locale >). Also you should remove the spaces and not the string in the vector:

inline bool is_space(char c) { return std::isspace(c); }


...
for(std::vector<std::string>::iterator t = temp_data.begin(); t != temp_data.end(); ++t) {
    std::string& s = *t;
    std::remove_if(s.begin(), s.end(), is_space);
}
...

Upvotes: 0

Marius Bancila
Marius Bancila

Reputation: 16318

Why do you return the elems vector? You have it passed by reference, you don't have to return it.

void split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

Of course, this looks better (notice it's returned by value):

std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

As for how to convert a vector<string> to char*, concatenate the strings in the vector and then access the internal buffer of the resulting string with c_str().

Upvotes: 1

Related Questions