简体   繁体   English

C++ for 循环中的逗号运算符

[英]comma operator in a C++ for loop

I am trying to use comma to separate multiple initialization within a for loop, but I am getting the error below.我正在尝试使用逗号分隔 for 循环中的多个初始化,但出现以下错误。

Is is legal to use the comma in the first part of a for-loop?在 for 循环的第一部分使用逗号是否合法?

error: too few template-parameter-lists
error: sEnd was not declared in this scope


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

int main() {
  using namespace std;
  typedef vector<int> Vc;
  Vc v;
  for(Vc::iterator sIt = v.begin(), Vc::iterator sEnd = v.end();
      sIt != sEnd; ++sIt) {
    // do something
  }    
  return 0;
}

Should just be:应该只是:

                                  /* remove this  */
for(Vc::iterator sIt = v.begin(), /* Vc::iterator */ sEnd = v.end();
    sIt != sEnd; ++sIt) {
  // do something
}

Becomes:变成:

for(Vc::iterator sIt = v.begin(), sEnd = v.end();
    sIt != sEnd; ++sIt) {
  // do something
}

Also, this is not a usage of the comma operator (the comma operator can only be used in an expression);此外,这不是逗号运算符的用法(逗号运算符只能在表达式中使用); this is a simple variable declaration.这是一个简单的变量声明。

允许使用逗号,不允许使用类型名开始逗号后面的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM