Suppose some data structure:
typedef struct {
std::string s;
int i;
} data;
If I use the field data.s
as key when adding instances of data
in a map of type std::map<std::string&, data>
, do the string gets copied? Is it safe to erase an element of the map because the reference will become invalid?
Also do the answers to these questions also apply to an unordered_map
?
EDIT:
This is my current solution... but adding iterator to the map is UGLY:
typedef struct {
const std::string* s;
int i;
} data;
std::map<std::string, data> map;
typedef std::map<std::string, data>::iterator iterator;
// add an element to the map
iterator add_element(const std::string& s) {
std::pair<iterator, bool> p = states.insert(std::make_pair(s, data()));
iterator i = p.first;
if(p.second) {
data& d = (*i).second;
d.s = &(*i).first;
}
return i;
}
The reason why you can't store them in a map is purely semantic; you have to initialize a reference when it's created and you can't change it afterward anymore. This doesn't mesh with the way a map works.
A C++ map is a way to store a key-value pair. A map can be declared as follows: #include <iostream> #include <map> map<int, int> sample_map; Each map entry consists of a pair: a key and a value.
C++ standard provided specialisation of std::less for pointers, so yes you can safely use them as map keys etc.
Yes -- when you insert an item into an std::map, you pass it by value, so what it contains is a copy of what you passed.
C++11
Since C++11 reference wrapper is part of standard.
#include <functional>
std::map<std::reference_wrapper<std::string>, data>
Using Boost
You may want to take a look at boost.ref. It provides a wrapper that enables references to be used in STL-containers like this:
std::map<boost::reference_wrapper<std::string>, data>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With