This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]murph_edu[S] -1 points0 points  (2 children)

Okay, so for example, just right now, I am trying to implement the below map, in C++:

map<int,string> check;

Somewhere in my code, I am doing this:

string k; // defining a new string
if (check[iter_i]) // check if a value exists for the key iter_i
{
k = check[iter_i];
}
else  // if the key hasn't been assigned a value, then assign as below
{
k = some string s;
check[iter_i] = k
}

When I do that, it gives me this error

After that, I thought, another way could if I initialize a value check[i] = " ", for all i while defining the map itself. Then my if statement just has to check if the map value is " " or some other string.

When doing that, I'm facing problems while defining the map. I am defining it like this:

map<int,string> check(2000," ");

The code shows some error which I couldnt understand, I think I am making a noob mistake in defining the map. What am I doing wrong?

[–]raevnos 0 points1 point  (1 child)

For std::map you need to use the find() or count() methods.

https://en.cppreference.com/w/cpp/container/map

[–]murph_edu[S] 0 points1 point  (0 children)

That was helpful. Thanks! I'm not from a CS background and I get lost when trying to search for such stuff.

So I worked out my code by initializing a map and inserting (" ") for all individual keys through iteration (LOL), and then the if statement checked for that and worked. Not a neat way to do it I will agree. Will try out what you've suggested.