使用c++进行字符串数组标记分析
String array token analysis using c++
两个样本数组:
string key[] = {"a|b", "a|c","a|d","b|c","b|d"};
int value[]={"2", "3","4","5","2"};
这两个阵列是连接的<a|b>->2 <a|c>->3 <a|d>->4 <b|c>->5 <b|d>->2
每个由"|"
分隔的密钥,该令牌用于front_element和sencond_element
例如:a|b
a是前元素b为第二元素
数组可能是一个非常大的数组,我想找到一种方法或算法可以快速搜索元素
if(front_elemnet = "a"){ // find all of the front element with "a"
value_plus(); //plus the value 2+3+4
}
然后检查下一个不同的前端元件
if(front_elemnet = "b"){ // find all of the front element with "b"
value_plus(); //plus the value 5+2
}
#include <iostream>
#include <map>
#include <string>
using namespace std;
int value_plus(const map<string, int>& myMap, string target)
{
int res = 0;
map<string, int>::const_iterator iter = myMap.begin();
for(; iter!=myMap.end(); ++iter)
{
string key = iter->first;
if(key.substr(0, 1) == target)
{
res += iter->second;
}
}
return res;
}
int main() {
string key[] = {"a|b", "a|c","a|d","b|c","b|d"};
int value[] = {2, 3, 4, 5, 2};
map<string, int> myMap;
for(int i = 0; i < sizeof(key)/sizeof(string); i++)
{
myMap.insert(std::pair<string, int>(key[i], value[i]));
}
cout<<"front_element = a "<<value_plus(myMap, "a")<<endl;
cout<<"front_element = b "<<value_plus(myMap, "b")<<endl;
return 0;
}
您可以使用map
,请参阅:http://www.cplusplus.com/reference/map/map/
相关文章:
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13