这段代码中的错误是什么?

时间:2015-03-01 19:08:47

标签: c++

当我尝试构建此代码时,它会显示错误! 而且我不知道如何解决它!!

错误C3531:' x':符号类型包含' auto'必须有一个初始化器 错误C2143:语法错误:缺少','之前':'

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <cctype>
using namespace std;

   int main(){
ifstream in("input.txt");
ofstream out("output.txt");
string s;
int line=0;
vector<string> vec(1,"dummy");
multimap<int,int> M;

while(getline(in, s)){
    line++;
    vec.push_back(s);
    if(line%12==10){
        string temp="";
        for(auto x:s) if(isdigit(x)) temp+=x;
        int key = stoi(temp);
        M.insert(make_pair(key,line));      
    }
}

auto it = M.rbegin();
while(it != M.rend()){      
    int i = it->second;
    int start = (int(i/12))*12 +1;
    for(int j=1; j<=12; j++) out << vec.at(start++) << "\n";        
    it++;
}


in.close();
out.close();    
return 0;
}

2 个答案:

答案 0 :(得分:1)

MS VC ++ 2010不支持基于for循环的范围的标准语法。但它支持以下语法:

for each (auto x in s) if(isdigit(x)) temp+=x;

因此,这就是编译错误的原因。

答案 1 :(得分:1)

由于VS2010不支持语法,只需使用pre-c ++ 11语法:

if(line%12==10){
    string temp="";
    for (std::string::const_iterator iter=s.begin(); iter!=s.end(); ++iter)
        if (isdigit(*iter)) temp += *iter;
    int key = stoi(temp);
    M.insert(make_pair(key,line));      
}

或者也许:

if (line%12 == 10) {
    int key = 0;
    for (std::string::const_iterator iter=s.begin(); iter!=s.end(); ++iter)
        if (isdigit(*iter)) key = (key * 10) + (*iter - '0');
    M.insert(make_pair(key, line));
}

并删除临时字符串stoi