mercredi 23 août 2017

C++ text to std::vector with a delimeter, when text begins with delimeter

I want to split the string "/1/2/3/4" to four parts, ie 1,2,3,4. I am using boost to split the string

#include <boost/algorithm/string.hpp>
#include <vector>
#include <string>

int main(int argc, char** argv) {

        std::vector<std::string> v;
        //boost::split(v, argv[1], boost::is_any_of("/"));
        boost::split(v, argv[1], [](char c){return c == '/';});
        for( auto i : v )
                std::cout << i << ' ';
        std::cout << std::endl;

        std::cout << v.size() << std::endl;

        return 0;

}

when I run I am taking an extra empty word in my vector

[oracle@localhost split]$ ./a.out /1/2/3/4
 1 2 3 4 
5

due to the fact that my word (/1/2/3/4) starts with the delimiter. How do I resolve this issue? I want the vector to hold 1,2,3,4 only.

Aucun commentaire:

Enregistrer un commentaire