【问题标题】:terminate called after throwing an instance of 'std::length_error' what(): basic_string::_M_create在抛出 \'std::length_error\' what(): basic_string::_M_create 的实例后调用终止
【发布时间】:2022-11-14 05:25:04
【问题描述】:

问题是反转字符串中的单词...

Eg. - This is Nice
Output -Nice is This

所以这是错误

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_M_create

这是我的实际代码,不知道哪里出错了我刚开始使用 c++,但我确定我正在尝试访问未定义的索引。如果我错了,请纠正我

string reverseWords(string s) {
        vector<string> v;
        string x="";
        for(int i=0;i<s.size();i++)
        {
            if(isspace(s[i]))
            {
                v.push_back(x);
                x="";
                v.push_back(" ");
                
            }
            else
            {
                x=x+s[i];
            }
        }
        v.push_back(x);
        x="";
        for(int j=v.size();j>=0;j--) x=x+v[j];
        return x;
    }

【问题讨论】:

  • 在for(int j=v.size();j&gt;=0;j--) x=x+v[j]; 的第一次迭代中,j 的值是多少,向量中有多少个元素?你说行不行?
  • 供参考 - 问题是反转字符串中的单词-- 只需一个简单的std::stack 和std::stringstream 即可解决此问题。
  • 您用来学习 C++ 的 C++ 教科书章节的主题是什么,这个练习题来自哪里?显然,为了完成此编码任务,您应该使用本章讨论的任何主题,有必要了解这一点以便为您指明正确的方向。
  • 至于我的建议,std::stringstream 会自动找出空格的位置,因此您无需编写代码来执行此操作。仅此一项就可以消除您拥有的大部分错误代码。
  • 最后的循环超出了向量的范围,即未定义的行为.您需要在v.size()-1 开始循环,但如果向量为空则要小心。使用反向迭代器迭代向量将是更好的选择。

标签: c++ string algorithm reverse function-definition


【解决方案1】:

你的方法效率低下。

此外,您并没有反转源字符串,而是以相反的顺序构建一个新字符串。

if 语句的复合语句

if(isspace(s[i]))
{
    v.push_back(x);
    x="";
    v.push_back(" ");
    
}

当源字符串包含相邻空格时没有多大意义,因为它将空字符串附加到向量。

此外,如果字符串的最后一个字符是空格,则在 for 循环之后执行此语句

v.push_back(x);

再次将冗余的空字符串附加到向量。

这个for循环

for(int j=v.size();j>=0;j--) x=x+v[j];

当j 等于v.size() 时调用未定义的行为。

如果您正在处理std::string 类型的对象,那么您应该使用类的方法和标准算法,例如算法std::reverse。

这是一个演示程序,展示了如何反转字符串。

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

std::string & reverseWords( std::string &s, const char *delim = " 	" )
{
    auto first = s.find_first_not_of( delim );

    if (first != std::string::npos)
    {
        auto last = s.find_last_not_of( delim ) + 1;
        
        std::reverse( std::next( std::begin( s ), first ), std::next( std::begin( s ), last ) );

        while ( first != last )
        {
            auto pos = s.find_first_of( delim, first );

            if (pos == std::string::npos) pos = last;

            std::reverse( std::next( std::begin( s ), first ),
                std::next( std::begin( s ), pos ) );

            first = pos;

            if ( first != last ) first = s.find_first_not_of( delim, first );
        }
    }

    return s;
}

int main()
{
    std::string s( "The quick brown fox jumps over the lazy dog" );

    std::cout << s << '
';
    std::cout << reverseWords( s ) << '
';
}

程序输出为

The quick brown fox jumps over the lazy dog
dog lazy the over jumps fox brown quick The

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多