如何从 C++ 中的字符串中提取值对
How can I extract pairs of values from a string in C++
我有一个格式如下的字符串:
"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++;
}
}
提前致谢。
不幸的是,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
只要知道格式,提取数据就很容易了。首先从字符串中删除任何引号或冒号,并将它们替换为 space。现在字符串由 space 分隔。
#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
我有一个格式如下的字符串:
"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++;
}
}
提前致谢。
不幸的是,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
只要知道格式,提取数据就很容易了。首先从字符串中删除任何引号或冒号,并将它们替换为 space。现在字符串由 space 分隔。
#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