【问题标题】:How can I extract pairs of values from a string in C++如何从 C++ 中的字符串中提取值对
【发布时间】:2017-10-24 01:39:41
【问题描述】:

我有一个这样格式的字符串:

"name1":1234  " name2  "  : 23456  "name3"  : 12345 

等等……

我曾尝试使用嵌套的 while 循环和两个整数来存储要在 string::substr 中使用的位置和长度,但我找不到合适的方法来获取它(大多数情况下我都退出了字符串)。

这些值不需要存储,因为我可以调用一个函数来处理它们。

这是我到目前为止所做的:

void SomeClass::processProducts(std::string str) {
unsigned int i = 0;
std::string name;
    while (i < str.length()) {
        if (str[i] == '\"') {
            int j = 1;
            while (str[i + j] != '\"') {
                j++;
            }
            name = str.substr(i + 1, j - 1);
            i += j;
        }
        else if (str[i] >= '0' && str[i] <= '9') {
            int j = 1;
            while (str[i + j] >= '0' && str[i + j] <= '9') {
                j++;
            }

            //This is just processes the values
            std::stringstream ss;
            std::string num = str.substr(i, j);
            ss.str(num);
            int products = 0;
            ss >> products;
            if (products == 0) {
                Util::error(ERR_WRONG_PRODUCTS);
            }
            int pos = getFieldPos(name);
            if (pos == -1) {
                Util::error(ERR_WRONG_NAME);
            }
            else {
                fields[pos].addProducts(products);
            }
            i += j;
        }
        i++;
    }
}

提前致谢。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。
  • edit 您的问题显示您尝试过但不适合您的实际代码。到目前为止,您为自己调试做了什么?
  • 你想得到什么结果?例如,您想从“name1”中得到什么?
  • 名称是引号之间的内容,产品是数字
  • 这正是您所期望的格式吗?都在一行,用空格隔开?

标签: c++ string text-parsing


【解决方案1】:

不幸的是,C++ 没有强大的开箱即用的字符串解析能力。这就是为什么有很多方法可以完成这类任务。

但是,C++ 确实提供了帮助工具。所以我们可以使用它们,至少可以避免手动循环。

在我们开始之前,我想提请注意一个事实,即当我们处理用户输入时,我们必须格外小心地验证输入。

我选择的解决方案需要的块是:

  • 匹配格式(与"name" : value)。为此我选择了std::find。也可以使用正则表达式。
  • value 解析为一个数字。为此,我们可以使用std::stoi。看看下面为什么还不够。
  • 始终确保获得预期的输入。这增加了一些样板代码,但这是我们必须付出的代价。同样在这里,我们遇到了std::stoi 的问题,因为它很乐意接受尾随的非空格而不大惊小怪。因此,例如123 invalid 将被解析为123。这就是我在它周围使用一个小包装器的原因parse_string_to_int

好的,我们开始吧:

小帮手:

auto parse_string_to_int(const std::string& str)
{
    std::size_t num_processed = 0;
    int val                   = std::stoi(str, &num_processed, 10);

    auto next_non_space = std::find_if(str.begin() + num_processed, str.end(),
                                       [](char ch) { return !std::isspace(ch); });

    if (next_non_space != str.end())
        throw std::invalid_argument{"extra trailing characters in parse_string_to_int"};

    return val;
}
struct Product_token
{
    std::string name;
    int value;
};

auto get_next_product(std::string::const_iterator& begin, std::string::const_iterator end)
    -> Product_token
{
    // match `"name" : value "`
    auto name_open_quote       = std::find(begin, end, '\"');
    auto name_close_quote      = std::find(name_open_quote + 1, end, '\"');
    auto colon                 = std::find(name_close_quote, end, ':');
    auto next_token_open_quote = std::find(colon, end, '\"');

    if (name_close_quote == end || name_close_quote == end || colon == end)
    {
        // feel free to add more information regarding the error.
        // this is just the bare minimum to accept/reject the input
        throw std::invalid_argument{"syntax error on parsing product"};
    }

    // advance to next token
    begin = next_token_open_quote;

    return Product_token{{name_open_quote + 1, name_close_quote},
                         parse_string_to_int({colon + 1, next_token_open_quote})};
}

auto process_products(const std::string& str)
{
    auto begin = str.begin();

    while (begin != str.end())
    {
        auto product = get_next_product(begin, str.end());
        cout << '"' << product.name << "\" = " << product.value << endl;
    }
}
int main()
{
    auto str = R"("name1":1234  " name2  "  : 23456  "name3"  : 12345)"s;

    try
    {
        process_products(str);
    }
    catch (std::exception& e)
    {
        cerr << e.what() << endl;
    }
}

查看完整代码on ideone

【讨论】:

  • 看起来棒极了!非常感谢。
【解决方案2】:

只要你知道格式,那么提取数据就相当容易了。首先从字符串中删除任何引号或冒号并用空格替换它们。现在字符串由空格分隔。

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

using namespace std;


int main() 
{
    string str("\"name1\":1234  \" name2  \"  : 23456  \"name3\"  : 12345");
    cout << str << endl;
    // remove ':' and '"' and replace them by space 
    std::replace_if(str.begin(), str.end(), ispunct, ' ');
    istringstream ss(str);
    vector<string> words;
    // store data as name and number in vector<string> 
    copy(istream_iterator<string>(ss),istream_iterator<string>(),back_inserter(words));

    for (int i(0); i < words.size(); i+=2)
        cout << "name: " << words[i] << "  number: "  << words[i+1] << endl;


    return 0;
}

结果是

"name1":1234  " name2  "  : 23456  "name3"  : 12345
name: name1  number: 1234
name: name2  number: 23456
name: name3  number: 12345

【讨论】:

  • 好答案,但由于名称可能包含空格,因此不适用于我的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
相关资源
最近更新 更多