avatar
p*g
1
Since std::set::iterator only support ++ and --, i write a piece of code to do
the + (and -) functionality.
the code is below:
set::const_iterator& operator+(set::const_iterator& it, const int& n
)
{
for (int i=0; ireturn it;
}
it works fine. But only for set. I want to write a generic function, the
code is
template
set::const_iterator& operator+(set::const_iterator& it, const int& n)
{
for (int i=0; ireturn it;
}
Even more gene
avatar
t*q
2
why not use the built-in "advance" function?

do
n
the

【在 p******g 的大作中提到】
: Since std::set::iterator only support ++ and --, i write a piece of code to do
: the + (and -) functionality.
: the code is below:
: set::const_iterator& operator+(set::const_iterator& it, const int& n
: )
: {
: for (int i=0; i: return it;
: }
: it works fine. But only for set. I want to write a generic function, the

avatar
p*g
3
yeah right, surely you can use it but what if there is no advance?
hehe just for the sake of pedagogy, how to solve the problem?
I come up with an ugly solution
template
struct MyIter : T::iterator {
MyIter(const T& a) : T::iterator(a.begin()) {};
MyIter& operator+(const int& n);
};
template
MyIter& MyIter::operator+(const int& n)
{
for (int i=0; ireturn *this;
}
Any better idea? it seems I can't use "T::iterator &" why?

【在 t*q 的大作中提到】
: why not use the built-in "advance" function?
:
: do
: n
: the

avatar
p*o
4
you need 'typename', i.e,
typename std::set::const_iterator &it

do
n
the

【在 p******g 的大作中提到】
: Since std::set::iterator only support ++ and --, i write a piece of code to do
: the + (and -) functionality.
: the code is below:
: set::const_iterator& operator+(set::const_iterator& it, const int& n
: )
: {
: for (int i=0; i: return it;
: }
: it works fine. But only for set. I want to write a generic function, the

avatar
p*g
5
cool.
thanks that resolved the problem.

【在 p***o 的大作中提到】
: you need 'typename', i.e,
: typename std::set::const_iterator &it
:
: do
: n
: the

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。