首页 > 解决方案 > C++ 问题:string.at 函数在终端返回“std::out_of_range”错误

问题描述

我已经在这段代码上工作了一段时间,并且一直收到相同的终端错误。我已将问题缩小到两个 at 函数。我已经查过了,但我似乎能找到的唯一答案是,如果编码器在 for 循环中使用了错误的变量,或者 at 函数中的变量没有正确索引。

似乎无法弄清楚为什么 str.at() 函数在应该初始化变量 str 时特别抛出错误。有问题的 at 函数是 do-while 循环中的第二个和第四个 if 语句。

这是代码:

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

// start main function
int main()
{
    ifstream infile;
    string line;
    string str;
    int words = 0;
    int charsNotIncludingSpaces = 0;
    int charsIncludingSpaces = 0;
    int wordsEndingWithE = 0;
    int sixLetterWords = 0;
    int wordsBeginningWithVowel = 0;
    int wordsContainingATE = 0;
    int allEs = 0;
    int wordsWithAtleastTwoEs = 0;

    infile.open("USDictionary.txt");

    // read the first line from the file
    getline(infile, line);
    while(!infile.eof())
    {
        unsigned int startIndex = 0;
        unsigned int endIndex = 0;      

        // get each word from the line and find the required results
        do
        {
            endIndex = line.find(' ', startIndex);          
            if(endIndex > 0 && endIndex < line.size())
            {
                str = line.substr(startIndex, endIndex - startIndex);
            }
            else
            {
                str = line.substr(startIndex);              
            }
            startIndex = endIndex + 1;

            words++;

            charsNotIncludingSpaces += str.size();

            if(str.at(str.size() - 1) == 'e')
            {
                wordsEndingWithE++;
            }

            if(str.size() == 6)
            {
                sixLetterWords++;
            }

            if(str.at(0) == 'a' || str.at(0) == 'A'
                || str.at(0) == 'e' || str.at(0) == 'E'
                || str.at(0) == 'i' || str.at(0) == 'I'
                || str.at(0) == 'o' || str.at(0) == 'O'
                || str.at(0) == 'u' || str.at(0) == 'U')
            {
                wordsBeginningWithVowel++;
            }

            unsigned int ateIndex = str.find("ate");
            if (ateIndex >= 0 && ateIndex < str.size())
            {
                wordsContainingATE++;
            }

            for (unsigned int k = 0; k < str.size(); k++)
            {
                if(str.at(k) == 'e')
                    allEs++;
            }

            if (str.find_first_of('e') != str.find_last_of('e'))
            {
                wordsWithAtleastTwoEs++;
            }          
        } while (endIndex > 0 && endIndex < line.size());

        charsIncludingSpaces += line.size();

        // read the next line from the file
        getline(infile, line);
    }
    infile.close();

    /*// print the results
    cout << "Total number of words in the dictionary: "
            << words << endl;
    cout << "Total number of characters in the dictionary (not including white spaces): "
            << charsNotIncludingSpaces << endl;
    cout << "Total number of characters in the dictionary (including white spaces): "
            << charsIncludingSpaces << endl;
    cout << "Total number of words ending in the letter e: "
            << wordsEndingWithE << endl;
    cout << "Total number of 6 letter words: "
            << sixLetterWords << endl;
    cout << "Total number of words beginning with a vowel: "
            << wordsBeginningWithVowel << endl;
    cout << "Total number of words containing the substring \"ate\": "
            << wordsContainingATE << endl;
    cout << "Total number of occurances of the letter e: "
            << allEs << endl;
    cout << "Total number of words containing at least two occurances of the letter e: "
            << wordsWithAtleastTwoEs << endl;*/

    return 0;
} 

标签: c++stringindexoutofrangeexception

解决方案


推荐阅读