对"std :: istreambuf_iterator"的用法感到困惑 [英] Confused about usage of 'std::istreambuf_iterator'

查看:126
本文介绍了对"std :: istreambuf_iterator"的用法感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是来自cppreference.com的示例

The Code is:
#include <vector>
#include <sstream>
#include <iostream>
#include <iterator>

int main()
{
// typical use case: an input stream represented as a pair of iterators
std::istringstream in("Hello, world");
std::vector<char> v( (std::istreambuf_iterator<char>(in)),
                      std::istreambuf_iterator<char>() );
std::cout << "v has " << v.size() << " bytes. ";
v.push_back('\0');
std::cout << "it holds \"" << &v[0] << "\"\n";


// demonstration of the single-pass nature
std::istringstream s("abc");
std::istreambuf_iterator<char> i1(s), i2(s);
std::cout << "i1 returns " << *i1 << '\n'
          << "i2 returns " << *i2 << '\n';
++i1;
std::cout << "after incrementing i1, but not i2\n"
          << "i1 returns " << *i1 << '\n'
          << "i2 returns " << *i2 << '\n';
++i2; // this makes the apparent value of *i2 to jump from 'a' to 'c'
std::cout << "after incrementing i2, but not i1\n"
          << "i1 returns " << *i1 << '\n'
          << "i2 returns " << *i2 << '\n';

}

我有两个问题:

  1. 有人可以详细说明代码 std :: vector< char>v((std :: istreambuf_iterator< char>(in)),std :: istreambuf_iterator< char>()); ,我不太了解它在做什么.为什么我们要打印字符串您好,世界"只需使用 cout<<& v [0]
  2. 为什么* i2的学徒值跳跃为"a"?到"c"?有人可以解释它的机制吗?
  1. Can someone elaborate on the code std::vector<char> v( (std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>() );, I do not quite understand what it's doing..and why can we print the string "Hello, world" just by using cout<<&v[0]
  2. Why does the apprent value of *i2 jump for "a" to "c"? can someone explain the mechanisms of it?

非常感谢!

推荐答案

有人可以详细说明代码...

Can someone elaborate on the code ...

std :: vector< T> 具有一个构造函数,该构造函数在< T> 上采用两个迭代器-一个用于范围的开始,另一个用于范围的结束.

std::vector<T> has a constructor that takes two iterators on <T> - one for the beginning and one for the end of the range.

此构造函数根据输入流 in 中的输入流进行迭代:

This constructor makes an input stream iterator from an input stream in:

std::istreambuf_iterator<char>(in)

您可以访问其元素,直到到达流的末尾.一旦到达流的末尾,迭代器就等同于使用默认构造函数创建的迭代器:

You can access its elements going forward until you reach the end of the stream. Once you reach the end of stream, the iterator becomes equivalent to an iterator created using the default constructor:

std::istreambuf_iterator<char>()

因此,传递这对迭代器将根据从输入流读取的数据构造 vector< T> .整个流将被消耗.

Therefore, passing this pair of iterators constructs a vector<T> from the data read from an input stream. The entire stream will be consumed.

为什么 * i2 的权限值从"a" 跳到"c" ?

两个迭代器都从同一流中读取.当您增加第一个迭代器时,它将消耗基础流中的'b'.同时, i2 指的是流的第一个字符,它在构造时就没有前进.

Both iterators are reading from the same stream. When you increment the first iterator, it consumes 'b' from the underlying stream. In the meantime, i2 refers to the first character of the stream, which it got without advancing at the time when it has been constructed.

一旦增加 i2 ,它就会向流请求下一个字符.字符'b'已被使用,因此下一个字符为'c'.

Once you increment i2, it asks the stream for the next character. Character 'b' has been consumed already, so the next character is 'c'.

最后,代码带来了一个您可能会忽略的小技巧:它将空终止符推入 vector< char> 中,以便能够使用 const来打印矢量 operator<<(...)的char * 重载.

Finally, the code pulls a little trick that you may have overlooked: it pushes a null terminator into the vector<char> in order to be able to print the vector using the const char* overload of operator <<(...).

这篇关于对"std :: istreambuf_iterator"的用法感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆