【问题标题】:In C++, I'm getting a compiler error that I can't make sense of在 C++ 中,我遇到了一个我无法理解的编译器错误
【发布时间】:2020-11-03 22:50:41
【问题描述】:

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

#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.

【问题讨论】:

  • 检查你的 for 循环条件。
  • i &lt; sentence.length() 即使对于 i 的负值也是如此
  • 我们是在反向说完整的,还是只是倒序的单词?
  • 您应该在reversedSentence 中分配空间,然后再将其视为数组。默认声明的字符串是空的。

标签: c++ gcc compiler-errors g++ mingw


【解决方案1】:

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

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

另请注意,您的循环条件需要修改。如果sentence 为空,您应该将static_cast .length() 转换为int,然后再减1,如下所示:

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 字符串为空时的复杂情况。

【讨论】:

    【解决方案2】:

    你的 for 循环说 i &lt; sentence.length() 是结束条件。这导致它始终为 true 并且永远不会访问您的 for 循环,因为您将 i 声明为 sentence.length() - 1。这总是小于sentence.length()

    【讨论】:

      【解决方案3】:

      我的建议:不要使用索引。尽可能使用迭代器。

      for (auto iter = sentence.rbegin(); iter != sentence.rend(); ++iter)
      {
          reversedSentence.push_back(*iter);
      }
      

      【讨论】:

        【解决方案4】:
        #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,但它是字符,因为我提供了原句的反向迭代器。字符串的各个元素都是字符。

        最后,没有提示是否需要保存这些颠倒的句子,所以我不打扰。

        【讨论】:

          【解决方案5】:
          for (int i = sentence.length() - 1; i < sentence.length(); i--)
          

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

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

          如果我是你……

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-13
            • 2021-11-13
            • 1970-01-01
            • 2018-01-25
            相关资源
            最近更新 更多