简体   繁体   English

在C ++中解析字符串中的整数

[英]Parse integers in string in C++

I have a string that looks like this: 我有一个看起来像这样的字符串:

"{{2,3},{10,1},9}" 

and I want to convert it to an array (or vector) of strings: 我想将其转换为字符串数组(或向量):

["{", "{", "2", "}", ",", "{", "10", ",", "1", "}", ",", "9", "}"]

I can't just pull out each character separately because some of the integers may be double-digit, and I can't figure out how to use stringstream because there are multiple delimiters on the integers (could be followed by , or }) 我不能只拔出每个字符,因为某些整数可能是两位数,而且由于整数上有多个定界符(可能跟着或},所以我无法弄清楚如何使用stringstream。

Just walk through the string. 只需遍历字符串即可。 If we're on a digit, walk 'til we're not on a digit: 如果我们在数字上,请走直到直到我们不在数字上:

std::vector<std::string> split(std::string const& s)
{
    std::vector<std::string> results;
    std::locale loc{};

    for (auto it = s.begin(); it != s.end(); )
    {
        if (std::isdigit(*it, loc)) {
            auto next = std::find_if(it+1, s.end(), [&](char c){
                return !std::isdigit(c, loc);
            });
            results.emplace_back(it, next);
            it = next;
        }
        else {
            results.emplace_back(1, *it);
            ++it;
        }
    }

    return results;
}

The logic is straightforward: 逻辑很简单:

  1. Iterate over the string. 遍历字符串。

  2. push_back() each character as a string of its own into the output vector, unless it's a digit and the last character was a digit, in which case append the digit to the last string in the output vector: push_back()每个字符作为其自身的字符串放入输出向量中,除非它是一个数字并且最后一个字符是一个数字,在这种情况下,将数字附加到输出向量的最后一个字符串中:

That's it. 而已。

std::string s="{{2,3},{10,1},9}";

std::vector<std::string> v;

bool last_character_was_a_digit=false;

for (auto c:s)
{
    if ( c >= '0' && c <= '9')
    {
        if (last_character_was_a_digit)
        {
            v.back().push_back(c);
            continue;
        }
        last_character_was_a_digit=true;
    }
    else
    {
        last_character_was_a_digit=false;
    }
    v.push_back(std::string(&c, 1));
}

Thank you Sam and Barry for answering! 谢谢Sam和Barry的回答! I actually came up with my own solution (sometimes posting a question here helps me see things more clearly) but yours are much more elegant and delimiter-independent, I'll study them further! 实际上,我想出了自己的解决方案(有时在此处发布问题可以使我更清楚地看到问题),但是您的解决方案更加优雅且与分隔符无关,我将进一步研究它们!

std::vector<std::string> myVector;
std::string nextSubStr;
for (int i=0; i<myString.size(); i++)
{
    nextSubStr = myString[i];

    // if we pulled a single-digit integer out of myString
    if (nextSubStr != "{" && nextSubStr != "}" && nextSubStr != ",")
    {
        // let's make sure we get the whole integer!
        int j=i;
        peekNext = myString[++j];
        while (peekNext != "{" && peekNext != "}" && peekNext != ",")
        {
            // another digit on the integer
            nextSubStr += peekNext;
            peekNext = myString[++j];
            i++;
        }            
    }

    myVector.push_back(nextSubStr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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