This is my first time working with pairs, totally confused.
How to initialize a pair as to insert it in the map?
Should I include some standard library for this?
#include <string>
#include <map>
using namespace std;
class Roads
{
public:
map< pair<string,string>, int > Road_map;
void AddRoad( string s, string d )
{ int b = 2 ; Road_map.insert( pair<s,d>, b) ; } //pair<s,d> is wrong here.
};
You can use
std::make_pair:Alternatively, you can construct an
std::pairlike so:The
std::make_pairapproach saves you having to name the types ofsandd.Notice that the appropriate function here is
operator[]and notinsert.std::map::inserttakes a single argument which is astd::paircontaining the key and value you want to insert. You would have to do that like this:You can make this a bit prettier with
typedef: