如何提取字符串中所有夹杂字母的数字?

huangapple go评论66阅读模式
英文:

How do I extract all the numbers in a string where the numbers are interspersed with letters?

问题

#include <bits/stdc++.h>

int main(){
    string f = "e385p336J434Y26C2Z6X5Z2";
    vector<int> f_numb; 
    string sum; 
    for (int i = 0; i < f.size(); ++i){
         if (('0' <= f[i]) && (f[i] <= '9')){
            sum += (f[i]);
         }
    }
    vector<int> m_numb; 
    for (int i = 0; i < sum.size(); ++i){
        m_numb.push_back(sum[i] - '0'); 
    }
    int sm = 0; 
    for (int i = 0; i < m_numb.size(); ++i){
        sm += m_numb[i]; 
        std::cout << m_numb[i] << " "; 
    }
    std::cout << std::endl; 
}
英文:

How do I loop through a string consisting of numbers and letters and add only numbers to the vector?

For example if the input is:

> e385p336J434Y26C2Z6X5Z2

I want to get a vector of int like this:

> number = {385, 336, 434, 26, 2, 6, 5, 2}


The best I got was to iterate over the line and add all the digits like that:

#include &lt;bits/stdc++.h&gt;


int main(){
    string f = &quot;e385p336J434Y26C2Z6X5Z2&quot;;
    vector&lt;int&gt; f_numb; 
    string sum; 
    for (int i = 0; i &lt; f.size(); ++i){
         if ((&#39;0&#39; &lt;= f[i]) &amp;&amp; (f[i] &lt;= &#39;9&#39;)){
            sum += (f[i]);
            
             
         }
    }
    //std::cout &lt;&lt; sum &lt;&lt; std::endl; 
    vector&lt;int&gt; m_numb; 
    for (int i = 0; i &lt; sum.size(); ++i){
        m_numb.push_back(sum[i] - &#39;0&#39;); 
    }
    int sm; 
    for (int i = 0; i &lt; m_numb.size(); ++i){
        sm += m_numb[i]; 
        std::cout &lt;&lt; m_numb[i] &lt;&lt; &quot; &quot;; 
    }
    std::cout &lt;&lt; std::endl; 
    
}

答案1

得分: 0

Output 经过处理之后是:

385 336 434 26 2 6 5 2

假设我理解你的问题和你的代码,尽管它们在显然的目标方面存在很大差异。

英文:

Foregoing the unstated reason you're not using std::isdigit, you can/should simply build each number as you process its digits. Note that the code below does NOT check, nor care, about unsigned overflow, and makes no attempt at processing negative numbers.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

int main()
{
    std::string f = &quot;e385p336J434Y26C2Z6X5Z2&quot;;
    std::vector&lt;unsigned int&gt; f_numb;

    for (auto it = f.begin(); it != f.end();)
    {
        if (&#39;0&#39; &lt;= *it &amp;&amp; *it &lt;= &#39;9&#39;)
        {
            unsigned int sm = 0;
            for (;it != f.end() &amp;&amp; &#39;0&#39; &lt;= *it &amp;&amp; *it &lt;= &#39;9&#39;; ++it)
                sm = (sm * 10) + (*it - &#39;0&#39;);

            f_numb.emplace_back(sm);
        }
        else
        {
            ++it;
        }
    }

    for (auto x : f_numb)
        std::cout &lt;&lt; x &lt;&lt; &#39; &#39;;
    std::cout &lt;&lt; &#39;\n&#39;;
}

Output

385 336 434 26 2 6 5 2 

That, assuming I understand your question vs. your code, which differ highly in their apparent goals.

huangapple
  • 本文由 发表于 2023年2月19日 19:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定