-5

This is an example from the e-book "Jumping into C++" by Alex Allain which I downloaded from here. On page 207 he has this code snippet:

#include <map>
#include <string>
using namespace std;
map<string, string> name_to_email;

My question is, please, what is the meaning of the last line, in particular the significance of the "<" and ">". Can the line be written map < string, string > name_to_email; i.e. must here be no spaces as I have inserted them?

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
Harry Weston
  • 696
  • 8
  • 22
  • 4
    This question appears to be off-topic because it is too basic and can be found in any basic books/tutorials. – haccks Sep 09 '14 at 14:43
  • 1
    The `map` is a `template class`. Look up `templates` in a tutorial book. – Galik Sep 09 '14 at 14:44
  • Templates are rather too big a subject to cover here. If your book doesn't cover them, find one that does [here](http://stackoverflow.com/questions/388242). – Mike Seymour Sep 09 '14 at 14:57
  • Isn't this explained in the text of that book, somewhere before page 207? If it isn't, you should take advantage of your money-back guarantee. – molbdnilo Sep 09 '14 at 15:12
  • Sorry about that, it was not in the index, no idea where to start, and having to plough through all that material that prompted me to ask here. GR Envoy's answer put me on the right track. Very grateful to all those who answered without quibbling. I'm happy. You can delete it all now if appropriate – Harry Weston Sep 10 '14 at 18:25

3 Answers3

4

That notation specifies the template parameters.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
3

Can the line be written map < string, string > name_to_email; i.e. must here be no spaces as I have inserted them?

Spaces are fine.

My question is, please, what is the meaning of the last line, in particular the significance of the "<" and ">".

As @Cyber mentioned, they're template parameters. It's the way of using variable types in a C++. Rather than having a map for every type, like a StringToIntMap and a StringToCharMap and a CharToStringMap and a StringToStringMap etc. There's just a map, which can use any type. So a map<string, int> is essentially a map that takes a string as a key and maps it to an int as a value.

OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
2

As Cyber noted, the notation specifies the template parameters. If you read this link to get an understanding of what the map is, you can see that when you are defining a map, you need to specify the two parts of the map. The key, and the value. In your example above you are creating a map of strings, that are accessed by a string key.

map<key, value>. So in another answer above, if you wanted to store integers, accessible by a string key, you would create a map like this map<string, int> lMyMap

Brian S
  • 3,096
  • 37
  • 55