【发布时间】:2014-10-31 21:39:08
【问题描述】:
在第一个语句中,it 被取消引用为 0,因为向量中的元素 1 为 0。在我的第二个 if 语句中,我想在取消引用之前增加 it。
int function(vector<int>& vec){
for (auto it = vec.begin() + 1; it != vec.end(); ++it){
if (*it == 0)
{
cout << "element 1 = 0" << endl;
if (*(it +1) && *(it +2) == 0)
cout << "element 2 and 3 = 0";
return 0;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int>grid = { 0, 0, 0, 0, 5, 2, 2, 2, 2 };
function(grid);
输出:
元素 1 = 0
目标输出:
element 1 = 0
element 2 and 3 = 0
【问题讨论】:
-
不清楚你在问什么。
-
抱歉,我已经编辑了我的问题。
标签: c++ if-statement iterator dereference