如何使用stringstream解析数字并将其放入数组中?

问题描述 投票:-1回答:3

因此,我在c ++中的函数中使用stringstream从字符串中获取数字,然后将数字返回到main中的数组,但是由于某些原因,它们总是返回0而不是实际数字。代码在下面,有人知道如何解决此问题吗?

int main()
{
for(int i = 0; i < userInput.length(); i++)
{
...
else
{
chemNumbers[i] = extractIntegerWords(userInput);
cout << chemNumbers[i] << endl;

}

}
int extractIntegerWords(string str)
{
    stringstream ss;
    int num = 0;

    ss << str;

    /* Running loop till the end of the stream */
    string temp;
    int found;
    if(!ss.eof())
    {

        ss >> temp;
        if (stringstream(temp) >> found)
        {
           num = found;
        }
        temp = "";

    }
    return found;
}

原始的if语句仅与在else语句中看到的功能不相关,该语句位于main中

c++ stringstream
3个回答
0
投票

这太麻烦了,我没有太多时间来测试它,但这应该使您朝正确的方向:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using std::cout; 
using std::endl;
using std::istringstream;
using std::string;
using std::vector;

inline bool tryGetInt(const string& str, string& out) { istringstream sStream(str); return !(sStream >> out).fail(); } /*!< Tries to parse a string to int. */

void splitString(const string &str, const string &delimiters, vector<string> &tokens) {
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos) {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}

int32_t main(int argCount, char* argValues[]) {
    for (int32_t i = 0; i < argCount; i++) {
        cout << "Found argument " << argValues[i] << endl;
        auto foundIntegers = getIntegerFromString(string(argValues[i]));

        // Do whatever
    }

    return 0;
}

vector<int64_t> getIntegerFromString(string formula) {
    vector<int64_t> foundInts;
    string temp;

    if (tryGetInt(formula, temp)) {
        vector<string> intsAsStrings;
        splitString(temp, " ", intsAsStrings);
        for (auto item : intsAsStrings) {
            foundInts.push_back(stol(item));
        }
    }

    return foundInts;
}

0
投票
#include <vector>
#include <string>
#include <sstream>

vector<int> extractIntegerWords(const string& _str)
{
    stringstream ss;
    ss << _str;    
    vector<int> vec;

    string temp;
    int found;
    while(!ss.eof())
    {
        ss >> temp;
        if (stringstream(temp) >> found)
        {
           vec.push_back(found);
        }
        temp = ""; 
    }

    return vec;
}

一些评论:

    输入参数中的
  1. const string&,而不是string。您不想损害您的源数据
  2. 您需要一个loop而不是“ if”(例如,该操作将只运行一次)
  3. 您的数组在哪里?
  4. 在C ++中更喜欢使用向量而不是数组

0
投票

正则表达式可能会帮助:

std::vector<std::pair<std::string, std::size_t>> Extract(const std::string& s)
{
    std::vector<std::pair<std::string, std::size_t>> res;
    const std::regex reg{R"(([a-zA-Z]+)(\d+))"};

    for (auto it = std::sregex_iterator(s.begin(), s.end(), reg);
         it != std::sregex_iterator();
         ++it)
    {
        auto m = *it;
        res.push_back({m[1], std::stoi(m[2])});
    }
    return res;
}

int main()
{
    for (auto p : Extract("C6H12O6")) {
        std::cout << p.first << ": " << p.second << std::endl;
    }
}

Demo

© www.soinside.com 2019 - 2024. All rights reserved.