简体   繁体   English

为什么取消对迭代器的引用会使输出流中断?

[英]Why does dereferencing an iterator make output-stream cut out?

I'm practicing std::deque , specifically iterators 我正在练习std::deque ,特别是迭代器

I tested the end iterator. 我测试了end迭代器。 like this: 像这样:

    std::deque<const char*> wup {"how","are","you","doing","today?"};
    std::deque<const char*>::iterator it = wup.end();

Then, I want to print it , and I know that the end iterator is empty. 然后,我要打印it ,并且我知道结束迭代器为空。

在此处输入图片说明

Then I try to use 2 cout statements to print the content of it : one of them before --it and another after --it 然后,我尝试使用2条cout语句要打印的内容it :他们中的一个前--it和另一个之后--it

But if I write a cout before --it , the last cout does not appear in the output. 但是,如果我在--it之前编写了cout ,则最后一个cout不会出现在输出中。

Something like this: 像这样:

std::deque<const char*> wup {"how","are","you","doing","today?"};
std::deque<const char*>::iterator it = wup.end();
std::cout<<"Before --it: "<<*it<<std::endl;// okay, it works good | and finishes console
--it;
std::cout<<"After --it: "<<*it<<std::endl;// I do not know why it does not appear.

Output: 输出:

  Before --it: Process returned 0 (0x0) execution time : 0.010 s Press ENTER to continue. 

Where did I make a mistake ? 我在哪里弄错了?
if dereferencing an iterator is wrong ... 如果取消引用迭代器是错误的...
why this case, cuts the output-stream without any exception 为什么这种情况会毫无例外地削减输出流
Thanks a lot. 非常感谢。 tested on: 经过测试:
OS: Ubuntu 16.01
compiler: g++ version: 5.3.1
IDE: code::blocks 16.01


Edit: I found this note in /reference/en/cpp/container/map/erase.html : 编辑:我在/reference/en/cpp/container/map/erase.html中找到了此注释

The iterator pos must be valid and dereferenceable . 迭代器pos必须有效且可取消引用 Thus the end() iterator (which is valid, but is not dereferencable ) cannot be used as a value for pos. 因此,end()迭代器(有效,但不可取消引用 )不能用作pos的值。

I think this is real reason for, why ... . 我认为这是真正的原因,为什么……。

my native language is not English, so excuse me, if you see some mistake 我的母语不是英语,请原谅,如果您发现一些错误

Where did i make a mistake? 我在哪里弄错了?

This line: 这行:

std::cout<<"Before --it: "<<*it<<std::endl;

is de-referencing an iterator that points past the end of the container which is undefined behavior. 正在取消引用指向未定义行为的容器末端的迭代器。

Why this case, cuts the output-stream without any exception? 为什么这种情况会毫无例外地削减输出流?

This question comes in 2 parts: 这个问题分为2部分:

  1. Why does it cut the output stream? 为什么会削减输出流? Because undefined behavior is undefined. 因为未定义的行为是未定义的。 You don't know what will happen. 你不知道会发生什么。 There is plenty of information around about this: http://en.cppreference.com/w/cpp/language/ub 有关此的信息很多: http : //en.cppreference.com/w/cpp/language/ub

  2. Why no exception? 为什么没有例外? In order to provide an exception to everything that might cause UB, c++ would have to check every de-reference which would be computationally expensive for very little gain. 为了为可能导致UB的所有事件提供一个例外, c++必须检查每个取消引用,这在计算上非常昂贵,而且收益很小。 It is up to the user to use the iterator interface correctly. 用户可以正确使用迭代器界面。 This is the same in the following example: 在以下示例中,这是相同的:


 int a[5];
 a[6] = 10; // Why does c++ allow this when it is clearly a mistake? Because the user is 
            // responsible for writing good code, not the compiler, and certainly
            // not the run-time exception handler.  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM