jeudi 28 avril 2016

Latter part of code somehow affecting former

I have this program:

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>

int main(int argc, const char * argv[]) {
    std::vector<int> task_scores;
    int n;
    std::cin >> n;
    for(int i = 0; i < n; i++) {
        int sc;
        std::cin >> sc;
        task_scores.push_back(sc);
    }
    std::map<std::string, std::vector<size_t>> student_solutions;
    std::string command = "";
    while(true) {
        std::vector<std::string> tok_com;
        getline(std::cin, command);
        std::stringstream strstream(command);
        std::string token = "";
        while (getline(strstream, token, ' ')) {
            std::cout << token;
            tok_com.push_back(token);
        }
        if (tok_com[0] == "SOLVED") {
            std::string name = tok_com[1];
            int task = std::stoi(tok_com[2]);
            if(std::find(student_solutions[name].begin(), student_solutions[name].end(), task) !=
               student_solutions[name].end()) {
                student_solutions[name].push_back(task);
            }
        }
    }
    return 0;
}

If you comment out if clause, then code works just fine. But if you don't, the code stops with EXC_BAD_ACCESS when trying to cout the token. How can this happen?

Aucun commentaire:

Enregistrer un commentaire