[英]I'm new to C++ and I got this error help. I'm pretty sure its an easy fix but the answers i looked up didn't make sense
[英]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
为空,则应在减 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';
}
有点不清楚你所说的反转是什么意思。 您的代码表示我称之为“全镜”的东西,其中所有字符的顺序都是倒着的。 但是也有可能你只是想把单词倒过来,这是当人们说他们想要一个句子倒过来时我首先想到的。 根据您的需要,您 go 关于它的方式会有所不同。
对于反向的单词,我需要确保每个单词都被视为自己的实体。 为此,我声明了一个字符串向量,其中每个元素都是一个单独的单词。 我使用字符串流,因为它会为我分隔单词,而无需编写 function 来手动检查句子。 这是因为默认情况下流在空格上分开。 一旦向量被填满的话,我 go 关于以相反的顺序打印它们。 我用std::ostream_iterator
做到这一点。 它比基于范围的 for 更紧凑,并且允许我利用向量的反向迭代器; 它省去了我反转向量的麻烦。
对于“完整镜像”,我同样使用std::ostream_iterator
,但它是字符的,因为我提供了原始句子的反向迭代器。 字符串的各个元素是字符。
最后,没有提示是否需要保存这些倒置的句子,所以我不打扰。
for (int i = sentence.length() - 1; i < sentence.length(); i--)
你为什么要写一个无限的for循环? 你正在减少 i,它总是小于 sentence.length().... 我会使用:
for(int i = sentence.length() - 1; i > 0; i--)
如果我是你....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.