Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关于字符串旋转测验的错误:std::超出范围:基本字符串_C++ - Fatal编程技术网

C++ 关于字符串旋转测验的错误:std::超出范围:基本字符串

C++ 关于字符串旋转测验的错误:std::超出范围:基本字符串,c++,C++,我是这里的新生 我为一个名为“字符串旋转”的小测验编写了一个类:给定一个字符串及其长度和特定位置,返回一个旋转的sting。e、 g“ABCDEFGH”,8,4 返回:“FGHABCDE” 该类已通过联机检测,但在我运行测试cpp文件时在我的计算机中工作不正确,错误显示: libc++abi.dylib:以std::out_of_range:basic_string类型的未捕获异常终止 (lldb) 下面是课堂: #ifndef basic_string_rotate_hpp #define b

我是这里的新生

我为一个名为“字符串旋转”的小测验编写了一个类:给定一个字符串及其长度和特定位置,返回一个旋转的sting。e、 g“ABCDEFGH”,8,4 返回:“FGHABCDE”

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

libc++abi.dylib:以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 */
\ifndef基本\u字符串\u旋转\u水电站
#定义基本\u字符串\u旋转\u hpp
#包括
#包括
#包括
使用名称空间std;
类字符串旋转{
公众:
字符串旋转字符串(字符串A、整数n、整数p){
B、C串;
B=A.substr(p+1,n-(p+1));//返回值
C=A.substr(0,p+1);//前值
A=B+C;//在这里编写代码
返回A;
}
};
#endif/*基本字符串旋转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;
}
#包括“基本字符串旋转.hpp”
#包括
#包括
#包括
使用std::cin;使用std::cout;使用std::endl;使用std::string;使用std::vector;
int main()
{
string A=“cao ni ma bi”,new_A;//初始化必须使用“”,而不是此“”
int p=3;
自动n=(int)(A.size());
串旋转串;

你能不能把参数倒过来。你把它叫做
string.rotateString(A,p,n);
但是你把它定义为
rotateString(string A,int n,int p)
。函数中的
n
应该是大小还是旋转位置?这是什么
string A=“cao ni ma bi”,new_A
?删除
,new_A
为什么在使用std::string对象隐式获取大小时显式传递大小?(即使使用一个好的旧c字符串,您仍然可以应用strlen…)实际上您是对的..我太笨了…它应该是string.rotateString(A,n,p),而不是string.rotateString(A,p,n)