a***e 发帖数: 413 | 1 请问,为什么这个答案在oj能通过,但在VS2010中编译for (auto i:num)老是出错呢?
多谢
error C3531: 'i': a symbol whose type contains 'auto' must have an
initializer
int longestConsecutive(vector &num) {
unordered_map used;
for (auto i:num) used[i]=false;
int longest = 0;
for (auto i:num)
{
if (used[i])
continue;
int length = 1;
used[i] = true;
for (int j=i+1; used.find(j)!=used.end(); j++)
{
used[j]=true;
length++;
}
for (int j=i-1; used.find(j)!=used.end(); j--)
{
used[j]=true;
length++;
}
longest = max(longest,length);
}
return longest;
} |
k*******a 发帖数: 433 | 2 将auto改成int行吗?
【在 a***e 的大作中提到】 : 请问,为什么这个答案在oj能通过,但在VS2010中编译for (auto i:num)老是出错呢? : 多谢 : error C3531: 'i': a symbol whose type contains 'auto' must have an : initializer : int longestConsecutive(vector &num) { : unordered_map used; : : for (auto i:num) used[i]=false; : : int longest = 0;
|
s**x 发帖数: 7506 | 3 auto 这个用法得用很新的compiler 吧? 确实很不错的 feature, 老的 compiler
may not support this. c++11?
change to:
for (size_t i =0; i< num.size(); i++) {
}
variable name num is not good. use numList or something like that. |