Bayram Jumageldiyew
Bayram Jumageldiyew

Reputation: 13

Compare variables from map with vector in c++

So, I have an input

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

I store variables without operators in a map as the variable name and its type like x int, y int, and etc.

map <string,string> finalMap

I split variables of each line into a vector of string for each line as tokens = {20,40,80}; tokens = {s1,w}, and etc.

vector<string> tokens;

I want to compare variables in tokens and finalMap, if I have already declared a variable of tokens in the map. For example z=x+y, x and y already declared in finalMap, I want to check if those x and y already declared in finalMap to get their dataType string or int. I use double for loop, but it doesn't work for some reason.

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

I have trouble with the for loop above because when I check values after, it appears that it doesn't replace dataType from map.

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() returns variable type of string, either it is string, int, or list. Please don't downgrade I am a new user if you need more details just let me know I will explain.

Here is the output:

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

Upvotes: 0

Views: 96

Answers (1)

acraig5075
acraig5075

Reputation: 10756

Double equals == is comparison and not assignment.

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

You meant to use single equals =

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

Upvotes: 1

Related Questions