Question: I'm new to c++ and have a simple program as below

```
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main()
{
 std::map<std::string, double*> testMap;
 std::vector<std::string> v = {"A", "B", "C"};
 for(size_t i=0;i<v.size();++i)
 {
   std::string sym = v[i];
   double* d = testMap[sym];
   if(!d) continue;
   cout << "this";
 }
 for(std::map<std::string, double*>::iterator  itr=testMap.begin();itr!=testMap.end();++itr)
 {
    cout << itr->first << " " << itr->second << std::endl;
 }

 return 0;
}

```

and its O/P is 

```
sh-4.3$ main                                                                                                                                                  
A 0                                                                                                                                                           
B 0                                                                                                                                                           
C 0  

```

My question is why map is creating new elements with null double pointers

Answer: 
When you write `testMap[sym]` a check is done whether `sym` exists in map, and if it does then reference to it is returned. If it does not then new entry value initialized is inserted into map with key `sym`, in your case it is a null pointer.

So if you dont want to have null vaules, then check if key exists with [std::map::find](http://en.cppreference.com/w/cpp/container/map/find), and compare to `std::map::end()`, instead of using `std::map::operator[]`

> 
> My question is why map is creating new elements with null double pointers
> 
> 
> 

this is how standard defines behaviour of [std::map::operator[]](http://en.cppreference.com/w/cpp/container/map/operator_at)

n4140 23.4.4.3 map.access

> 
> T& operator[](const key\_type& x);
> 
> 
> Effects: If there is no key equivalent to x in the map, inserts
>  value\_type(x, T()) into the map.
> 
> 
> 

and:

> 
> T& operator[](key\_type&& x);
> 
> 
> Effects: If there is no key equivalent to x in the map, inserts value\_type(std::move(x), T()) into
>  the map.
> 
> 
>
That is the way maps works.
The map must be check if the key is all raedy containt.
Use find(key) for that. In your excample testMap[sym]; creates a new entry.

```
std::map<std::string,double*>::iterator it;
it = testMap.find("A");
if ( it == testMap.end() )
{ // create a new entry
   testMap["A"] = new double(1.0);
}

```

Match fun
Hilko