使用C字符串数组令牌分析+ [英] String array token analysis using c++

查看:209
本文介绍了使用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;
}

您可以使用地图,请参考:的 http://www.cplusplus.com/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: http://www.cplusplus.com/reference/map/map/

这篇关于使用C字符串数组令牌分析+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆