C++,我收到一个无法理解的编译器错误

In C++, I'm getting a compiler error that I can't make sense of

本文关键字:错误 编译器 一个 C++      更新时间:2023-10-16

这是我的代码,它只是颠倒了句子:

#include <iostream>
#include <string>   
using namespace std;
int main()
{
string sentence;
string reversedSentence;
int i2 = 0;
cout << "Type in a sentence..." << endl;
getline(cin, sentence);
for (int i = sentence.length() - 1; i < sentence.length(); i--)
{
reversedSentence[i2] = sentence[i];
i2++;
}
cout << reversedSentence << endl;
}

编译工作正常,但当我尝试运行程序时,会发生以下情况:

Type in a sentence...
[input]
/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]: Assertion '__pos <= size()' failed.

reversedSentence字符串为空,因此对其进行索引会调用未定义的行为。相反,您可以这样使用push_back

for (int i = sentence.length() - 1; i >= 0; i--)
{
reversedSentence.push_back(sentence[i]);
}

还要注意,您的循环条件需要修改。如果sentence为空,则在减去1之前,应将static_cast.length()int相加,如下所示:

for (int i = static_cast<int>(sentence.length()) - 1; i >= 0; i--)
{
reversedSentence.push_back(sentence[i]);
}

你也可以使用一种算法:

reversedSentence = sentence;
std::reverse(reversedSentence.begin(), reversedSentence.end());

这避免了sentence字符串为空时的复杂情况。

您的for循环表示i < sentence.length()是结束条件。这导致它始终是true,并且从不访问for循环,因为您将i声明为sentence.length() - 1。这将始终小于sentence.length()

我的建议是:不要使用索引。尽可能使用迭代器。
for (auto iter = sentence.rbegin(); iter != sentence.rend(); ++iter)
{
reversedSentence.push_back(*iter);
}
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::string sentence;
std::cout << "Sentence: ";
std::getline(std::cin, sentence);
std::stringstream sstrin(sentence);
int spaces = std::count(sentence.begin(), sentence.end(), ' ');
std::vector<std::string> chunks;
chunks.reserve(spaces + 1);
while (sstrin) {
std::string tmp;
sstrin >> tmp;
chunks.push_back(tmp);
}
std::ostream_iterator<std::string> strout(std::cout, " ");
std::cout << "Words in reverse:n";
std::copy(chunks.rbegin(), chunks.rend(), strout);
std::cout << "nFull Mirror:n";
std::ostream_iterator<char> charout(std::cout);
std::copy(sentence.rbegin(), sentence.rend(), charout);
std::cout << 'n';
}

你所说的反向有点不清楚。你的代码指示了我所说的";全反射镜";其中所有字符的顺序是向后的。但也有可能你只是想把单词倒过来,这是当人们说他们想要一个倒过来的句子时,我首先想到的。根据你想要的不同,你做这件事的方式也不同。

对于相反的单词,我需要确保每个单词都被视为自己的实体。为此,我声明了一个字符串向量,其中每个元素都是一个单独的单词。我使用字符串流,因为它可以为我分离单词,而不必编写手动检查句子的函数。这是因为默认情况下,流在空白处是分开的。一旦矢量被单词填满,我就开始按相反的顺序打印它们。我用std::ostream_iterator来做这件事。它比基于范围的for更紧凑,并允许我利用向量的反向迭代器;它省去了我反转矢量的麻烦。

对于";全反射镜;我同样使用std::ostream_iterator,但它是由字符组成的,因为我提供了原始句子的反向迭代器。字符串的各个元素都是字符。

最后,没有迹象表明你是否需要保存这些颠倒的句子,所以我不介意。

for (int i = sentence.length() - 1; i < sentence.length(); i--)

你为什么要写一个无穷循环??你正在减少i,它总是小于句子.length((……我会使用:

for(int i = sentence.length() - 1; i > 0; i--)

如果我是你。。。。