使用C字符串数组令牌分析+

编程入门 行业动态 更新时间:2024-10-28 03:23:04
本文介绍了使用C字符串数组令牌分析+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

两个示例数组:

字符串键[] = {A | B,A | C,A | D,B | C,B | D};int值[] = {2,3,4,5,2};

和这两个数组是结< A | B> - > 2';一个| C> - >第3版; A | D> - > 4℃; B | C&GT ; - > 5℃; b | D> - > 2

分隔每个键|此标记为front_element和sencond_element

例如: A | B 是front_element的乙是second_element

阵列可能是一个非常大的数组,我想找到一种方法或算法可能快速搜索元素

如果(front_elemnet =A){//找到所有的正面因素与一value_plus(); //加上值2 + 3 + 4}

接着检查下一个不同的前元素

如果(front_elemnet =B){//找到所有的正面元素与B    value_plus(); //加上价值5 + 2    }

解决方案

的#include<&iostream的GT;#包括LT&;地图和GT;#包括LT&;串GT;使用命名空间std;INT value_plus(常量地图<字符串,整数>&安培; MYMAP,目标字符串){    INT RES = 0;    地图<字符串,整数> ::为const_iterator ITER = myMap.begin();    为(;!ITER = myMap.end(); ++ ITER)    {        字符串键= iter->首先,        如果(key.substr(0,1)==靶)        {            RES + = iter->第二个;        }    }    返回水库;}诠释主(){    字符串键[] = {A | B,A | C,A | D,B | C,B | D};    int值[] = {2,3,4,5,2};    地图<字符串,整数> MYMAP;    的for(int i = 0; I<的sizeof(键)/的sizeof(串);我++)    {        myMap.insert(性病::对<字符串,整数>(键[I],价值[I]));    }    COUT&所述;&下;front_element =一个&所述;&下; value_plus(MYMAP,一个)&所述;&下; ENDL;    COUT&所述;&下;front_element = B&所述;&下; value_plus(MYMAP,B)所述;&下; ENDL;    返回0;}

您可以使用地图,请参考:的 www.cplusplus/reference/map/map/

Two sample arrays:

string key[] = {"a|b", "a|c","a|d","b|c","b|d"}; int value[]={"2", "3","4","5","2"};

and these two arrays are connective <a|b>->2 <a|c>->3 <a|d>->4 <b|c>->5 <b|d>->2

every keys separated by "|" this token for the front_element and sencond_element

for example : a|b a is the front_element b is the second_element

array could be a very large array and I want to find a way or algorithm that could quick search the element

if(front_elemnet = "a"){ // find all of the front element with "a" value_plus(); //plus the value 2+3+4 }

then check the next different front element

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; }

You can use map, please refer: www.cplusplus/reference/map/map/

更多推荐

使用C字符串数组令牌分析+

本文发布于:2023-10-29 22:44:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   数组   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!