通用间接适配器 [英] Generic indirection adaptor

查看:78
本文介绍了通用间接适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




由于我上次发布有关适应类模板的结果,因为
调用迭代器容器上的算法或指向元素的指针<另一个容器中的
,我已经提出了这个代码:


#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()(它lhs,它是rhs)const {

返回Pred()(* lhs,* rhs);

} < br $>
};


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 * GT; v2;

v2.push_back(& v1 [0]);

v2.push_back(& v1 [1]);


indirect_binary< int *,std :: less< int> > fctor; //注意int *!

std :: sort(v2.begin(),v2.end(),fctor);


std :: vector< int *> :: iterator it = v2.begin();

for(; it!= v2.end(); ++ it)

{

std :: cout<< **它<< std :: endl;

}

}


嗯,它有效,输出为:2,5。

但是,您可能已经注意到我使用int *

而不是IntVecIt实例化了适配器。如果我做后者,我会得到大量的错误信息:


deref.cpp:在函数`int main()''中:

deref.cpp :25:错误:没有匹配函数来调用`

std :: vector< main():: IntVecIt,std :: allocator< main():: IntVecIt>

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 `
std::vector<main()::IntVecIt, std::allocator<main()::IntVecIt>

:: push_back(
int *)''

/usr/include/c++/3.3/bits/stl_vector.h:596:错误:候选人是:void

std :: vector< _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp =

main():: IntVecIt,_Alloc = std :: allocator< main() :: IntVecIt>]

deref.cpp:26:错误:没有匹配函数来调用`

std :: vector< main():: IntVecIt,std: :allocator< main():: IntVecIt> :: push_back(
::push_back( int*)''
/usr/include/c++/3.3/bits/stl_vector.h:596: error: candidates are: void
std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
main()::IntVecIt, _Alloc = std::allocator<main()::IntVecIt>]
deref.cpp:26: error: no matching function for call to `
std::vector<main()::IntVecIt, std::allocator<main()::IntVecIt>::push_back(



int *)''

/usr/include/c++/3.3/bits/ stl_vector.h:596:错误:候选人是:void

std :: vector< _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp =

main ():: IntVecIt,_Alloc = std :: allocator< main():: IntVecIt> ;]

deref.cpp:31:错误:转换自`

__gnu_cxx :: __ normal_iterator< main():: IntVecIt *,

std :: vector< main():: IntVecIt,std :: allocator< main():: IntVecIt> > >''

非标量类型`__gnu_cxx :: __ normal_iterator< int **,std :: vector< int *,

std :: allocator< int * > > >''要求

deref.cpp:32:错误:''运算符!=''在''中没有匹配!= std :: vector< _Tp,

_Alloc> :: end()[with _Tp = main():: IntVecIt,_Alloc =

std :: allocator< main():: IntVecIt>]()''


还有一个问题:为什么我必须按价值获取lhs和rhs

operator()?如果我声明它采用const引用,它也不会编译




我会很高兴你的所有输入,因为我打算使用这个模板

类一旦完成就会经常使用,所以我希望它能够像

一样健壮。


-

Matthias Kaeppler


int*)''
/usr/include/c++/3.3/bits/stl_vector.h:596: error: candidates are: void
std::vector<_Tp, _Alloc>::push_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.

--
Matthias Kaeppler

推荐答案

Matthias Kaeppler写道:
Matthias Kaeppler wrote:

#include< vector>
#include< functional>
#include< algorithm>
#include< iostream>

模板< typename它,typename Pred>
class indirect_binary:public std :: binary_function< It,it,bool>
{
public:
bool operator()(它lhs,It rhs)const {
返回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; //注意int *!
std :: sort(v2.begin(),v2.end(),fctor);

std :: vector< int *> :: iterator it = v2.begin();
for(; it!= v2.end(); ++ it)
{
std :: cout<< **它<< std :: endl;
}


嗯,它的工作原理,输出是:2,5。
但是,您可能已经注意到我实例化了适配器使用int *
而不是IntVecIt。如果我执行后者,我会收到大量的错误消息:

您的比较运算符已经通过已取消引用的迭代器,

而不是迭代器本身。

另一个问题:为什么我必须在
operator()中按值获取lhs和rhs?如果我声明它采用const引用,它也不会编译
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:
Your comparison operators gets passed already-dereferenced iterators,
not the iterators themselves.
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.




奇怪,我将你的比较函数更改为:


bool operator()(const它& lhs,const It& rhs)const {


它运行良好......

你的意思是什么?


Chris



Strange, I changed your comparison function to:

bool operator() (const It& lhs, const It& rhs) const {

and it worked fine...

Did you mean something else?

Chris


Chris Jefferson写道:
Chris Jefferson wrote:
您的比较运算符已经通过已取消引用的迭代器,而不是迭代器本身。
Your comparison operators gets passed already-dereferenced iterators,
not the iterators themselves.




你的意思是什么?在大多数情况下,对于int *

的类型定义是不是在Into?至少这是我的实施。所以:


bool operator()(它是lhs,它是rhs){

返回Pred()(* lhs,* rhs);

}


转换为:


bool operator()(int * lhs,int * rhs){

返回Pred()(* lhs,* rhs);

}


我看不出有什么问题。


-

Matthias Kaeppler



What do you mean? Isn''t IntVecIt in most cases just a typedef for int*
anyway? At least it is on my implementation. So:

bool operator() (It lhs, It rhs) {
return Pred() (*lhs, *rhs);
}

translates to:

bool operator() (int* lhs, int* rhs) {
return Pred() (*lhs, *rhs);
}

I can''t see what''s wrong with that.

--
Matthias Kaeppler


Matthias Kaeppler写道:
Matthias Kaeppler wrote:
Chris Jefferson写道:
Chris Jefferson wrote:
你的比较运算符已经通过已经解除引用的迭代器,而不是迭代器本身。
Your comparison operators gets passed already-dereferenced iterators,
not the iterators themselves.



什么你的意思是?在大多数情况下,无论如何都不是int *
的typedef吗?至少这是我的实施。所以:


What do you mean? Isn''t IntVecIt in most cases just a typedef for int*
anyway? At least it is on my implementation. So:




这不是我的实现(gcc 3.3.3)。假设它是一个很大的

no-no!


然而,您可以将迭代器赋予向量< int>做& *它并得到一个

int *。我不肯定这是标准的保证,我记得

看到有关它的缺陷报告,不记得它是否被接受。

我'从来没有见过没有工作的实施。


Chris



It isn''t on my implementation (gcc 3.3.3). Assuming that it is is a big
no-no!

You can however given an iterator into a vector<int> do &*it and get an
int*. I''m not positive this is guaranteed by the standard, I remember
seeing a defect report about it and can''t remember if it got accepted.
I''ve never seen an implementation where it didn''t work.

Chris


这篇关于通用间接适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆