简体   繁体   English

向量数组的调试断言失败错误

[英]Debug assertion Failed error with vector array

  vector<string> names;

    // Read names from file book.txt
    ifstream in("movie.txt");
    if (!in.is_open())
        cout << "Unable to open file\n";

    string word;
    while (getline(in, word))
        names.push_back(word);

    int pos(0), i(0), j(0);
    string temp;

    for (size_t i = 0; i < names.size(); i++)
    {

        j = i;

        while (j >= 0 && names[j] < names[j - 1])
        {
            temp = names[j];
            names[j] = names[j - 1];
            names[j - 1] = temp;
            j--;
        }

    }

    // Loop to print names
    for (size_t i = 0; i < names.size(); i++)
        cout << names[i] << '\n';

Not sure where the error is coming from as it still runs, but as i try and execute the file it says "Debug assertion error."不确定错误来自哪里,因为它仍在运行,但是当我尝试执行该文件时,它显示“调试断言错误”。 Any help?有什么帮助吗?

If you want to read the names then try this it works如果您想阅读名称,请尝试此操作

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

int main(int argc, char **argv)
{    
    vector<string> names;
    ifstream in("movies.txt");

    if (!in.is_open())
        cout << "Unable to open file\n";

    string word;
    while (getline(in, word))
        names.push_back(word);


    for (auto i : names)
        cout << i << '\n';

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM