【发布时间】:2018-05-08 11:32:56
【问题描述】:
为什么迭代 bool 向量(w/ 修改元素)需要 &&,而不是 int 向量?
// junk10.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
vector<int> miv{ 1, 2, 3 };
for (auto &e : miv) { e = 15; } // Legal
vector<bool> mbv{ false };
for (auto &e : mbv) { e = true; } // Illegal
for (auto &&e : mbv) { e = true; } // Legal
return 0;
}
【问题讨论】:
-
因为
std::vector<bool>不像其他任何向量。 -
谢谢,那篇文章主要解释了原因。大多数情况下,我的意思是我主要理解解释;您链接到的帖子几乎可以肯定是 100% 准确和完整的。
标签: c++11