Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- using namespace std;
- void PrintInsertResult(const pair<map<int, string>::iterator, bool> & result) {
- auto [iter, inserted] = result;
- auto [key, value] = (*iter);
- if (inserted) {
- cout << "Successfully inserted (" << key << ", " << value
- << ") into map" << endl;
- } else {
- cout << "Value was not inserted, existing value found: ("
- << key << ", " << value << ")" << endl;
- }
- }
- //------------------------------------------------------------------------------
- int main()
- {
- map<int, string> numbers;
- cout << "insert(1, one)" << endl;
- pair<map<int, string>::iterator, bool> first_inserted =
- numbers.insert({1, "one"});
- PrintInsertResult(first_inserted);
- cout << "insert(1, uno)" << endl;
- auto duplicate_first_inserted = numbers.insert({1, "uno"});
- PrintInsertResult(duplicate_first_inserted);
- cout << "emplace(1, uno)" << endl;
- auto duplicate_first_emplaced = numbers.emplace(1, "uno");
- PrintInsertResult(duplicate_first_emplaced);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment