【问题标题】:Compare variables from map with vector in c++比较map中的变量和c ++中的向量
【发布时间】:2020-03-05 05:40:29
【问题描述】:

所以,我有一个意见

x=20
y=40
z=x+y
w=20+40+80
s1="str1"
s2=s1+w

我将不带运算符的变量作为变量名及其类型(如 x int、y int 等)存储在映射中。

map <string,string> finalMap

我将每行的变量拆分为每行的字符串向量,标记为 tokens = {20,40,80};令牌 = {s1,w} 等。

vector<string> tokens;

如果我已经在映射中声明了一个标记变量,我想比较标记和 finalMap 中的变量。例如 z=x+y, x 和 y 已经在 finalMap 中声明,我想检查那些 x 和 y 是否已经在 finalMap 中声明以获取它们的 dataType 字符串或 int。我使用双循环,但由于某种原因它不起作用。

for(auto i=finalMap.begin(); i!=finalMap.end();i++){
    for(int j=0; j<tokens.size(); j++){
        if(i->first==tokens.at(j)){
            tokens.at(j)==i->second;
        }
    }
}

我在上面的 for 循环中遇到了问题,因为当我在之后检查值时,它似乎没有替换 map 中的 dataType。

for(int i=0; i<tokens.size()-1; i++){
    if(checkType(tokens.at(i))!=checkType(tokens.at(i+1)))
        return "undefined";
    else
        return checkType(tokens.at(i));
} 

checkType() 返回字符串的变量类型,可以是字符串、整数或列表。请不要降级我是新用户,如果您需要更多详细信息,请告诉我,我会解释。

这是输出:

s1:str     
s2:undefined  
w:int          
x:int       
y:int     
z:undefined   

【问题讨论】:

    标签: c++ dictionary vector


    【解决方案1】:

    双等于== 是比较而不是赋值。

    if(i->first==tokens.at(j)) {
        tokens.at(j) == i->second;
    }
    

    您的意思是使用单个等于 =

    if(i->first==tokens.at(j)) {
        tokens.at(j) = i->second;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多