在 C++ 中,我遇到了一个我无法理解的编译器错误
In C++, I'm getting a compiler error that I can't make sense of
这是我的代码,它只是颠倒了句子:
#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
为空,您应该 static_cast
将 .length()
减去 1 之前的 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';
}
不太清楚你所说的反向是什么意思。您的代码表示我称之为“完整镜像”,其中所有字符的顺序都是倒过来的。但也有可能你只是想把单词倒过来,当人们说他们想要一个句子倒过来时,我首先想到的就是这个。根据您的需要,您的处理方式会有所不同。
对于反向单词,我需要确保每个单词都被视为自己的实体。为此,我声明了一个字符串向量,其中每个元素都是一个单独的单词。我使用 stringstream,因为它会为我分隔单词,而无需编写函数来手动检查句子。这是因为默认情况下,流以空格分隔。一旦矢量填满了单词,我就开始以相反的顺序打印它们。我用 std::ostream_iterator
来做到这一点。它只是比基于范围的 for 更紧凑,并且允许我利用向量的反向迭代器;它省去了我反转矢量的麻烦。
对于“完整镜像”,我同样使用 std::ostream_iterator
,但它是字符,因为我提供了原始句子的反向迭代器。字符串的各个元素是字符。
最后,这些倒装的句子是否需要保存也没有提示,我就不费心了。
for (int i = sentence.length() - 1; i < sentence.length(); i--)
你为什么要写一个无限循环??你正在减少 i,它总是会小于 sentence.length().... 我会使用:
for(int i = sentence.length() - 1; i > 0; i--)
如果我是你....
这是我的代码,它只是颠倒了句子:
#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
为空,您应该 static_cast
将 .length()
减去 1 之前的 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';
}
不太清楚你所说的反向是什么意思。您的代码表示我称之为“完整镜像”,其中所有字符的顺序都是倒过来的。但也有可能你只是想把单词倒过来,当人们说他们想要一个句子倒过来时,我首先想到的就是这个。根据您的需要,您的处理方式会有所不同。
对于反向单词,我需要确保每个单词都被视为自己的实体。为此,我声明了一个字符串向量,其中每个元素都是一个单独的单词。我使用 stringstream,因为它会为我分隔单词,而无需编写函数来手动检查句子。这是因为默认情况下,流以空格分隔。一旦矢量填满了单词,我就开始以相反的顺序打印它们。我用 std::ostream_iterator
来做到这一点。它只是比基于范围的 for 更紧凑,并且允许我利用向量的反向迭代器;它省去了我反转矢量的麻烦。
对于“完整镜像”,我同样使用 std::ostream_iterator
,但它是字符,因为我提供了原始句子的反向迭代器。字符串的各个元素是字符。
最后,这些倒装的句子是否需要保存也没有提示,我就不费心了。
for (int i = sentence.length() - 1; i < sentence.length(); i--)
你为什么要写一个无限循环??你正在减少 i,它总是会小于 sentence.length().... 我会使用:
for(int i = sentence.length() - 1; i > 0; i--)
如果我是你....