[ create a new paste ] login | about

Link: http://codepad.org/1zrNIKgT    [ raw code | output | fork ]

C++, pasted on Nov 19:
template<class F1, class F2> inline
F1 find_first_not_of(const F1& first1, const F1& last1,
                     const F2& first2, const F2& last2)
{
    F1 fir = first1;
    for (; fir != last1; ++fir)
    {
        F2 mid2;
        for (mid2 = first2; mid2 != last2; ++mid2)
            if (*fir == *mid2)
                break;
        if(mid2==last2)
            return(fir);
    }
    return (fir);
}

bool Split(const std::string& org, const std::string& separators, std::vector<std::string>& words)
{
    std::string::const_iterator en, be=org.begin();
    do
    {
        en=find_first_of(be,org.end(), separators.begin(),separators.end());
        if(be!=en)
        {
            words.push_back("");
            for(std::string::const_iterator i=be; i!=(en); ++i)
                words.back()+=*i;
        }
        be=find_first_not_of(en,org.end(),
            separators.begin(),separators.end());
    } while(be!=org.end());

    return(words.size()?true:false);
}

int main()
{
    std::string str("Hallo,     wie geht es dir\tdenn heute so?");
    std::vector<std::string> v;
    if(Split(str," ",v))
    {
        for(size_t i=0;i<v.size();++i)
            std::cout << v[i] << endl;
    }
    v.clear();
    std::cout << "------------------------\n";
    if(Split(str," ,.?\t",v))
    {
        for(size_t i=0;i<v.size();++i)
            std::cout << v[i] << endl;
    }
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Hallo,
wie
geht
es
dir	denn
heute
so?
------------------------
Hallo
wie
geht
es
dir
denn
heute
so


Create a new paste based on this one


Comments: