compilation error with merge algorithm

S

subramanian100in

Consider the following program:

#include <cstdlib>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
#include <utility>

using namespace std;

int main()
{
typedef pair<int, int> p_type;

multiset<p_type> c1;

c1.insert(make_pair(0, 2));
c1.insert(make_pair(0, 0));
c1.insert(make_pair(0, 1));
c1.insert(make_pair(2, 3));

multimap<int, int> c3;

c3.insert(make_pair(5, 0));
c3.insert(make_pair(0, 0));
c3.insert(make_pair(3, 2));
c3.insert(make_pair(0, -1));
c3.insert(make_pair(1, -1));

multimap<int, int> dest;

merge(c1.begin(), c1.end(), c3.begin(), c3.end(),
inserter(dest, dest.begin()));

return EXIT_SUCCESS;
}

Suppose the above program is named z.cpp and is compiled as
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp

Then I get compilation error for the line
merge(...)

z.cpp:32: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/stl_algo.h:2979: error: no match for 'operator<' in '(&__first2)-
std::_Rb_tree_iterator<_Tp>::eek:perator* [with _Tp = std::pair<const int, int>]() < (&__first1)-
std::_Rb_tree_const_iterator<_Tp>::eek:perator* [with _Tp =
main()::p_type]()'

Kindly help me to fix this compilation error.

Thanks
V.Subramanian
 
B

Barry

Consider the following program:

#include <cstdlib>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
#include <utility>

using namespace std;

int main()
{
typedef pair<int, int> p_type;

multiset<p_type> c1;

c1.insert(make_pair(0, 2));
c1.insert(make_pair(0, 0));
c1.insert(make_pair(0, 1));
c1.insert(make_pair(2, 3));

multimap<int, int> c3;

c3.insert(make_pair(5, 0));
c3.insert(make_pair(0, 0));
c3.insert(make_pair(3, 2));
c3.insert(make_pair(0, -1));
c3.insert(make_pair(1, -1));

multimap<int, int> dest;

merge(c1.begin(), c1.end(), c3.begin(), c3.end(),
inserter(dest, dest.begin()));

return EXIT_SUCCESS;
}

Suppose the above program is named z.cpp and is compiled as
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp

Then I get compilation error for the line
merge(...)

z.cpp:32: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/stl_algo.h:2979: error: no match for 'operator<' in '(&__first2)-
std::_Rb_tree_iterator<_Tp>::eek:perator* [with _Tp = std::pair<const int, int>]() < (&__first1)-
std::_Rb_tree_const_iterator<_Tp>::eek:perator* [with _Tp =
main()::p_type]()'

Kindly help me to fix this compilation error.

typeof "multiset<p_type>::value_type" == p_type == pair<int, int> ... Type1
typeof "multimap<int, int>::value_type" == pair<const int, int> ... Type2

Type1. Type2 are two different specialization of template class pair

so there are no specialization of
template <class T1, class T2>
bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);

while the 5-arg merge needs operator<
take a look at the implementation of merge.
 
B

Barry

Barry said:
Consider the following program:

#include <cstdlib>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
#include <utility>

using namespace std;

int main()
{
typedef pair<int, int> p_type;

multiset<p_type> c1;

c1.insert(make_pair(0, 2));
c1.insert(make_pair(0, 0));
c1.insert(make_pair(0, 1));
c1.insert(make_pair(2, 3));

multimap<int, int> c3;

c3.insert(make_pair(5, 0));
c3.insert(make_pair(0, 0));
c3.insert(make_pair(3, 2));
c3.insert(make_pair(0, -1));
c3.insert(make_pair(1, -1));

multimap<int, int> dest;

merge(c1.begin(), c1.end(), c3.begin(), c3.end(),
inserter(dest, dest.begin()));

return EXIT_SUCCESS;
}

Suppose the above program is named z.cpp and is compiled as
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp

Then I get compilation error for the line
merge(...)

z.cpp:32: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/stl_algo.h:2979: error: no match for 'operator<' in '(&__first2)-
std::_Rb_tree_iterator<_Tp>::eek:perator* [with _Tp = std::pair<const int, int>]() < (&__first1)-
std::_Rb_tree_const_iterator<_Tp>::eek:perator* [with _Tp =
main()::p_type]()'

Kindly help me to fix this compilation error.

typeof "multiset<p_type>::value_type" == p_type == pair<int, int> ... Type1
typeof "multimap<int, int>::value_type" == pair<const int, int> ... Type2

Type1. Type2 are two different specialization of template class pair

so there are no specialization of
template <class T1, class T2>
bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);

while the 5-arg merge needs operator<
take a look at the implementation of merge.

"merge" also requires the two value_type to be the same type.
 
C

Christian Hackl

int main()
{
typedef pair<int, int> p_type;

multiset<p_type> c1;

c1.insert(make_pair(0, 2));
c1.insert(make_pair(0, 0));
c1.insert(make_pair(0, 1));
c1.insert(make_pair(2, 3));

multimap<int, int> c3;

c3.insert(make_pair(5, 0));
c3.insert(make_pair(0, 0));
c3.insert(make_pair(3, 2));
c3.insert(make_pair(0, -1));
c3.insert(make_pair(1, -1));

multimap<int, int> dest;

merge(c1.begin(), c1.end(), c3.begin(), c3.end(),
inserter(dest, dest.begin()));

return EXIT_SUCCESS;
}

[...]

z.cpp:32: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/stl_algo.h:2979: error: no match for 'operator<' in '(&__first2)-
std::_Rb_tree_iterator<_Tp>::eek:perator* [with _Tp = std::pair<const int, int>]() < (&__first1)-
std::_Rb_tree_const_iterator<_Tp>::eek:perator* [with _Tp =
main()::p_type]()'

The value type of your multimap is actually std::pair<const int, int>.
However, std::pair<const int, int> and std::pair<int, int> are unrelated
types. The basic problem of type incompatibility can be illustrated by a
simpler example:

#include <utility>

int main()
{
std::pair<const int, int> a;
std::pair<int, int> b;

a < b; // error
}

Depending on how big your containers really are and how often that piece
of code is executed, a viable solution may be copying your
std::pair<const int, int> elements from the multimap into a new range of
std::pair<int, int> and use that one in std::merge.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top