c++ - 这段代码中有什么错误?

标签 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;
}

最佳答案

由于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

关于c++ - 这段代码中有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797813/

相关文章:

c++ - 指针分配和段错误

c++ - 定位/下载头文件 R.h 和 Rmath 用于 C 与 R 接口(interface)

c++ - 如何让std::thread在执行完其成员函数后自动删除对象

c++ - 在循环中使用自动 C++

c++ - 在 WINPCAP 中如何知道哪些安装的设备有互联网连接?

c++ - 无法获取 std::bind() 返回对象的 operator() 指针

c++ - 使用 Doxygen 注释不同文件中的函数

java - java 中的 g_ascii_strcasecmp 等效项

c++ - 如何提取以 midi 文件格式播放的所有乐器?

c++ - .cpp 和 .R 文件的常量