how use a split complex?

1
2
3
4
5
6
7
8
9
10
template<typename Out>
void split(const std::string &s, Out result)
{
    std::stringstream ss(s);
    std::string item;
    while ((std::getline(ss, item, ' ')) || (std::getline(ss, item, '\t')) || (std::getline(ss, item, '('))  || (std::getline(ss, item, ')')) )
    {
        *(result++) = item;
    }
}

like you see i'm using several char's with several or's.
these line\condition can be simplificated(i have much more to add)?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <vector>


void split( const std::string &s, std::vector<std::string> &result, std::string dividers )
{
    int p = 0, q;
    while ( p < s.size() )
    {   
        q = s.find_first_of( dividers, p );
        if ( q == std::string::npos ) break;
        result.push_back( s.substr( p, q - p ) );
        p = q + 1;
    }
    if ( p < s.size() ) result.push_back( s.substr( p ) );
}


int main()
{
   std::string test = "Beans&means\tHeinz";
   std::vector<std::string> phrases;

   split( test, phrases, "&$\t" );
   for ( std::string s : phrases ) std::cout << s << '\n';
}

the code is encompleted :(
only the tabs and empty spaces must be ignored, but '&' must be added splitted too.
so what you can advice me more?
(i don't understand the while code :( )
I don't understand what you are asking, @Cambalinho.

The idea is that you can specify whatever characters you like in dividers.

If you want it to split on the characters in your code then just use
split( test, phrases, " \t()" );
the problem is the '(' and ')' been ignored.
almost completed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

struct Separator
{
    enum enSeparator
    {
        OpenParenthese='(',
        CloseParenthese=')'
    };

    bool IsSeparator(char chrSeparator)
    {
        switch (chrSeparator)
        {
            case '(':
            case ')':
            {
                return true;
            }
        }
        return false;
    }
}Separator;

void split( const std::string &s, std::vector<std::string> &result, std::string dividers )
{
    unsigned int p = 0, q;
    while ( p < s.size() )
    {
        q = s.find_first_of( dividers, p );
        if ( q == std::string::npos) break;
        result.push_back( s.substr( p, q - p ) );
        if( Separator.IsSeparator(s[q]))
        {
            string strValue{s[q]};
            result.push_back(strValue );
        }
        p = q + 1;
    }
    if ( p < s.size() ) result.push_back( s.substr( p ) );
}

int main()
{
   std::string test = "Beans&means\tHeinz (hey) hello";
   std::vector<std::string> phrases;

   split( test, phrases, "&\t()\n" );
   for ( std::string s : phrases ) std::cout << s << '\n';


    return 0;
}

i have 1 question: why the last one give me ' hello' instead 'hello'?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <vector>


void split( const std::string &s, std::vector<std::string> &result, std::string dividers )
{
    int p = 0, q;
    while ( p < s.size() )
    {   
        q = s.find_first_of( dividers, p );
        if ( q == std::string::npos ) break;
        if ( q != p ) result.push_back( s.substr( p, q - p ) );
        p = q + 1;
    }
    if ( p < s.size() ) result.push_back( s.substr( p ) );
}


int main()
{
   std::string dividers = " \t()";               // put what you like here

   std::string test = "And Quiet Flows the  Don(Mikhail\tSholokhov)";
   std::vector<std::string> phrases;

   split( test, phrases, dividers );

   for ( std::string s : phrases ) std::cout << s << '\n';
}


And
Quiet
Flows
the
Don
Mikhail
Sholokhov
Last edited on
thank you so much. fixed like i need.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

//the saperators that must be printed:
struct Separator
{
    enum enSeparator
    {
        OpenParenthese='(',
        CloseParenthese=')'
    };

    bool IsSeparator(char chrSeparator)
    {
        switch (chrSeparator)
        {
            case '(':
            case ')':
            {
                return true;
            }
        }
        return false;
    }
}Separator;

void split( const std::string &s, std::vector<std::string> &result, std::string dividers )
{
    unsigned int p = 0, q;
    while ( p < s.size() )
    {
        q = s.find_first_of( dividers, p );
        if ( q == std::string::npos ) break;
        if ( q != p ) result.push_back( s.substr( p, q - p ) );

        //if have a separator, must be added:
        if(Separator.IsSeparator(s[q])==true)
        {
            string strValue{s[q]};
            result.push_back(strValue);
        }
        p = q + 1;
    }
    if ( p < s.size() ) result.push_back( s.substr( p ) );
}

int main()
{
   std::string test = "And Quiet Flows the  Don(Mikhail\tSholokhov)";
   std::vector<std::string> phrases;

   split( test, phrases, " &\t()\n" );
   for ( std::string s : phrases ) std::cout << s << '\n';


    return 0;
}

output(with my change):
And
Quiet
Flows
the
Don
(
Mikhail
Sholokhov
)

thank you so much for all... thank you
Topic archived. No new replies allowed.