Function library find () does not return if found or not

4

I'm writing a program for a college exercise and I'm not sure about find() . I must enter two integers, check if the first number exists within the set already defined previously. If it exists, I should insert the second integer in set .

Then I did:

cin >> valor >> valord;
if (di.find(valor))
   di.insert(valor);

Where di is my set defined already with filled values.

You are giving the following error:

  

[Error] could not convert 'di.std :: set > find, std :: allocator> (( (const key_type ) (& value ) ')' from 'std :: set :: iterator {aka std :: _ Rb_tree_const_iterator}' to 'bool'

Can anyone explain to me if the function find returns true or false ?

    
asked by anonymous 14.10.2018 / 00:04

2 answers

4

This function does not return a boolean, as documentation returns an iterator with the position found , or the position after the end if nothing is found. So I should do something like this:

if (di.find(valor) != di.end()) di.insert(valord);
    
14.10.2018 / 00:14
4

There are 2 errors in the code.

// errado
cin >> valor >> valord;
if (di.find(valor)) // erro de sintaxe: find retorna um "iterator" e não um bool
  di.insert(valor); // erro lógico: deve ser "valord"

// certo
cin >> valor >> valord;
if (di.find(valor) != di.end()) // <----
  di.insert(valord);            // <----
    
14.10.2018 / 01:37