关于c++中string函数应用及动态创建数组

来源:百度知道 编辑:UC知道 时间:2024/06/05 12:02:39
int n=0;
class Trans
{
public:
int key;
string ciphertext;
char* plaintext;
int* ciphercode;
int* plaincode;
Trans(int k,string ct)
{
key=k;ct=ciphertext;n=strlen(ciphertext);//错误1
plaintext=new char[n];
ciphercode=new int[n];
plaincode=new int[n];
}//n=//得到长度;

int* TextToCode(string ciphertext);//暗文转暗码
int* CipherToPlain(int* ciphercode);//暗码转明码
char* CodeToText(int* plaincode);//明码转明文
void out(char* plaintext); //打印

};

int main()
{
int key=0;
string ciphertext;

cin>>key;
while(key!=0)
{
cin>>ciphertext;//错误2
Trans t(key,ciphertext);
t.out( t.CodeToText( t.CipherToPlain( t.TextToCode(ciphertext) ) ) );
cin>>key;
}
cin.get();
return 0;
}

以上程序是类的声明部分,期中n是从字符串ciphertext中得到其长度,下面通过声明动态数组的方式创建长度为n的数组,但是有两个错误:
1.er

1.
n=strlen(ciphertext.c_str()); //c_str返回C风格的字串
2.
#include<iostream>
#include<string> //string类需要用到的头文件
using namespace std;

strlen(ciphertext) 写stl的人会被你气哭的。。 string类很全面的功能不用,非要用strlen
ciphertext.length() 就是它的长度了
需要#include <string>