“iterator + n”没有out_of_range异常与vector.at(n) [英] No out_of_range exception for "iterator + n" vs. vector.at( n )
问题描述
大家好。只是在一个小型虚拟机上工作,并考虑使用
向量迭代器而不是指针算法。问题是,为什么
迭代器加上任何超出范围的数字都不会产生out_of_range异常?
也许这是一个gcc问题?
我正在使用gcc版本3.3.3(cygwin特别版)。
这里是完整的示例代码:
#include< iostream>
#include< vector>
#include< stdexcept>
using namespace std;
int main(){
vector< int>代码;
code.push_back(10L);
code.push_back(20L);
vector< int> :: iterator iter = code.begin();
try {
cout<< *(iter + 5)<< ENDL; // 0
cout<< code.at(10)<< ENDL; // vector []访问超出范围
} catch(out_of_range e){
cout<< e.what()<<结束;
}
返回0;
}
谢谢,
迈克
" Mike Austin" < MI ** @ mike-austin.com>在留言中写道
新闻:0L ********************* @ bgtnsc04-news.ops.worldnet.att.net ...大家好。只是在一个小型虚拟机上工作,并考虑使用
向量迭代器而不是指针算法。问题是,为什么
迭代器加上任何超出范围的数字都不会产生out_of_range
异常?
因为这就是库的设计方式。
如果你想要范围检查,请使用vector :: at()。
这就是它的用途。
这是在不要为你不使用的东西付费之后的。
C ++原理。 (''at()''必然会增加更多
的开销,这对某些
申请来说可能是不可接受的。
也许这是一个gcc问题?
No.
我正在使用gcc 3.3.3版(cygwin特别版)。
这里是完整示例代码:
#include< iostream>
#include< vector>
#include< stdexcept>
使用命名空间std;
int main(){
vector< int>代码;
code.push_back(10L);
code.push_back(20L);
vector< int> :: iterator iter = code.begin();
尝试{
cout<< *(iter + 5)<< ENDL; // 0
我不知道你的评论是什么意思0,
但请注意这句话会产生''未定义的行为''。
cout<< code.at(10)<< ENDL; // vector []访问超出范围
} catch(out_of_range e){
cout<< e.what()<< endl;
返回0;
}
如果你不使用' 'vector :: at()''你仍然可以通过检查例如来保护你自己的b $ b ''vector :: size()'',''vector :: empty()'',比较
与''vector :: end()''等。
-Mike
" Mike Wahler" < MK ****** @ mkwahler.net>在留言中写道
news:s9 ***************** @ newsread3.news.pas.earthl ink.net ...尝试{
cout<< *(iter + 5)<< ENDL; // 0
另请注意,您可以使用带有向量的可能更具优势的
数组表示法:
iter [n]; / *但仍无边界检查* /
-Mike
" Mike Austin" < MI ** @ mike-austin.com>在留言中写道
新闻:0L ********************* @ bgtnsc04-news.ops.worldnet.att.net ...大家好。只是在一个小型虚拟机上工作,并考虑使用
向量迭代器而不是指针算法。问题是,为什么
迭代器加上任何超出范围的数字都不会产生out_of_range
异常?
也许这是一个gcc问题?
矢量迭代器通常被实现为指针,例如
模板< class T>
类向量
{
public:
typedef T * iterator;
typedef const T * const_iterator;
因此,矢量迭代器不会抛出异常,原因与
普通指针不同。
john
Hi all. Just working on a small virtual machine, and thought about using
vector iterators instead of pointer arithmetic. Question is, why does an
iterator plus any number out of range not generate a out_of_range exception?
Maybe this is a gcc issue?
I''m using gcc version 3.3.3 (cygwin special).
Here''s the full sample code:
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
int main() {
vector<int> code;
code.push_back( 10L );
code.push_back( 20L );
vector<int>::iterator iter = code.begin();
try {
cout << *(iter + 5) << endl; // 0
cout << code.at( 10 ) << endl; // vector [] access out of range
} catch( out_of_range e ) {
cout << e.what() << endl;
}
return 0;
}
Thanks,
Mike
"Mike Austin" <mi**@mike-austin.com> wrote in message
news:0L*********************@bgtnsc04-news.ops.worldnet.att.net...Hi all. Just working on a small virtual machine, and thought about using
vector iterators instead of pointer arithmetic. Question is, why does an
iterator plus any number out of range not generate a out_of_range exception?
Because that''s the way the library is designed.
If you want range checking, use vector::at().
That''s what it''s for.
This follows the "don''t pay for what you don''t use"
principle of C++. (''at()'' will necessarily add more
overhead which might be unacceptable for some
applications).
Maybe this is a gcc issue?
No.
I''m using gcc version 3.3.3 (cygwin special).
Here''s the full sample code:
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
int main() {
vector<int> code;
code.push_back( 10L );
code.push_back( 20L );
vector<int>::iterator iter = code.begin();
try {
cout << *(iter + 5) << endl; // 0
I don''t know what you mean by your comment "0",
but note that this statement produces ''undefined behavior''.
cout << code.at( 10 ) << endl; // vector [] access out of range
} catch( out_of_range e ) {
cout << e.what() << endl;
}
return 0;
}
If you don''t use the protection of ''vector::at()'' you can still protect
yourself by checking e.g. ''vector::size()'', ''vector::empty()'', comparing
against ''vector::end()'', etc.
-Mike
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:s9*****************@newsread3.news.pas.earthl ink.net...try {
cout << *(iter + 5) << endl; // 0
Also note that you can use the possibly more intiutive
array notation with a vector:
iter[n]; /* but still no bounds checking */
-Mike
"Mike Austin" <mi**@mike-austin.com> wrote in message
news:0L*********************@bgtnsc04-news.ops.worldnet.att.net...Hi all. Just working on a small virtual machine, and thought about using
vector iterators instead of pointer arithmetic. Question is, why does an
iterator plus any number out of range not generate a out_of_range
exception?
Maybe this is a gcc issue?
Vector iterators are often implemented as pointers, i.e. something like
template <class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
So vector iterators don''t throw exceptions for the same reasons that
ordinary pointers don''t.
john
这篇关于“iterator + n”没有out_of_range异常与vector.at(n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!