关于字符串旋转的错误测验:std :: out_of_range:basic_string

时间:2017-04-20 11:27:16

标签: c++

我在这里更新鲜。

我为一个叫做字符串旋转的小测验写了一个类:给定一个字符串及其长度和一个特定的位置,返回一个旋转的刺。例如" ABCDEFGH",8,4 返回:" FGHABCDE"

该类已通过在线检测,但在运行测试cpp文件时在我的计算机中工作不正常,错误显示:

libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string (lldb)

这是班级:

#ifndef basic_string_rotate_hpp
#define basic_string_rotate_hpp

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
class StringRotation {
public:
    string rotateString(string A, int n, int p) {
        string B,C;
        B = A.substr(p+1,n-(p+1)); // back value
        C = A.substr(0,p+1); // front value
        A = B + C;               // write code here
        return A;
    }
};
#endif /* basic_string_rotate_hpp */

这是我用它来测试上一课的cpp文件:

#include "basic_string_rotate.hpp"
#include<iostream>
#include<string>
#include<vector>
using std::cin; using std::cout; using std::endl; using      std::string;using std::vector;
int main()
{
    string A = "cao ni ma bi", new_A; // initialize must use " ",   rather than this ' '
    int p = 3;
    auto n = (int)(A.size());
    StringRotation string;
    cout << string.rotateString(A,p,n);

    return 0;
}

0 个答案:

没有答案
相关问题