0
map<string, int> Employees; 

Employees.insert(map<string,int>::value_type("John A.",7582));

what does the value_type do?

what is the difference between pair() and make_pair()

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by johndoe444
1

map<string, int> Employees;

Employees.insert(map<string,int>::value_type("John A.",7582));

what does the value_type do?

what is the difference between pair() and make_pair()

The std::map<Key, Value>::value_type is just a typedef to std::pair<Key, Value>. So, there is no difference between them.

And the make_pair() is defined like the form:

template <typename T, typename U>
std::pair<T, U> make_pair(const T& first, const T& second) {
    return std::pair<T, U>(first, second);
}

When you need a pair, the make_pair() function can be shorter than pair class, and less error-prone:

empolyees.insert(pair<string,int>("John A.", 7582));
empolyees.insert(make_pair("John A.", 7582));

Edited by Reverend Jim: Fixed formatting

0

The std::map<Key, Value>::value_type is just a typedef to std::pair<Key, Value>. So, there is no difference between them.

And the make_pair() is defined like the form:

template <typename T, typename U>
std::pair<T, U> make_pair(const T& first, const T& second) {
    return std::pair<T, U>(first, second);
}

When you need a pair, the make_pair() function can be shorter than pair class, and less error-prone:

empolyees.insert(pair<string,int>("John A.", 7582));
empolyees.insert(make_pair("John A.", 7582));

end quote.

Very good. I gave you an up vote.

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.