hw——取出字符串中的字符

#include <iostream>

using namespace std;
//ab7c d234bk jalf 34 78k3j4 a 59jfd45
int stoi(string str, int *start = NULL)   //string to integer
{
    bool bFirstTime = true;

    int s = 0;
    size_t i;
    for(i=*start; i<str.size(); i++)
    {
        if (bFirstTime)
        {
            if (str[i]<='9' && str[i]>='0')
            {
                bFirstTime = false;
                s = str[i]-'0';
            }
        }
        else
        {
            if (str[i]<='9' && str[i]>='0')
            {
                s = 10*s+(str[i]-'0');
            }
            else               //多次尝试,要是不符合就直接return了
            {
                *start = i;
                return s;
            }
        }
    }

    if (bFirstTime)
        *start = -1;
    else
        *start = i;
    return s;
}

int main()
{
    string line;
    int ndigit;
    int start = 0;

    getline(cin, line);
    while(start<(int)line.size() && start!=-1)
    {
        ndigit = stoi(line, &start);
        if (start!=-1)
            cout << ndigit << " ";
    }

    return 0;
}

@单独出一个函数,把一次提取数字当作一个函数:我是想着一串数字一起整,没写函数,你既要记录数字在之前是否出现过,又要照顾到b*10,需要在不同的地方立好多flag

@指针的用法要学习

原文地址:https://www.cnblogs.com/syzyaa/p/12901095.html