2016-11-17 73 views
1

我有以下代码。它应该计算给定文件中给定字母的重复次数。然而,当我尝试运行这个我得到的向量下标超出范围。其他人有相同的错误试图访问它的未定义的部分,但这似乎并不是我认为的问题。向量下标超出范围 - 结构向量

struct letters 
{ 
    char letter; 
    int repetitions=0; 
}; 


void howManyTimes(const string &output) 
{ 
    ifstream file(output); 
    vector <letters> alphabet; 

    for (int i = 0; i < 'z' - 'a' + 1; i++) 
    { 
     alphabet[i].letter = 'a' + i; 
    } 

    string line; 

    while (file.eof() == 0) 
    { 
     getline(file, line); 
     for (char c : line) 
     { 
      if(c >= 'a' && c <= 'z') 
       alphabet[c - 'a'].repetitions++; 
      else if (c >= 'A' && c >= 'Z') 
       alphabet[c - 'A'].repetitions++; 
     } 
    } 
    cout << alphabet[10].repetitions; 
} 
+1

你在哪里填充元素的向量? – UnholySheep

+1

为了使用它,需要初始化矢量。否则,使用push_back('a'+ i)'而不是'operator []'来分配值。 –

+0

如果您想查找唯一项目的出现次数,您应该使用'std :: map'。 – NathanOliver

回答

2
vector <letters> alphabet; // (1) 

for (int i = 0; i < 'z' - 'a' + 1; i++) 
{ 
    alphabet[i].letter = 'a' + i; // (2) 
} 

(1)创建一个空矢量

里面的for回路(2)您尝试访问的载体使用索引i项目,这么清楚你的索引是超出范围

您首先必须使用一些数据填充矢量,然后才能访问这些数据。

如果你想添加新项目的载体,可以使用vector::push_back(这可能是你在(2)的意思)。

0

其他人有相同的错误试图访问它的未定义的部分,但这似乎并不是一个问题,我认为。

这绝对是这里的一个问题:

vector <letters> alphabet; // Now size of the vector is 0. 

for (int i = 0; i < 'z' - 'a' + 1; i++) 
{ 
    // You are trying to access elements 0, 1, 2, ..., which do not exist. 
    alphabet[i].letter = 'a' + i; 
} 

最简单的办法是适当的大小来构建你的载体:

vector <letters> alphabet('z' - 'a' + 1); 
+0

应该注意的是,'''''''在系统上不起作用。它将在使用ASCII编码(这是所有系统的绝大多数)的系统上工作,但它不是真正的便携式。 –

+0

它的工作!但它必须是 ''z' - 'a'+ 1' 而我有这个 '(c> ='A'&& c> ='Z')' 如果错了。标志应该是'c''Z'' –

0

我没有看到你的代码,其中的一部分alphabet已扩展为容纳您计划存储在其中的对象。当您使用push_back,insert,emplace或其他类似方法时,std::vector仅调整自身大小;它在直接使用operator[]方法访问时不这样做。

无论如何,像这样的任务,我不知道你想使用的载体,当std::map<char, int64_t>将可能会更加清晰,并且将让你保持你试图语法使用没有吨的额外维护。

void howManyTimes(const string &output) 
{ 
    ifstream file(output); 

    map<char, int64_t> alphabet; 

    string line; 

    while (getline(file, line)) 
    { 
     for (char c : line) 
     { 
      if(c >= 'a' && c <= 'z') 
       alphabet[c - 'a']++; 
      else if (c >= 'A' && c >= 'Z') 
       alphabet[c - 'A']++; 

     } 
    } 
    cout << alphabet[10]; 

} 
+0

你至少应该注意到循环条件是错误的,如果你要重现它 –

+0

@LightnessRacesinOrbit我真的没有注意到这一点。我复制了他们的代码,并对我所关注的内容进行了更改。我会解决它。 – Xirema

0

这个程序是什么的问题是问真厉害(我不知道做时,我把它作为家庭作业:)我的学生的感受)。你必须使用一个结构?假设没有了,还假设我们知道字母的大小,而“一”是第一个字母“Z”是最后一个字母:

vector<int> repetitions(26, 0); 
char nextCharacter; 
while(!file.eof()) 
{ 
    cin >> nextCharacter; 
    nextCharacter = tolower(nextCharacter); 
    if(nextCharacter >= 'a' && nextCharacter <= 'z') 
    { 
     repetitions[nextCharacter - 'a']++; 
    } 
} 

要检查的一封信:

cin >> letterToQuery; 
cout <<"The amount of occurrences of " << letterToQuery <<" is "; 
cout << repetitions[tolower(letterToQuery) - 'a'] << endl; 

如果你不知道你的字母表的大小,代码更改为:

vector<int> repetitions('last_alphabet' - 'a' + 1, 0); 
... 
if(nextCharacter >= 'a' && nextCharacter <= 'last_letter') 

最后,如果你必须使用结构,你的代码更改为:

struct Letter 
{ 
    char letter; 
    int repetitions=0; 
}; 

vector<Letter> alphabet; 
letter temp; 
for(int i = 0; i < 'last_alphabet' - 'a' + 1; ++i) 
{ 
    temp.letter = 'a' + i; 
    alphabet.push_back(temp); 
} 

// then everything else is a similar structure 
char nextCharacter; 
while(!file.eof()) 
{ 
    cin >> nextCharacter; 
    nextCharacter = tolower(nextCharacter); 
    if(nextCharacter >= 'a' && nextCharacter <= 'last_alphabet') 
    { 
     alphabet[nextCharacter - 'a'].repetitions++; 
    } 
} 

要检查的一封信:

cin >> letterToQuery; 
cout <<"The amount of occurrences of " << letterToQuery <<" is "; 
cout << alphabet[tolower(letterToQuery) - 'a'].repetitions << endl; 

注意,如果你用 'Z' 替代 'last_alphabet',你会得到当前字母。