sinat_21595363 于 2016.03.02 18:07 提问

`````` /串的动态堆分配存储
//by zoe

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define MAXSIZE 100
using namespace std;

typedef struct {
char *ch;
int length;
int size;
}HString;

void InitString(HString &S){
S.ch=new char[MAXSIZE];
if(S.ch==NULL) {cerr<<"error!\n";exit(1);}
S.ch[0]='\0';
S.length=0;
S.size=MAXSIZE;
}

//字符串复制
void StrAssign(HString &S,char *chars){
S.length=strlen(chars);
strcpy(S.ch,chars);
}

int StrLength(HString S){
return S.length;
}

//若s>t,则返回值大于零；s=t，返回值等于0；s<t，返回小于零
int StrCompare(HString S,HString T){
for(int i=0;i<S.length&&i<T.length;i++){
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}

//将s2连接在s1后面
HString Concat(HString &S1,HString &S2){
int i=0,j;
if((S1.length+S2.length)>MAXSIZE){
char *dest=new char[S1.length+S2.length+1];
if(dest==NULL) {cerr<<"error!\n";exit(1);}
char *temp=dest;
for(int i=0;i<S1.length;i++) dest[i]=S1.ch[i];
delete [] S1.ch;
S1.ch=temp;
S1.size=S1.length+S2.length;
}
for(int j=0;j<S2.length;j++,i++) S1.ch[i]=S2.ch[j];
S1.ch[i]='\0';
S1.length=S1.length+S2.length;
return S1;
}

HString SubString(HString S,int pos,int len ){
HString sub;
sub.ch=(char*)malloc(len*sizeof(char));
if(!sub.ch) {cerr<<"error!\n";exit(1);}
if(pos>=S.length||pos<0||len<0||len>S.length){
sub.ch[0]='\0';sub.length=0;
}
else{
if(pos+len-1>S.length) len=S.length-pos;
for(int i=0,j=pos;i<len;i++,j++) sub.ch[i]=S.ch[j];
sub.ch[len]='\0';
sub.length=len;
}
return sub;
}

//在串s中删除与所有与串t相同的子串，并返回删除的次数
int Delet_SubString(HString &S,HString T){
int n=0;
for(int i=1;i<=(StrLength(S)-StrLength(T)+1);i++){
if(!StrCompare(SubString(S,i,StrLength(T)),T)){
StrAssign(tail,SubString(S,i+StrLength(T),StrLength(S)-i-StrLength(T)+1));
n++;
}
}
return n;
}

void PrintString(HString S){
for(int i=0;i<S.length;i++){
printf("%c ",S.ch[i]);
}
}

int main(){
HString S,T;
InitString(S);
InitString(T);
char s1[]={"abcdefgabcimcseeokabsf"};
char s2[]={"ab"};
StrAssign(S,s1);
StrAssign(T,s2);
int n=Delet_SubString(S,T);
PrintString(S);
printf("%d\n",n);
return 0;
}

``````

`````` wangxiaoxiaodeMacBook-Air:Data-Structures-and-Algorithm wangxiaoxiao\$ g++ HString.cpp -std=c++11
HString.cpp:84:4: error: no matching function for call to 'StrAssign'
^~~~~~~~~
HString.cpp:27:6: note: candidate function not viable: no known conversion from
'HString' to 'char *' for 2nd argument
void StrAssign(HString &S,char *chars){
^
HString.cpp:85:4: error: no matching function for call to 'StrAssign'
StrAssign(tail,SubString(S,i+StrLength(T),StrLen...
^~~~~~~~~
HString.cpp:27:6: note: candidate function not viable: no known conversion from
'HString' to 'char *' for 2nd argument
void StrAssign(HString &S,char *chars){
^
HString.cpp:86:4: error: no matching function for call to 'StrAssign'
^~~~~~~~~
HString.cpp:27:6: note: candidate function not viable: no known conversion from
'HString' to 'char *' for 2nd argument
void StrAssign(HString &S,char *chars){
^
3 errors generated.

``````

5个回答

caozhy      2016.03.02 18:31

void StrAssign(HString &S,char *chars)

caozhy      2016.03.02 18:53

HString Concat(HString &S1,HString &S2)

caozhy      2016.03.02 18:54
``````        StrAssign(head,SubString(S,1,i-1));
StrAssign(tail,SubString(S,i+StrLength(T),StrLength(S)-i-StrLength(T)+1));
``````

sinat_21595363 你好！多谢指教，我修改了下代码，如下：
2 年多之前 回复
sinat_21595363 你好！多谢指教，我修改了下代码，如下：
2 年多之前 回复
sinat_21595363 你好！多谢指教，我修改了下代码，如下：
2 年多之前 回复
sinat_21595363   2016.03.02 20:33
`````` //串的动态堆分配存储
//by zoe

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define MAXSIZE 100
using namespace std;

typedef struct {
char *ch;
int length;
int size;
}HString;

void InitString(HString &S){
S.ch=new char[MAXSIZE];
if(S.ch==NULL) {cerr<<"error!\n";exit(1);}
S.ch[0]='\0';
S.length=0;
S.size=MAXSIZE;
}

int StrLength(HString S){
return S.length;
}

//字符串复制
void StrAssign(HString &S,HString T){
if(S.ch) free(S.ch);
if(!T.ch) {exit(1);}
else{
if(!(S.ch=(char*)malloc(StrLength(T)*sizeof(char)))) {exit(1);}
for(int i=1;i<=StrLength(T);i++){
S.ch[i]=T.ch[i];
}
S.length=T.length;
S.size=T.size;
}
}

//若s>t,则返回值大于零；s=t，返回值等于0；s<t，返回小于零
int StrCompare(HString S,HString T){
for(int i=0;i<S.length&&i<T.length;i++){
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}

//将s2连接在s1后面
void Concat(HString &S1,HString &S2){
int i=0,j;
if((S1.length+S2.length)>MAXSIZE){
char *dest=new char[S1.length+S2.length+1];
if(dest==NULL) {cerr<<"error!\n";exit(1);}
char *temp=dest;
for(int i=0;i<S1.length;i++) dest[i]=S1.ch[i];
delete [] S1.ch;
S1.ch=temp;
S1.size=S1.length+S2.length;
}
for(int j=0;j<S2.length;j++,i++) S1.ch[i]=S2.ch[j];
S1.ch[i]='\0';
S1.length=S1.length+S2.length;
}

HString SubString(HString S,int pos,int len ){
HString sub;
sub.ch=(char*)malloc(len*sizeof(char));
if(!sub.ch) {cerr<<"error!\n";exit(1);}
if(pos>=S.length||pos<0||len<0||len>S.length){
sub.ch[0]='\0';sub.length=0;
}
else{
if(pos+len-1>S.length) len=S.length-pos;
for(int i=0,j=pos;i<len;i++,j++) sub.ch[i]=S.ch[j];
sub.ch[len]='\0';
sub.length=len;
}

return sub;
}

//在串s中删除与所有与串t相同的子串，并返回删除的次数
int Delet_SubString(HString &S,HString T){
HString sub;
InitString(sub);
int m=StrLength(S);
int n=StrLength(T);
int i=1,count=0;
while(i<=m){
StrAssign(sub,SubString(S,i,n));
if(StrCompare(sub,T)==0){//匹配相等
InitString(sub);
Concat(sub,SubString(S,1,i-1));
Concat(sub,SubString(S,i+n,m-i-n+1));
StrAssign(S,sub);
n=StrLength(S); //要注意，操作完一次后s的值改变了
count++;
}
else {
i++;
}
}
return count;
}

void PrintString(HString S){
for(int i=0;i<S.length;i++){
printf("%c ",S.ch[i]);
}
}

int main(){
HString S,T;
InitString(S);
InitString(T);
char s1[]={"abcdefgabcimcseeokabcsf"};
char s2[]={"bc"};
for(int i=1;i<=S.size;i++){S.ch[i]=s1[i];}
for(int i=1;i<=T.size;i++){T.ch[i]=s2[i];}
int n=Delet_SubString(S,T);
PrintString(S);
printf("%d\n",n);
return 0;
}

``````

`````` wangxiaoxiaodeMacBook-Air:Data-Structures-and-Algorithm wangxiaoxiao\$ g++ HString.cpp
HString.cpp:97:4: error: no matching function for call to 'Concat'
Concat(sub,SubString(S,1,i-1));
^~~~~~
HString.cpp:53:6: note: candidate function not viable: expects an l-value for
2nd argument
void Concat(HString &S1,HString &S2){
^
HString.cpp:98:4: error: no matching function for call to 'Concat'
Concat(sub,SubString(S,i+n,m-i-n+1));
^~~~~~
HString.cpp:53:6: note: candidate function not viable: expects an l-value for
2nd argument
void Concat(HString &S1,HString &S2){
^
2 errors generated.

``````

sinat_21595363   2016.03.02 20:39

`````` //串的动态堆分配存储
//by zoe

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define MAXSIZE 100
using namespace std;

typedef struct {
char *ch;
int length;
int size;
}HString;

void InitString(HString &S){
S.ch=new char[MAXSIZE];
if(S.ch==NULL) {cerr<<"error!\n";exit(1);}
S.ch[0]='\0';
S.length=0;
S.size=MAXSIZE;
}

int StrLength(HString S){
return S.length;
}

//字符串复制
void StrAssign(HString &S,HString T){
if(S.ch) free(S.ch);
if(!T.ch) {exit(1);}
else{
if(!(S.ch=(char*)malloc(StrLength(T)*sizeof(char)))) {exit(1);}
for(int i=1;i<=StrLength(T);i++){
S.ch[i]=T.ch[i];
}
S.length=T.length;
S.size=T.size;
}
}

//若s>t,则返回值大于零；s=t，返回值等于0；s<t，返回小于零
int StrCompare(HString S,HString T){
for(int i=0;i<S.length&&i<T.length;i++){
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}

//将s2连接在s1后面
void Concat(HString &S1,HString &S2){
int i=0,j;
if((S1.length+S2.length)>MAXSIZE){
char *dest=new char[S1.length+S2.length+1];
if(dest==NULL) {cerr<<"error!\n";exit(1);}
char *temp=dest;
for(int i=0;i<S1.length;i++) dest[i]=S1.ch[i];
delete [] S1.ch;
S1.ch=temp;
S1.size=S1.length+S2.length;
}
for(int j=0;j<S2.length;j++,i++) S1.ch[i]=S2.ch[j];
S1.ch[i]='\0';
S1.length=S1.length+S2.length;
}

HString SubString(HString S,int pos,int len ){
HString sub;
sub.ch=(char*)malloc(len*sizeof(char));
if(!sub.ch) {cerr<<"error!\n";exit(1);}
if(pos>=S.length||pos<0||len<0||len>S.length){
sub.ch[0]='\0';sub.length=0;
}
else{
if(pos+len-1>S.length) len=S.length-pos;
for(int i=0,j=pos;i<len;i++,j++) sub.ch[i]=S.ch[j];
sub.ch[len]='\0';
sub.length=len;
}

return sub;
}

//在串s中删除与所有与串t相同的子串，并返回删除的次数
int Delet_SubString(HString &S,HString T){
HString sub;
HString lsub;
InitString(sub);
int m=StrLength(S);
int n=StrLength(T);
int i=1,count=0;
while(i<=m){
StrAssign(sub,SubString(S,i,n));
if(StrCompare(sub,T)==0){//匹配相等
InitString(sub);
InitString(lsub);
Concat(sub,lsub=SubString(S,1,i-1));
Concat(sub,lsub=SubString(S,i+n,m-i-n+1));
StrAssign(S,sub);
n=StrLength(S); //要注意，操作完一次后s的值改变了
count++;
}
else {
i++;
}
}
return count;
}

void PrintString(HString S){
for(int i=0;i<S.length;i++){
printf("%c ",S.ch[i]);
}
}

int main(){
HString S,T;
InitString(S);
InitString(T);
char s1[]={"abcdefgabcimcseeokabcsf"};
char s2[]={"bc"};
for(int i=1;i<=S.size;i++){S.ch[i]=s1[i];}
for(int i=1;i<=T.size;i++){T.ch[i]=s2[i];}
int n=Delet_SubString(S,T);
PrintString(S);
printf("%d\n",n);
return 0;
}

``````

1.jps可以理解成Java进程的ps，也就是罗列出所有的正在运行的Java进程（这里不截图了）； 2.jstat 一个极强的一个VM监控工具；jstat -gcutil pid 可以查看对应该id进程的gc所有状况，如下图： 下面解释一下各个字母表示的意思： s0：如果了解JVM的童鞋，很清楚s0就是Java堆内存里的新生代的
JAVA基础__语法1

//============================================================================ // Name : DoubleBuffer.cpp // Author : Vit // Version : // Copyright : Your copyright notice // Descrip

#include using namespace std;class A{public: A(int i):index(i){} void display() { cout << index << endl; }private: int index;};class B{public: B(){
String str = new String("abc"), 这行代码究竟创建了几个 String 对象?
String str = new String("abc")究竟创建了几个String对象?
//写这段代码的时候，只有上帝和我知道它是干嘛的 ,现在，只有上帝知道
// 亲爱的维护者： 如果你尝试了对这段程序进行‘优化’，  并认识到这种企图是大错特错，请增加 下面这个计数器的个数，用来对后来人进行警告：  浪费在这里的总时间 = 39h /**  * 致终于来到这里的勇敢的人： 你是被上帝选中的人，英勇的、不辞劳苦的、不眠不修的来修改 我们这最棘手的代码的编程骑士。你，我们的救世主，   *人中之龙， 我要对你说：永远不要放弃，永远不要对自己失

发信人: Turkey (冬日校园主干道上的阳光), 信区: Jobs标 题: 面试题目(转载)发信站: 紫金飞鸿 (2001年11月18日14:10:17 星期天), 站内信件 【 以下文字转载自 PostGraduate 讨论区 】【 原文由 Turkey 所发表 】  1、What are the two operating mode of X86, and

#include void strcat(char *a,char *b) {     int i=0;     while(*(a+i)!='\0')     {         i++;     }     for(int j=0;*(b+j)!='\0';j++,i++)     {         *(a+i)=*(b+j);     }     *(a+i)='\