尝试运行代码时获得"Exited with return code -11(SIGSEGV)"


#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> separate(string str){
string build = "";
vector<string> temp;

for(int i = 0;i < str.size(); i++){
if(str[i] != ' '){

build += str[i];

} else if(str[i] == ' '){

temp.push_back(build);
build = "";
}
}

return temp;
}
int main() {

int count;
string sentence;
vector<int> numTimes;

getline(cin, sentence);

vector<string> words = separate(sentence);

for(int i = 0; i < words.size(); i++){
for(int j = 0; j < words.size(); i++){
if(words[i] == words[j]){
count++;
}
}
numTimes.push_back(count);
}

for(int k = 0; k < words.size(); k++){
cout << words[k] << " - " << numTimes[k] << endl;
}
return 0;
}

代码应该接收一个字符串,将其分成单独的单词,将这些单词放入向量中,最后输出该单词在句子中出现的次数。然而,当运行我的代码时,我得到一条消息,说程序以代码-11退出。我在网上看了一下,但不完全理解这是什么意思,或者它在我的代码中发生的地方。

将有符号计数器变量(i,j)更改为无符号计数器变量(size_t)。在separate(..)中,将if-else-if更改为if-else,并固定了每个@user4581301的循环以使用正确的循环变量。也固定了最后一句话没有被添加。

#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> separate(string str) {
string build = "";
vector<string> temp;
for(size_t i = 0; i < str.size(); i++) {
if(str[i] == ' ') {
temp.push_back(build);
build = "";
} else {
build += str[i];
}
}
if(build.size()) {
temp.push_back(build);        
}       

return temp;
}
int main() {
int count = 0;
string sentence;
vector<int> numTimes;
getline(cin, sentence);
vector<string> words = separate(sentence);
for(size_t i = 0; i < words.size(); i++) {
for(size_t j = 0; j < words.size(); j++) {
if(words[i] == words[j]) {
count++;
}
}
numTimes.push_back(count);
}
for(size_t k = 0; k < words.size(); k++) {
cout << words[k] << " - " << numTimes[k] << endl;
}
return 0;
}

这似乎修复了回答问题的段错误。

您没有提供示例输入和输出,但计数显然似乎是错误的。你说的sentence是什么意思?英语中不存在以'结尾的句子。'或其他:

./a.out
a bc d
a - 1
bc - 2
d - 3
./a.out 
a a b
a - 2
a - 4
b - 5

建议你继续努力,如果你需要进一步的帮助,可以提出新的问题。

@Allan Wind是对的,但是提供一个使用c++ 17标准的替代解决方案。

与其使用索引,不如使用更现代的for循环。

for (const char &ch : s)

而不是:

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

毕竟,索引在这种情况下并不重要。

处理多个空格

现在,OP的代码和Allan的代码都会在遇到多个连续空间时将空字符串压入输出向量。我们可以通过在遇到空格时将字符串重置为空来纠正这个错误,但是当遇到空格而字符串为空时,不要执行任何操作。

还需要在循环结束时检查字符串是否为非空。如果是,我们需要把它推到输出向量上。我们可能没有一个尾随空格来触发最后一个字的压入。

vector<string> separate(string s) {
vector<string> output;
string current = "";
for (const char &ch : s) {
if (current != "" && ch == ' ') {
output.push_back(current);
current = "";
}
else if (ch == ' ') {
// Do nothing!
}
else {
current += ch;
}
}
if (current != "") {
output.push_back(current);
}
return output;
}

放到一起看

#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<string> separate(string s);
int main() {
auto v = separate("hello world   foo"); 
for (auto i : v) {
cout << i << endl;
}
}
vector<string> separate(string s) {
vector<string> output;
string current = "";
for (const char &ch : s) {
if (current != "" && ch == ' ') {
output.push_back(current);
current = "";
}
else if (ch == ' ') {
// Do nothing!
}
else {
current += ch;
}
}
if (current != "") {
output.push_back(current);
}
return output;
}

计算单词

我们可以使用映射来计算单词的出现次数。我们使用map<string, int>,其中每个单词都是键,val是出现的次数。在遍历单词时,如果该单词已经作为键存在于map中,则将其加1 '。如果不是,则设置为1。

int main() {
auto v = separate("hello    world   hello world   foo");    
map<string, int> m;
for (auto i : v) {
if (m[i]) {
m[i] += 1;
}
else {
m[i] = 1;
}
}
for (auto const& [key, val] : m) {
cout << "The word "" << key << "" occurs " 
<< val << " times." << endl;
}    
}

最新更新