Having split function in string library

Skip to first unread message

Radium Sharma

Feb 25, 2019, 3:24:06 AM2/25/19
to ISO C++ Standard - Future Proposals
Hi all,
I thought we should have a split function of string like other languages so thought of proposing an idea.
Following is the implementation for having a split function in c++ string module like other languages. 
Function signature  -   vector<string> split(string s, char delimiter);
The function should take a string which should be splitted as first argument and a delimiter which will decide based on what it should split like (',',' ').
It should return a vector of strings which are splitted based on the deimiter.
Following is the function code :-

vector<string> split(string s, char delimiter){

int n = s.length(), k=0;

vector<string> v;

for(int i=0;i<n;i++){


string x = s.substr(k,i-k);



} else if(i==n-1){

string x = s.substr(k,i-k+1);




return v;



Ren Industries

Feb 25, 2019, 10:24:44 AM2/25/19
to std-pr...@isocpp.org
Why would we copy all these strings when std::string_view exists?

You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5a8125fc-82bd-4247-8da8-3e7b4d62bed7%40isocpp.org.
Message has been deleted
Message has been deleted

Cleiton Santoia

Mar 6, 2019, 10:04:47 AM3/6/19
to ISO C++ Standard - Future Proposals

You may implement this "generically", to work on other containers, std::array, plain arrays, vector, etc...

template< class ForwardIt1, class ForwardIt2, class OutIt >
void split( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, OutIt o ) { // 'o' may be anything
    auto sz = std::distance(s_first, s_last);
    auto next = first;
    while ( next != last ) {
        next = std::search(first, last, s_first, s_last);
        *o++ = { first, next } ; // <- copy elision guarantee that this is moving ?
        first = next;
        std::advance(first, sz);

have fun :)  https://coliru.stacked-crooked.com/a/a020522ddc01b8bc


Mar 7, 2019, 11:13:56 AM3/7/19
to ISO C++ Standard - Future Proposals
Reply all
Reply to author
0 new messages