C++ 多重分割一个字符串
C++ Multi-split a string
我正在尝试拆分我收到的一些数据,数据是这样的:
0010|chocolate|cookie;458|strawberry|cream;823|peanut|butter;09910|chocolate|icecream
所以首先我需要分离 food
的每个部分(用“;”分隔),然后只得到包含 [=17= 的 food
部分的 ID
] 在上面,问题是数据不是静态的,所以我无法预测带有 "chocolate"
的 food
部分会出现多少次。
这是我拆分食物部分并获取数据中部分数量的代码:
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s);
string item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim)
{
vector<string> elems;
split(s, delim, elems);
return elems;
}
char* data = "0010|chocolate|cookie;458|strawberry|cream;823|peanut|butter;09910|chocolate|icecream";
int main()
{
vector<string> food = split(data, ';');
cout << number of food sections is : " << food.size();
return 0;
}
它有效,但现在我希望它阅读所有部分并列出哪些部分包含 "chocolate",例如:
0010|chocolate|cookie
09910|chocolate|icecream
然后只列出包含 chocolate
的部分的 ID,这可能与我使用的相同分割向量有关。
0010
09910
尝试使用函数在由 delim 分隔的字符串中查找单词,例如:
bool find(string vfood, string s, char delim)
{
std::istringstream to_find(vfood);
for (std::string word; std::getline(to_find, word, delim); ) if (word == s) return true;
return false;
}
然后你可以在'food'
的每个字符串中找到你想要的任何东西
vector<string> food_with_chocolate;
for (string &s : food)
{
if (find(s, "chocolate", '|')) food_with_chocolate.push_back(s);
}
这取决于你的数据有多丰富。最终你必须向它抛出一个递归下降解析器。但这似乎更简单。
分号可以转义吗?如果没有,尽管如此,每次你打一个分号,将索引存储在一个增长的向量中。这给你记录开始。然后逐步浏览记录。创建一个临时字符串,由分号之前的记录组成,然后搜索字符串 "chocolate"。如果匹配,则 id 是您记录中的第一个字段,所以直到第一个 |字符.
我正在尝试拆分我收到的一些数据,数据是这样的:
0010|chocolate|cookie;458|strawberry|cream;823|peanut|butter;09910|chocolate|icecream
所以首先我需要分离 food
的每个部分(用“;”分隔),然后只得到包含 [=17= 的 food
部分的 ID
] 在上面,问题是数据不是静态的,所以我无法预测带有 "chocolate"
的 food
部分会出现多少次。
这是我拆分食物部分并获取数据中部分数量的代码:
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s);
string item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim)
{
vector<string> elems;
split(s, delim, elems);
return elems;
}
char* data = "0010|chocolate|cookie;458|strawberry|cream;823|peanut|butter;09910|chocolate|icecream";
int main()
{
vector<string> food = split(data, ';');
cout << number of food sections is : " << food.size();
return 0;
}
它有效,但现在我希望它阅读所有部分并列出哪些部分包含 "chocolate",例如:
0010|chocolate|cookie
09910|chocolate|icecream
然后只列出包含 chocolate
的部分的 ID,这可能与我使用的相同分割向量有关。
0010
09910
尝试使用函数在由 delim 分隔的字符串中查找单词,例如:
bool find(string vfood, string s, char delim)
{
std::istringstream to_find(vfood);
for (std::string word; std::getline(to_find, word, delim); ) if (word == s) return true;
return false;
}
然后你可以在'food'
的每个字符串中找到你想要的任何东西vector<string> food_with_chocolate;
for (string &s : food)
{
if (find(s, "chocolate", '|')) food_with_chocolate.push_back(s);
}
这取决于你的数据有多丰富。最终你必须向它抛出一个递归下降解析器。但这似乎更简单。
分号可以转义吗?如果没有,尽管如此,每次你打一个分号,将索引存储在一个增长的向量中。这给你记录开始。然后逐步浏览记录。创建一个临时字符串,由分号之前的记录组成,然后搜索字符串 "chocolate"。如果匹配,则 id 是您记录中的第一个字段,所以直到第一个 |字符.