0

here is my code snippet...

'''

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        map<pair<int ,int>, int>  m;
        m[{1,2}]=1;
        cout<<m[{1,2}];
        //  map<pair<int,int>,pair<int,int> > k;
        //  k[{2,3}]=make_pair(1,2);
       //   cout<<k[{2,3}];
       map<int,pair<int,int> > s;
       s[12]={1,23};
       cout<<s[12]<<endl;
    }

'''

now when I tried to bind pair to int it worked fine .. but when I reversed it and tried to bind int with pair<int,int> it failed.. and is giving the error

13 11 D:\dec\cf\Untitled2.cpp [Error] cannot bind 'std::ostream {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&'

39 0 D:\dev c++\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\istream In file included from D:/dev c++/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/istream

38 D:\dev c++\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\sstream from D:/dev c++/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/sstream

45 D:\dev c++\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\complex from D:/dev c++/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/complex

38 D:\dev c++\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\ccomplex from D:/dev c++/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ccomplex

52 D:\dev c++\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\x86_64-w64-mingw32\bits\stdc++.h from D:/dev c++/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/stdc++.h

Now can anyone tell me why this is happening and what different I can do to to handle situations where I need to bind int with pair of ints like in directed graph and traverse peacefully.

Also I also tried s[12].first/s[12]->first but it didnt work.

  • Ps: Iam not asking for binding pairs to int but to bind int to pairs as my question is tagged as duplicate and linked to another question which was about using pairs as key. – Suryansh Awasthi Dec 31 '20 at 12:07
  • it worked actually, what didn't work is outputting pair through chain stream, there is no operator<< defined for tuple-like objects(wasn't possible until C++20). You can use structured binding on `s[12]` and output result. – Swift - Friday Pie Dec 31 '20 at 12:11
  • whoever marked this as dupe, linked incorrect question. – Swift - Friday Pie Dec 31 '20 at 12:13
  • `s[12].first` should have worked, but then you repeat [] twice. `auto [a,b] = s[12]; cout << a << "," << b < – Swift - Friday Pie Dec 31 '20 at 12:17
  • If you provide a `ostream& operator<<(ostream& out, pair const& p)` the error about not having that function will go away. – Eljay Dec 31 '20 at 14:59

0 Answers0