M
Matthias Kaeppler
Hi,
as a result of my last posting about an adaption class template for
invoking algorithms on a container of iterators or pointers to elements
in another container, I have come up with this code:
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>
template< typename It, typename Pred >
class indirect_binary: public std::binary_function<It,It,bool>
{
public:
bool operator() (It lhs, It rhs) const {
return Pred() (*lhs, *rhs);
}
};
int main()
{
typedef std::vector<int> IntVec;
typedef IntVec::iterator IntVecIt;
std::vector<int> v1;
v1.push_back(5);
v1.push_back(2);
std::vector<int*> v2;
v2.push_back( &v1[0] );
v2.push_back( &v1[1] );
indirect_binary< int*, std::less<int> > fctor; // note the int*!
std::sort( v2.begin(), v2.end(), fctor );
std::vector<int*>::iterator it = v2.begin();
for( ; it != v2.end(); ++it )
{
std::cout << **it << std::endl;
}
}
Well, it works, output is: 2, 5.
However, you may have noticed that I instantiated the adaptor with int*
instead of IntVecIt. If I do the latter, I get tons of error messages:
deref.cpp: In function `int main()':
deref.cpp:25: error: no matching function for call to `
/usr/include/c++/3.3/bits/stl_vector.h:596: error: candidates are: void
std::vector<_Tp, _Alloc>:
ush_back(const _Tp&) [with _Tp =
main()::IntVecIt, _Alloc = std::allocator<main()::IntVecIt>]
deref.cpp:26: error: no matching function for call to `
/usr/include/c++/3.3/bits/stl_vector.h:596: error: candidates are: void
std::vector<_Tp, _Alloc>:
ush_back(const _Tp&) [with _Tp =
main()::IntVecIt, _Alloc = std::allocator<main()::IntVecIt>]
deref.cpp:31: error: conversion from `
__gnu_cxx::__normal_iterator<main()::IntVecIt*,
std::vector<main()::IntVecIt, std::allocator<main()::IntVecIt> > >' to
non-scalar type `__gnu_cxx::__normal_iterator<int**, std::vector<int*,
std::allocator<int*> > >' requested
deref.cpp:32: error: no match for 'operator!=' in 'it != std::vector<_Tp,
_Alloc>::end() [with _Tp = main()::IntVecIt, _Alloc =
std::allocator<main()::IntVecIt>]()'
And another question: Why do I have to take lhs and rhs by value in
operator() ? If I declare it taking const references, it won't compile
either.
I'd be glad for all your input, because I intend to use this template
class pretty often once it is finished, so I want it to be as robust as
possible.
as a result of my last posting about an adaption class template for
invoking algorithms on a container of iterators or pointers to elements
in another container, I have come up with this code:
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>
template< typename It, typename Pred >
class indirect_binary: public std::binary_function<It,It,bool>
{
public:
bool operator() (It lhs, It rhs) const {
return Pred() (*lhs, *rhs);
}
};
int main()
{
typedef std::vector<int> IntVec;
typedef IntVec::iterator IntVecIt;
std::vector<int> v1;
v1.push_back(5);
v1.push_back(2);
std::vector<int*> v2;
v2.push_back( &v1[0] );
v2.push_back( &v1[1] );
indirect_binary< int*, std::less<int> > fctor; // note the int*!
std::sort( v2.begin(), v2.end(), fctor );
std::vector<int*>::iterator it = v2.begin();
for( ; it != v2.end(); ++it )
{
std::cout << **it << std::endl;
}
}
Well, it works, output is: 2, 5.
However, you may have noticed that I instantiated the adaptor with int*
instead of IntVecIt. If I do the latter, I get tons of error messages:
deref.cpp: In function `int main()':
deref.cpp:25: error: no matching function for call to `
int*)'std::vector said::ush_back(
/usr/include/c++/3.3/bits/stl_vector.h:596: error: candidates are: void
std::vector<_Tp, _Alloc>:
main()::IntVecIt, _Alloc = std::allocator<main()::IntVecIt>]
deref.cpp:26: error: no matching function for call to `
int*)'std::vector said::ush_back(
/usr/include/c++/3.3/bits/stl_vector.h:596: error: candidates are: void
std::vector<_Tp, _Alloc>:
main()::IntVecIt, _Alloc = std::allocator<main()::IntVecIt>]
deref.cpp:31: error: conversion from `
__gnu_cxx::__normal_iterator<main()::IntVecIt*,
std::vector<main()::IntVecIt, std::allocator<main()::IntVecIt> > >' to
non-scalar type `__gnu_cxx::__normal_iterator<int**, std::vector<int*,
std::allocator<int*> > >' requested
deref.cpp:32: error: no match for 'operator!=' in 'it != std::vector<_Tp,
_Alloc>::end() [with _Tp = main()::IntVecIt, _Alloc =
std::allocator<main()::IntVecIt>]()'
And another question: Why do I have to take lhs and rhs by value in
operator() ? If I declare it taking const references, it won't compile
either.
I'd be glad for all your input, because I intend to use this template
class pretty often once it is finished, so I want it to be as robust as
possible.