运行时C ++代码崩溃

时间:2013-08-12 19:37:26

标签: c++

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

int main()
{
    freopen("DATA3.txt","r",stdin);
    freopen("DATA33.txt","w",stdout);
    int k;
    string name,costume;
    for(int z = 0; z < 5;z++)
    {
        cin >> k;
        vector<string> friends;
        vector<string> costumes;
        for (int i = 0 ; i < k; i++)
        {
            cin >> name >> costume;
            bool found = false;
            for (int j = 0; j < costumes.size();j++)
            {
                if (costumes[i] == costume) found = true;
            }
            if (found)
                friends.push_back(name);
            else
                costumes.push_back(costume);

        }
        if (!costumes.size()) cout << "SPOOKY\n";
        else
        {
            for (int i = 0; i < costumes.size();i++)
            {
                if ( i == costumes.size())
                    cout << costumes[i];
                else
                    cout << costumes[i] << " ";
            }
        }
        cout << endl;
    }
}

每当我尝试运行此代码时,它说3.exe已停止工作。为什么是这样?我检查了文本文件和所有事情。 Anynoe对问题的实际背景感兴趣http://www.dwite.ca/questions/costume_party.html

1 个答案:

答案 0 :(得分:1)

如果(服饰[i] ==服装)//你正在通过j = 0迭代到cosplays.ize(),所以将它改为if(服饰[j] ==服装)。