简体   繁体   中英

Split a sentence like 1100010011 into words in C++

I am parsing a string "1100010011" in C++ using the following:

string instring = "1100010011";
char last = instring.at(0);
string res = "";
vector<string> _chain;
int len_end = instring.length();
int len_instring = len_end + 1;
for (int count = 0; count != len_instring; ++count){
    if (count != len_end && instring.at(count) == last) {
        res += last;
    }
    else
    {
        _chain.push_back(res);
        if (count != len_end) {
            last = instring.at(count);
            res = last;
        };
    };
};

The result is '1100010011' -> '11', '000', '1', '00', '11'. But I think this code are pretty dumb. Is there a way to improve this code?

upd (10 feb 2021):
Is there a way to use bitwise logical operations to rewrite this code for faster splitting? If you imagine a text string with bits as a regular digit. 1100010011 = 313

its a simple iteration.. take a value 'x' as the current index character and increment the pointer until the value while pointer is not equal to the x, the pointer will stop when it will get value not equal to x,now update the x...

string s;
cin>>s;
int sz=s.size();
for(int i=0;i<sz;)
{
    char x=s[i];
    while(s[i]==x)
    {
        cout<<s[i];
        i++;
    }
    cout<<" ";
}

std::string has a find_first_not_of member function that you can use to find the first letter that is different to the current character.

So you can do a loop like this:

#include <iostream>
#include <vector>

int main()
{
    auto input = std::string("1100010011");
    std::vector<std::string> vec;

    size_t it = 0;
    size_t last = 0;

    while ((it = input.find_first_not_of(input[it], last)) != std::string::npos)
    {
        vec.push_back(input.substr(last, it-last));
        last = it;
    }

    vec.push_back(input.substr(last, input.size() - last));
}

Here is my proposition.You can find the explanations in the comments:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    string s = "1100010011";
    //string s = "1100010011222333";
    vector<string> chain;


    // the first char of the string
    std::string temp ;
    char tmp = s[0];
    // run though the characters of the string

    for (char x : s)
    {
        // compare each character
        if ( x != tmp )
        {
            //cout << x << endl;
            tmp = x;
            // if different then add the constructed string from temp and clear temp to receive new sets of characters
            chain.push_back(temp);
            temp.clear() ;
        }
        // we add the character to temp anyway
        temp.push_back(x);
    }
    // we add the last constructed value from temp
    chain.push_back(temp);

    for (string  x : chain)
    {
        std::cout << x << " " ;
    }
}

test cases: Input 1100010011 provides the ouput:

11 000 1 00 11

Input 1100010011222333 provides the ouput:

11 000 1 00 11 222 333

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM