-2
std::unordered_map<std::string, std::string> url;

url.insert(std::pair<std::string,std::string>("google","www.google.co.kr"));

bool ok = url.insert(std::pair<std::string,std::string>("google","www.google.com")).second;
    std::cout << (ok ? "susses" : "failed") << "\n";

This code save short URL and original URL in unordered_map.

In line 3, if I don't use '.second', so this line does not return boolean value.

vscode explain "/@c second is a copy of the second objet".

I want to know what ".second" does and what value it returns. and why a boolean value is reuturned by typing ".second".

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Kwon34
  • 11
  • 4
    [`std::unordered_map::insert`](https://en.cppreference.com/w/cpp/container/unordered_map/insert) returns a [`std::pair`](https://en.cppreference.com/w/cpp/utility/pair). The `second` member of that pair describes whether or not the insertion took place. – 0x5453 Jun 29 '21 at 14:39

2 Answers2

1

I would strongly suggest you to read the documentation for std::unordered_map::insert.

If you look closely, you'll see that when inserting a value_type the insert function will return a pair. You can see that std::unordered_map::value_type is a pair in the std::unordered_map documentation.

That pair contain an iterator and a boolean, to flag if the insertion was done or not.

It can be used like this:

auto [iterator, ok] = url.insert(std::pair<std::string,std::string>("google","www.google.com"));
std::cout << (ok ? "susses" : "failed") << "\n";

if (ok) {
    //                       yields google                  yield the address
    //                      v-------------v                  v--------------v
    std::cout << "key: " << iterator->first << " value: " << iterator->second << "\n";
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • Thank you. It seems like a question that arises from not understanding the return value of insert properly. Now I understand somewhat. I've read it several times, but I don't think I understand it well because there are still many things I don't know. Next time I will read more and ask questions. – Kwon34 Jun 29 '21 at 14:57
  • @Kwon34 Is there a specific thing I can add to the answer to help you? – Guillaume Racicot Jun 29 '21 at 15:18
0

The used by you overloaded member function insert is declared like

pair<iterator, bool> insert(const value_type& x);
pair<iterator, bool> insert(value_type&& x);

that is it returns an object of the type std::pair<iterator, bool>. It means that the data member second of the pair has the type bool. This data member of the object of the type std::pair<iterator, bool> reports whether the insertion was successful.

Pay attention to that in this statement

bool ok = url.insert(std::pair<std::string,std::string>("google","www.google.com")).second;

you are trying to insert a record with the same key "google" of the previously inserted record in the map.

Here is your code snippet formed as a program.

#include <iostream>
#include <string>
#include <unordered_map>

int main() 
{
    std::unordered_map<std::string, std::string> url;

    bool ok = url.insert(std::pair<std::string,std::string>("google","www.google.co.kr")).second;
    std::cout << (ok ? "success" : "failed") << "\n";

    ok = url.insert(std::pair<std::string,std::string>("google","www.google.com")).second;
    std::cout << (ok ? "success" : "failed") << "\n";
    
    return 0;
}

The program output is

success
failed
halfer
  • 19,824
  • 17
  • 99
  • 186
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335