如何打印 C++ 中的迭代器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31574737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:59:20  来源:igfitidea点击:

how the iterator in c++ could be printed?

c++iterator

提问by Enamul Hassan

Suppose, I have declared a vector in C++like this:

假设,我已经声明了一个C++像这样的向量:

vector<int>numbers = {4,5,3,2,5,42};

I can iterate it through the following code:

我可以通过以下代码迭代它:

for (vector<int>::iterator it = numbers.begin(); it!=numbers.end(); it++){
    // code goes here
}

Now, I would talk about coding in the block of for loop.

现在,我将讨论在for loop.

I can access and change any value using this iterator. say, I want to increase every value by 10 and the print. So, the code would be:

我可以使用此迭代器访问和更改任何值。说,我想将每个值增加 10 和打印。所以,代码将是:

*it+=10;
cout << *it << endl;

I can print the address of both iterator and elements that are being iterated.

我可以打印迭代器和正在迭代的元素的地址。

Address of iterator can be printed by:

迭代器的地址可以通过以下方式打印:

cout << &it << endl;

Address of iterated elements can be printed by:

迭代元素的地址可以通过以下方式打印:

cout << &(*it) << endl;

But why the iterator itself could not printed by doing the following?

但是为什么迭代器本身无法通过执行以下操作来打印?

cout << it <<endl;

At first I thought the convention came from JAVAconsidering the security purpose. But if it is, then why I could print it's address?

起初我认为这个约定是JAVA出于对安全目的的考虑。但如果是,那为什么我可以打印它的地址?

However, Is there any other way to do this? If not, why?

但是,有没有其他方法可以做到这一点?如果不是,为什么?

回答by Enamul Hassan

Yes, there is a way to do it!

是的,有办法做到!

You can't print the iterator because it is not defined to have a value. But you can perform arithematic operations on them and that helps you to print the value (of the iterator).

您无法打印迭代器,因为它未定义为具有 value。但是您可以对它们执行算术运算,这有助于您打印值(迭代器的)。

Do the following.

请执行下列操作。

cout << it - v.begin();  

Example:

例子:

#include <iostream>     
#include <algorithm>    
#include <vector> 
#include <iterator>

using namespace std;

int main () {
  vector<int> v = {20,3,98,34,20,11,101,201};           
  sort (v.begin(), v.end());                

  vector<int>::iterator low,up;
  low = lower_bound (v.begin(), v.end(), 20);          
  up = upper_bound (v.begin(), v.end(), 20);                  

  std::cout << "lower_bound at position " << (low - v.begin()) << std::endl;
  std::cout << "upper_bound at position " << (up - v.begin()) << std::endl;

  return 0;
}

Output of the above code:

上面代码的输出:

lower_bound at position 2
upper_bound at position 4

Note: this is just a way to get things done and no way I have claimed that we can print the iterator.

注意:这只是完成工作的一种方式,我没有声称我们可以打印迭代器。

...

...

回答by Baum mit Augen

There is no predefined output operator for the standard iterators because there is no conventional meaning of printing an iterator. What would you expect such an operation to print? While you seem to expect to see the address of the object the iterator refers to, I find that not clear at all.

标准迭代器没有预定义的输出运算符,因为没有打印迭代器的传统含义。您希望这样的操作打印什么?虽然您似乎希望看到迭代器所指对象的地址,但我发现这根本不清楚。

There is no universal answer to that, so the committee decided not to add a those operators. (The last half sentence is a guess, I am not part of the committee.)

对此没有统一的答案,因此委员会决定不添加这些运营商。(后半句是猜测,我不是委员会的成员。)

If you want to print those iterators, I would define a function like print(Iterator);(or something like this, whatever fits your needs) that does what you want. I would not add an operator <<for iterators for the reason I mentioned above.

如果你想打印这些迭代器,我会定义一个像print(Iterator);(或类似的东西,任何适合你的需要)的函数来做你想要的。operator <<由于我上面提到的原因,我不会添加for 迭代器。

回答by Baum mit Augen

why the iterator itself could not printed by doing the following?

为什么迭代器本身无法通过执行以下操作打印?

Because, it is not defined to a value internally.

因为,它没有在内部定义为一个值。

Is there any other way to do this?

有没有其他方法可以做到这一点?

Basically, the compiler does not facilitate it by default, you may try to edit the compiler code! But it is too terrific you know!

基本上编译器默认不方便,你可以尝试编辑编译器代码!但你知道它太棒了!

If not, why?

如果不是,为什么?

Because it has no well-defined way to express it.

因为它没有明确的表达方式。