So I am new to maps, vectors too, but I read a guide on those.

And I need to do one thing with the map that I haven't figured out yet.

Implement this C# code into C++

map.TryGetValue(oldValue, out newValue)

In the C# code map is a dictionary, so I want to do the same thing with a map file, from my understanding the TryGetValue() function looks for the first parameter, in the keys, and if it finds it will store the value into the reference newValue. Any help? Ty :)

Recommended Answers

All 8 Replies

I don't know if this exact same thing exists, but you could do something similar using the std::map.find member function.

std::map< char, int >::iterator it = myMap.find(myKey);
if(it != std::map::end)    value = *it;

Usually anything with Try in front of it in C# checks to see if the retrieval/conversion/etc. is valid so you can use the call in an if statement.

Since the bool is returned, the value is returned by reference in the second parameter (C# has the designation of "out" to indicate that the parameter will be given a value within the method call). You can modify ravenous's code slightly to attain the same features.

I came up with this, seem accurate?

std::map<int, int>::iterator it = map.find(oldValue);
		if (it != map.end())
			newValue = it->second;
		else
			newValue = oldValue;

Except that if the value is not found the newValue would be set to the default value for a particular type (0 for ints, false for bools, null for objects) see: http://msdn.microsoft.com/en-us/library/zkw5c9ak(v=vs.80).aspx

You can do this in one line, if you like:

newValue = ( map.find(oldValue) != map.end() ? map[oldValue] : 0 );

I think it's slower, since there's a second lookup, but there's something aesthetically pleasing about a one-liner :o)

Probably best to use the one you had (with the alteration that jonsca mentioned).

So I could use this instead:

std::map<int, int>::iterator it = map.find(oldValue);
		if (it != map.end())
                        if (it->second != 0)
			      newValue = it->second;
                        else
                              newValue = oldValue
		else
			newValue = oldValue;

right?


Original code exact:

if (!map.TryGetValue(oldValue, out newValue))
                {
                    newValue = oldValue;
                }

I think you only need one if statement. you want to emulate

if (!map.TryGetValue(oldValue, out newValue)){
    newValue = oldValue;
}

Here, TryGetValue() returns true if it can set newValue to the value of the element indicated by the key oldValue . You are using the ! operator on this, so the body of the if only get executed if oldValue isn't a key in the map . If oldValue is in the map, then newValue is set to whatever the value of the element indicated by the key oldValue and the body of the if isn't executed.

You can get this behaviour with:

map< int, int > m;
map< int, int >::iterator it = m.find(oldValue);
if(it != m.end())
    newValue = it->second;
else
    newValue = oldValue;

So you only need one if statement. I think that the default value comes in when you try to use a key that doesn't exist. In this case, you're using std::map::find to look for elements, so I don't think you have to worry about that (although someone correct me if I'm wrong!). One thing that can happen with maps is if you use the [] operator with a key that doesn't exist, then that element is generated, with the element at the default value. So

int main()
{
    map< int, int > m;
    cout << m[2] << endl;
    return 0;
}

would print 0 , but it will also put the key-value pair 2,0 into the map. So, if you subsequently do cout << m.size(); you will get a size of 1.

Ok, so it was right the first time, thought it was. I tested both outputs of the C# version and the C++ version and they matched. Ty :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.