异常抛出你的项目触发了断点

Exception Thrown Your project has triggered a breakpoint

我正在做一个 C++ 文件处理项目,已经花了很多时间来修复这个错误,但失败了,正在寻求帮助。

这是我的 .txt 文件

SNO, Name, NoOfPeopleLiked
1, The Shawshank Redemption, 77 
2, The Godfather, 20        
3, Into The Wild, 35
4, The Dark Knight, 55      
5, 12 Angry Men, 44
6, Schindler's List, 33
7, The Lord of the Rings: The Return of the King, 25
8, Pulp Fiction, 23
9, The Good, the Bad and the Ugly, 33   
10, The Lord of the Rings: The Fellowship of the Ring, 56
11, Lucifer, 1
12, 50 Shades of Grey, 1

我想显示关于 NoOfPeopleLiked 的评分最高的 5 部电影,当文件中只有 10 部电影 时效果很好。但是每当我在文件中添加更多电影时,它都会显示 Project Movies Management System.exe 已触发断点 然后我的编译器会显示此 if (_Result) { _STL_VERIFY(!_Pred(_Right, _Left), "invalid comparator"); }(BreakPoint).

代码:

int movieRating()
    {
        try {
            auto StrTrim = [](string& str) {
                str.erase(0, str.find_first_not_of("\t\n\v\f\r ")); // left trim
                str.erase(str.find_last_not_of("\t\n\v\f\r ") + 1); // right trim
            };
            ifstream ifile("movies.txt");
            //string line;
            vector<string> lines;
            // Read all lines of file.
            while (getline(ifile, line)) {
                StrTrim(line);
                if (line.empty())
                    continue;
                lines.push_back(line);
            }
            if (!lines.empty())
                lines.erase(lines.begin(), lines.begin() + 1); // remove first header line
            vector<tuple<size_t, string, size_t>> data;
            regex re(R"((\d+)\s*,\s*(.+)?\s*,\s*(\d+))");
            for (auto const& line : lines) {
                smatch matches;
                if (regex_match(line, matches, re)) {
                    if (matches.size() != 4)
                        throw runtime_error("Matched line with not 3 elements: '" + line + "'!");
                    data.push_back(make_tuple(size_t(stoi(matches[1])), matches[2], size_t(stoi(matches[3]))));
                }
                else
                    throw runtime_error("Unmatched line: '" + line + "'!");
            }
            sort(data.begin(), data.end(), [](auto const& l, auto const& r) {
                return get<2>(l) >= get<2>(r);
                });
            int sum = 0;
            for (size_t i = 0; i < 5; ++i) {
                if (i >= data.size())
                    break;
                cout << (i + 1) << ", " << get<1>(data[i]) << ", " << get<2>(data[i]) << endl;
                sum = sum + get<2>(data[i]);
            }
            cout << "\n---------------------------------------------------------------------------" << endl;
            cout << "\nThe total number of people who have already rated the movies is : " << sum << endl;
        }
        catch (exception const& ex) {
            cout << "Exception: " << ex.what() << endl;
            return -1;
        }
    }

简而言之,我想显示关于 NoOfPeopleLiked 的前 5 部电影 我的代码仅适用于文件中的 前 10 部电影 并在任何时候抛出异常电影超过了 10.

比较器必须 return 如果第一项比第二项更早排序,则为 true,否则为 false。这意味着对于 等于 项,比较器必须 return 为假。你的比较器不这样做

return get<2>(l) >= get<2>(r);

改成这个

return get<2>(l) > get<2>(r);

您实际上收到了非常有用的错误消息。