如何保证迭代器的行为? [英] How to gurantee iterator behavior?
问题描述
我已阅读有关输入,输出转发等迭代器的信息。但是如果我把
跟随下:
int myints [] = {1,2,3,4,5,1,2,3,4 ,5};
std :: vector< intmyvector(myints,myints + 10);
std :: vector< int> :: iterator forward_iterator = myvector.end( );
我仍然可以输入 - forward_iterator尽管如此 - 它
前向迭代器是非法的。
有没有办法指定迭代器,所以如果使用编译器会给出
错误不正确,就像使用关键字const时一样在一个
变量上?
我还读到find_end使用forward_iterators,但据我所知
他们提供顺序读写访问。我不明白为什么需要写入
访问权限,input_iterator(只读)不是更多
,因为find只返回未更改的信息?
I have read about input, output forward etc iterators. But if I make the
following:
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<intmyvector (myints,myints+10);
std::vector<int>::iterator forward_iterator = myvector.end();
I can still type "--forward_iterator" eventhough "--it" is illegal on
forward iterators.
Are there some way to specify an iterator so the compiler will give
errors if used incorrectly, like when using the keyword "const" on a
variable?
I also read that find_end uses forward_iterators, but as I understand
they provide sequential read-write access. I don''t see why the write
access is needed, would an input_iterator (read-only) not be more
appropriate since find only returns unchanged info?
推荐答案
desktop写道:
desktop wrote:
我已阅读有关输入,输出转发等迭代器的信息。但是如果我把
跟随下:
int myints [] = {1,2,3,4,5,1,2,3,4 ,5};
std :: vector< intmyvector(myints,myints + 10);
std :: vector< int> :: iterator forward_iterator = myvector.end( );
我仍然可以输入 - forward_iterator尽管如此 - 它在
前向迭代器上是非法的。
I have read about input, output forward etc iterators. But if I make the
following:
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<intmyvector (myints,myints+10);
std::vector<int>::iterator forward_iterator = myvector.end();
I can still type "--forward_iterator" eventhough "--it" is illegal on
forward iterators.
std :: vector< T> :: iterator支持 - 。
std::vector<T>::iterator does support --.
>
是否有某种方法来指定迭代器,因此如果使用不正确,编译器将给出
错误,例如当使用关键字const时在一个
变量?
>
Are there some way to specify an iterator so the compiler will give
errors if used incorrectly, like when using the keyword "const" on a
variable?
我想你可以创建一个迭代器类并覆盖
operator--。关于C ++ Concepts的事情有一些工作要做。
可能就是你要找的东西。我个人没有设定成本/效益
比例,但我可能错了。
I suppose you can create a class that is an iterator and override
operator--. There is some work on a thing called "C++ Concepts" that
may be what you''re looking for. I personally don''t set the cost/benefit
ratio working out but I may be wrong on this one.
>
我还读到find_end使用forward_iterators,但据我所知
他们提供顺序读写访问。我不明白为什么需要写入
访问权限,input_iterator(只读)不是更多
,因为find只返回未更改的信息?
>
I also read that find_end uses forward_iterators, but as I understand
they provide sequential read-write access. I don''t see why the write
access is needed, would an input_iterator (read-only) not be more
appropriate since find only returns unchanged info?
您可以将const_iterator发送到find_end。
You can send a const_iterator to find_end.
desktop写道:
desktop wrote:
我已阅读有关输入,输出转发等迭代器的信息。但是如果我把
跟随下:
int myints [] = {1,2,3,4,5,1,2,3,4 ,5};
std :: vector< intmyvector(myints,myints + 10);
std :: vector< int> :: iterator forward_iterator = myvector.end( );
我仍然可以输入 - forward_iterator尽管如此 - 它在
前向迭代器上是非法的。
I have read about input, output forward etc iterators. But if I make the
following:
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<intmyvector (myints,myints+10);
std::vector<int>::iterator forward_iterator = myvector.end();
I can still type "--forward_iterator" eventhough "--it" is illegal on
forward iterators.
这是因为,如果你看到类向量的文档,你就会发现向量迭代器是随机的迭代器,也就是说,它是
实现运算符++, - ,==,&,<,+ =, - =,+, - ,[](如果我>
没有忘记任何事情)。 forward_iterator只是变量的名称:)
that''s because, if you see the documentation for the class vector, you
will discover that the vector iterator is a random iterator, that is, it
implements the operators ++, --, ==, &, <, +=, -=, +, -, [] (if I
haven''t forgot any). forward_iterator is just the name of your variable :)
有没有办法指定迭代器,所以如果使用编译器会给出
错误不正确,就像使用关键字const时一样在一个
变量?
Are there some way to specify an iterator so the compiler will give
errors if used incorrectly, like when using the keyword "const" on a
variable?
不需要它。例如,如果他们告诉你函数
模板< typename iterator>
foo(iterator i);
需要一个双向迭代器,这是因为在foo内部将使用
运算符++和 - 。如果你提供了一个前向的
迭代器,编译器会抱怨,而不是找到任何操作符 - 对于
那个类。
it''s not needed. For example, if they tell you that the function
template<typename iterator>
foo(iterator i);
requires a bidirectional iterator, it''s because inside of foo both the
operators ++ and -- will be used. If you provide it with a forward
iterator, the compiler will complain, not finding any operator-- for
that class.
我还读到find_end使用forward_iterators,但据我所知
它们提供顺序读写访问。我不明白为什么需要写入
访问权限,input_iterator(只读)不是更多
,因为find只返回未更改的信息?
I also read that find_end uses forward_iterators, but as I understand
they provide sequential read-write access. I don''t see why the write
access is needed, would an input_iterator (read-only) not be more
appropriate since find only returns unchanged info?
这将是一个const_iterator。同样,但只读。
问候,
Zeppe
this would be a const_iterator. The same, but read only.
Regards,
Zeppe
Zeppe写道:
Zeppe wrote:
desktop写道:
desktop wrote:
>我已阅读有关输入,输出转发等迭代器的信息。但是,如果我做了
以下:
int myints [] = {1,2,3,4,5,1,2,3,4,5};
std :: vector< intmyvector(myints,myints + 10);
std :: vector< int> :: iterator forward_iterator = myvector.end();
我仍然可以输入 --forward_iterator"尽管如此 - 它在
前向迭代器上是非法的。
>I have read about input, output forward etc iterators. But if I make the
following:
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<intmyvector (myints,myints+10);
std::vector<int>::iterator forward_iterator = myvector.end();
I can still type "--forward_iterator" eventhough "--it" is illegal on
forward iterators.
那是因为,如果你看到类向量的文档,你将会发现向量迭代器是一个随机的
迭代器,也就是说,它是
实现运算符++, - ,==,&,<,+ =, - =,+, - ,[](如果我>
没有忘记任何事情)。 forward_iterator只是变量的名称:)
that''s because, if you see the documentation for the class vector, you
will discover that the vector iterator is a random iterator, that is, it
implements the operators ++, --, ==, &, <, +=, -=, +, -, [] (if I
haven''t forgot any). forward_iterator is just the name of your variable :)
>有没有办法指定迭代器,如果使用不正确,编译器会给出错误,比如当使用关键字const时在变量上?
>Are there some way to specify an iterator so the compiler will give
errors if used incorrectly, like when using the keyword "const" on a
variable?
不需要它。例如,如果他们告诉你函数
模板< typename iterator>
foo(iterator i);
需要一个双向迭代器,这是因为在foo内部将使用
运算符++和 - 。如果你提供了一个前向的
迭代器,编译器会抱怨,而不是找到任何操作符 - 对于
那个类。
it''s not needed. For example, if they tell you that the function
template<typename iterator>
foo(iterator i);
requires a bidirectional iterator, it''s because inside of foo both the
operators ++ and -- will be used. If you provide it with a forward
iterator, the compiler will complain, not finding any operator-- for
that class.
我有点困惑。如果前向
迭代器只是变量名,编译器会如何抱怨?我可以让它报告
错误的唯一方法是,如果我创建自己的类class ForwarIterator我不知何故
使用 - 是非法的。
I am a bit confused. How will the compiler complain if a forward
iterator is just a variable name? The only way I can get it to report an
error is if I make my own class "class ForwarIterator" where I somehow
make it illegal to use "--".
这篇关于如何保证迭代器的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!