let me give an example:I have three groups of strings which will have a fixed size, I thought of using lists. Let's say I name them Red, Green, Blue:
让我举一个例子:我有三组具有固定大小的字符串,我想使用列表。假设我将它们命名为Red,Green,Blue:
std::string Red[] = {"apple", "rose mary", "watermelon"};
std::string Green[] = {"cucumber", "avocado", "pine tree"};
std::string Blue[] = {"sea", "lake"};
I have found examples here where we search one item inside each of these lists, and they find it if the string matches, for example in that case I should have :
我在这里找到了一些示例,我们在每个列表中搜索一个项目,如果字符串匹配,他们会找到它,例如在这种情况下我应该:
std::string myinput = "watermelon";
if (std::find(std::begin(Red), std::end(Red), myinput) != std::end(Red))
{
cout << "found " << myinput << " in Red" << endl;
}
Ok so far, but I want something different: I want to scan a vector with 1000 elememnts, and myinput belongs to one of those elements which I access like this:
好到目前为止,但我想要一些不同的东西:我想扫描一个包含1000个元素的向量,myinput属于我访问过的元素之一:
for (int j = 0; j < Vector.size(); j++){
if (Vector[j].message == contains a string from one of the groups ){
cout<< Vector[j].message << endl;}
}
The Vector[j].message will be a string which has this format:
Vector [j] .message将是一个具有以下格式的字符串:
"flag: She adores watermelon"
"flag: They visited the lake"
"flag: He has cucumber for salad"
"flag: Wacamole made of avocado"
You see that the substring flag is common in all strings of the vectors. However, the watermelon doesn't exist in another group of strings.
您会看到substring标志在向量的所有字符串中都是通用的。但是,西瓜不存在于另一组字符串中。
The goal is to scan each group of lists and find that the element of the vector "flag: She adores watermelon" is listed in group Red. This should not be listed in the group yellow just because of the substring "flag". Also, I want the substring to contain the whole string stated in the group, for example if the Vector contains an element like "flag: the plant has many pines" , this should not be listed to the Green group, it should be uncategorized.
目标是扫描每组列表,并发现矢量“flag:She adores watermelon”的元素列在Red组中。由于子字符串“flag”,这不应该在组黄色中列出。此外,我希望子字符串包含组中声明的整个字符串,例如,如果Vector包含“flag:植物有许多松树”之类的元素,则不应将其列入Green组,它应该未分类。
Then these messages should be categorized and printed in different colors, red first in red colour etc.
然后,这些消息应分类并以不同的颜色打印,红色先用红色等。
First, do you agree with lists' idea? Do you suggest a more efficient way? What do you suggest for the substring search?
首先,你同意清单的想法吗?你建议一个更有效的方法吗?你对子串搜索有什么建议?
Excuse the lame examples and if my description in the title is not clear. I am new to this and looking for ideas.
请原谅蹩脚的例子,如果标题中的描述不清楚。我是新手,正在寻找想法。
1 个解决方案
#1
0
You can use regular expressions. Instead of this:
您可以使用正则表达式。而不是这个:
{"apple", "rose mary", "watermelon"}
Use this:
std::regex red("apple|rose mary|watermelon");
Then for each input line:
然后为每个输入行:
if (std::regex_search(line, red)) {
// it's red
}
You can then create a vector<pair<regex, string>>
and name each pattern:
然后,您可以创建一个向量
vector<pair<regex, string>> patterns = {
{"apple|rose mary|watermelon", "red"},
{"cucumber|avocado|pine tree", "green"},
{"sea|lake", "blue"},
};
This way you can easily iterate over all the patterns and get the color for any match.
这样,您可以轻松地遍历所有模式并获得任何匹配的颜色。
#1
0
You can use regular expressions. Instead of this:
您可以使用正则表达式。而不是这个:
{"apple", "rose mary", "watermelon"}
Use this:
std::regex red("apple|rose mary|watermelon");
Then for each input line:
然后为每个输入行:
if (std::regex_search(line, red)) {
// it's red
}
You can then create a vector<pair<regex, string>>
and name each pattern:
然后,您可以创建一个向量
vector<pair<regex, string>> patterns = {
{"apple|rose mary|watermelon", "red"},
{"cucumber|avocado|pine tree", "green"},
{"sea|lake", "blue"},
};
This way you can easily iterate over all the patterns and get the color for any match.
这样,您可以轻松地遍历所有模式并获得任何匹配的颜色。