具有低于对象reverse_iterator的行为[]
// #1 Outputs NOTHING, expected "jihgfedcba"
for (auto begin = std::rbegin(a), end = std::rend(a); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
// #2 Outputs "jihgfedcba", as expected
for (auto begin = std::rbegin(s), end = std::rend(s); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
但是打印字符输出的阵列打印字符串时没有任何内容显示预期的输出。
打印字符数组也会影响字符串的打印:如果在#2之前写入#1循环,则程序只输出一个字符,但会显示单个jihgfedcba
。
我注意到,调整的std::rbegin(a)
修复问题的返回值:
// Outputs "jihgfedcba", as expected
// notice the ++!!
for (auto begin = ++std::rbegin(a), end = std::rend(a); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
// Outputs "jihgfedcba", as expected
for (auto begin = std::rbegin(s), end = std::rend(s); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
这究竟是为什么?
我无法复制。两个循环都为我产生相同的结果。除了const char []之外,前面还有一个额外的空白空间(最有可能是'\ 0')。 – DeiDei
@DeiDei让我给你提供一个[代码](http://melpon.org/wandbox/permlink/k1RqUEKvVRxedJVf),它显示了所描述的行为。 –