0
#include <bits/stdc++.h>
using namespace std;

int main()
{
    
  pair<int,int>p = {1,3};

  if (p=={1,3})
      cout << "yeyey\n";

}

I want to check whether this pair is same as the integer inside the braces, but it returns error

"expected unqualified-id before ')' token if (p=={1,3}) cout << "yeyey\n";"

can someone explain why?

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 6
    Warning: `#include ` [loads the gun](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). `using namespace std;` [takes off the safety](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). After that, [it's really easy for the program to shoot itself](https://godbolt.org/z/6sWGov). Use with caution, but prefer to not use at all. – user4581301 Sep 25 '20 at 17:37
  • 2
    omg people stop using bits/stdc++.h!!! – Asteroids With Wings Sep 25 '20 at 17:37
  • mostly used for short example code or in competetive programming if you need to write really fast – Yamahari Sep 25 '20 at 18:04
  • @Yamahari -- With the advent of C++ 17, having that `bits` header, plus `using namespace std;` will lead to *very strange* errors if the program uses a variable called [data](https://en.cppreference.com/w/cpp/iterator/data). Note that `data` isn't the only name that will cause these issues. All of that time "competing" will now be spent scratching heads as to why the program doesn't compile. – PaulMcKenzie Sep 25 '20 at 18:45
  • sorry, but I used those bits/stdc++ for competitive programming, btw thanks for the suggestion! – Marcello Faria Sep 25 '20 at 18:55

5 Answers5

1

The way the language works {1, 3} in that context cannot be resolved to a type so you need:

// since c++17
p == std::pair{1, 3}

// before
p == std::make_pair(1, 3)
bolov
  • 72,283
  • 15
  • 145
  • 224
1

The type of {1,3} cannot be used to deduce a std::pair<K,T> directly. You'll need to use std::make_pair() to overcome this:

#include <utility>
#include <iostream>

int main() {
    
  std::pair<int,int>p = {1,3};

  if (p==std::make_pair(1,3))
      std::cout << "yeyey\n";    
}

Since c++17 you can alternatively use a temporary with a direct initializer for std::pair<T,K>:

  if (p==std::pair{1,3})

See a online demo here.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

To be able to use the operator == you need to compare two pairs, but {1,3} is not a pair. To do so you need to cast the object into a pair. Just like you would cast an integer to a float float f = (float)123. therefor the correct way to do so is this:

#include <iostream>
using namespace std;

int main(void)
{
    pair<int, int> p = { 1, 3 };
    if (p == (pair<int,int>{1, 3}))
        cout << "yay!" << endl;
}

Also please note that specifying using namespace std; is a bad habit and should not be used.

Raz Kissos
  • 295
  • 1
  • 7
  • Note: `if (p == (pair{1, 3}))` didn't cast. It created a temporary `pair` to compare against. This is the right solution, but casting plays no part in it. – user4581301 Sep 25 '20 at 17:56
0

Well I am no expert in STL still can tell, you cannot initialize a pair like that. You have to either call its parameterized constructor or make_pair() function template.

pair <int , int> p(1, 3);    //constructor one
pair <int , int> p = make_pair(1, 3);    // function template

Of course can do this.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    
  pair<int,int>p;
  p = {1,3}; // This here is actually the overloaded assignment operator which is copying
}

Also you can not compare your pair with {1, 3} Use:

if(p == make_pair(1,3)) // this 
if (p.first == 1 && p.second == 3) // or this

You should read documentation for if you want to know some internal structure because I am not much aware of it.

DK_bhai
  • 337
  • 3
  • 14
  • Recommendation: Don't put `#include ` in an answer and avoid putting `using namespace std;` in an answer. Both have bad effects individually and together they can combine to form a program-destroying megazord. Basically, it's stuff you don't leave lying around where some kid could find it. – user4581301 Sep 25 '20 at 17:59
  • I will take care next time. – DK_bhai Sep 25 '20 at 18:06
0

You can also use init-statement in if + structured bindings to do this in C++17:

#include <utility>

int main()
{
    std::pair<int, int> p{1, 2};
    if(auto &[x0, x1] = p; x0 == 1 && x1 == 2)
        return 0;
    return 1;
}
Yamahari
  • 1,926
  • 9
  • 25