2

Is there a difference between these maps? When should each of them be used?

const map<string, vector<unsigned char>> map1;
const map<const string, const vector<unsigned char>> map2;
const map<const string, const vector<const unsigned char>> map3;
map<const string, const vector<unsigned char>> map4;
map<const string, const vector<const unsigned char>> map5;
Tirafesi
  • 1,297
  • 2
  • 18
  • 36
  • Since [`std::map`](https://en.cppreference.com/w/cpp/container/map) is ordered the key is constant anyway, you don't have to specify it. – Some programmer dude Dec 09 '19 at 14:44
  • 2
    You can add/delete stuff from a map of const things? – AndyG Dec 09 '19 at 14:44
  • vectors with a `const` type aren't really useful ([and not legal?](https://stackoverflow.com/questions/6954906/does-c11-allow-vectorconst-t)). I'm not sure about maps though – kmdreko Dec 09 '19 at 15:10

1 Answers1

2

Having a const container means the container is immutable. You can't change it.

Having a non-const container means the container is mutable. You can change it.

Changing a map means: adding to it, modifying the things it contains (if they are, themselves, mutable), removing from it.

By the way, putting const on a map key is largely pointless.

I'm not going to enumerate "when should each be used"; you use the right tool for the job, and there are a near-infinite number of possible jobs.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055