Originally Posted by
hk_mp5kpdw
Code:
string s = "m";
int j2 = testMap[s];
If
testMap["m"] does not exist, this will create an entry in the
map for that key value thus increasing the size. The
value portion of the
map for that
key gets default initialized, which in the case of an integer means the value of 0. If you don't want to actually create an element, then you do as suggested above.
You then get the segmentation fault because the size of the
map is greater than the size of the
vector by 1 element. When you try to loop, you access
vec_st[i] for an index
i of
vec_st that does not exist.
[/code]
Many thanks for the explanation. I now see that even an attempt to access a missing key is causing the map to create an entry for this missing key, but I am still a little bit surprised because I did not make an assignment statement to the second element of the map, I only entered the first (left) element in my code.
By contrast, when we use a C++ array instead of a map, this time if we try to access an element of the array that is missing, this still does not seem to change the size of the array..
I wrote the following test code to illustrate this comparison, including the use of the map.find() function that you recommended. The output of the program is also listed after the code:
Code:
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <array>
using namespace std;
map<string, int> testMap;
int main()
{
array<double,5> my_array;
for(int i=0; i<5; i++) my_array[i]=i+1; // Initialize
testMap["a"]=4;
testMap["b"]=5;
testMap["c"]=6;
testMap["d"]=7;
testMap["e"]=8;
testMap["f"]=9;
testMap["g"]=10;
testMap["h"]=11;
testMap["k"]=12;
testMap["l"]=13;
int firstSize = testMap.size();
cout << "Before the search for missing key m, we have testMap.size() = " << firstSize << endl;
string s1, s2;
s2 = "m";
s1 = "a";
int j1, j2;
if(testMap.find(s2) == testMap.end() )
{
cout << "Key s2 = " << s2 << " is missing, so we don't try to access that element!" << endl;
}
else
{
j2 = (testMap.find(s2))->second;
cout << "s2 = " << s2
<< "But after this questionable lookup of s2, we get testMap[2] = "
<< j2 << endl;
cout << "And currently testMap.size() = " << testMap.size() << endl;
}
if( testMap.find(s1) != testMap.end() )
{
j1 = (testMap.find(s1))->second;
cout << "s1 = " << s1
<< " ,and after this acceptable lookup of s1 we get testMap[s1] = "
<< j1 << endl;
cout << "And currently we still have testMap.size() = " << testMap.size() << endl << endl ;
}
// Other possible safeguard: use the ::count() function to check that the key exists in the fist place:
// Write: if(testMap.count(s) >0) { j2=testMap[s] ; }
cout << "Here is the result of the same code written with the count() function instead of find():" << endl;
if(testMap.count(s2) < 1)
{
cout << "Key s2 = " << s2 << " is missing so we don't try to access that element!" << endl;
}
else
{
j2 = (testMap.find(s2))->second;
cout << "s2 = " << s2
<< "But after this questionable lookup of s2, we get testMap[2] = "
<< j2 << endl;
cout << "And currently testMap.size() = " << testMap.size() << endl;
}
if( testMap.count(s1) > 0 )
{
j1 = (testMap.find(s1))->second;
cout << "s1 = " << s1
<< " ,and after this acceptable lookup of s1 we get testMap[s1] = "
<< j1 << endl;
cout << " And currently testMap.size() = " << testMap.size() << endl<< endl ;
}
cout << "But finally we note that the size of a map changes if we access a missing key: " << endl;
j2 = testMap[s2] ; // Questionable attempt to find a missing key's value
cout <<"After this questionable attempt j2 = testMap[s2], testMap.size() = "
<< testMap.size() << endl<< endl;
cout << "Now we test what happens to a C++ array size if we try to access an element that doesn't exist:"
<< endl;
double x = my_array[7];
cout << "Size of my_array despite trying to access a missing element remains: " << my_array.size() << endl;
cout << "But x= " << x << endl;
cout << "But despite this questionable attempt to access a missing element we still have my_array: "<< endl;
for( auto it= my_array.begin(); it != my_array.end(); ++it)
{
cout << *it << endl;
}
}
OUTPUT :
Before the search for missing key m, we have testMap.size() = 10
Key s2 = m is missing, so we don't try to access that element!
s1 = a ,and after this acceptable lookup of s1 we get testMap[s1] = 4
And currently we still have testMap.size() = 10
Here is the result of the same code written with the count() function instead of find():
Key s2 = m is missing so we don't try to access that element!
s1 = a ,and after this acceptable lookup of s1 we get testMap[s1] = 4
And currently testMap.size() = 10
But finally we note that the size of a map changes if we access a missing key:
After this questionable attempt j2 = testMap[s2], testMap.size() = 11
Now we test what happens to a C++ array size if we try to access an element that doesn't exist:
Size of my_array despite trying to access a missing element remains: 5
But x= 6.90219e-310
But despite this questionable attempt to access a missing element we still have my_array:
1
2
3
4
5