简体   繁体   English

为什么我的 vector::erase 调用会抛出“向量下标超出范围”?

[英]Why does my vector::erase call throw "vector subscript out of range"?

I'm writing a program that saves words from a .txt file in vector words , calculates how many words are there (num_elements) and prints these words randomly to the screen (no duplicates).我正在编写一个程序,将 .txt 文件中的words保存在向量words ,计算有多少单词(num_elements)并将这些单词随机打印到屏幕上(没有重复)。

It all works fine up until rw.erase line, which just spits out the error "vector subscript out of range".一切正常,直到rw.erase行,它只是吐出错误“向量下标超出范围”。

Why is my erase call throwing "vector subscript out of range"?为什么我的erase调用会抛出“向量下标超出范围”?

#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
#include <direct.h>
#include <filesystem>
#include <time.h>
#include <random>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

void path_to_main_dir() {
    string path = "C:/Randomizer/";
    for (const auto& entry : experimental::filesystem::directory_iterator(path)) {
        cout << entry.path() << endl;
    }

}

int main() {

    path_to_main_dir();
    string dateread;
    printf("Which file do you want to open? ");
    cout << "---------------------------------------------------------------------------------------------------------" << endl;
    path_to_main_dir();
    cout << endl;
    cout << "---------------------------------------------------------------------------------------------------------" << endl;
    cout << "User: ";
    getline(cin, dateread);
    string path_to_file = "C:/Randomizer/" + dateread + ".txt";
    ifstream readfile(path_to_file.c_str());
    vector<string> words;
    string word;
    while (getline(readfile, word))
    {
        words.push_back(word);
    }
    readfile.close();
    srand(time(NULL));

    string randomword;
    vector<string> rw = { words };
    int num_elements = size(words);
    cout << endl;
    cout << "Number of words in the file: ";
    cout << num_elements;
    cout << endl;
    for (unsigned int a = 0; a < num_elements; a = a + 1)
    {
        randomword = rw[rand() % num_elements];
        cout << randomword << endl;
        rw.erase(remove(rw.begin(), rw.end(), randomword), rw.end());
        num_elements -= 1;
        system("pause");
    }

    goto firstline;

    return 0;
}

If the error only happens in the case where there are duplicates, it could be because the num_elements is wrong.如果错误只发生在有重复的情况下,可能是因为 num_elements 错误。 The remove / erase call will have deleted as many duplicates as there are, but num_elements has only been reduced by one. remove / erase调用将删除尽可能多的重复项,但 num_elements 仅减少了 1。

Fortunatly, vectors know their own size, so rather than trying to remember its internal information for it, you can just ask!幸运的是,向量知道它们自己的大小,所以与其试图记住它的内部信息,你可以问问!

int main()
{
    //...
    //Code to read words from file
    //...

    cout << "Number of words in file: " << words.size() << endl;

    while(!words.empty())
    {
        string randomWord = words[rand() % words.size()];
        cout << randomWord << endl;
        words.erase(remove(words.begin(), words.end(), randomWord), words.end());
    }

    return 0;
}

From what I can see, you only use words to create rw , so we could just use words directly instead.就我所见,你只words来创建rw ,所以我们可以直接words来代替。

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

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