#include "stdafx.h"
#include <iostream.h>
#include <string.h>
int main(int argc, char* argv[])
{
char ch1[]="what 's your name";
char ch2[]="my name is jack";
//strcpy(ch1,ch2);
//strcpy会将ch2中的所有字符,包括结束标志"/0"一起复制到ch1中去
//不能直接对char型字符串数组进行赋值操作,如:ch1=ch2;
strcpy(ch1,"what is your name ");
cout<<ch1<<endl;
cout<<ch2<<endl;
return 0;
}
由于在string类中使用operator函数重载了运算符,对象之间可以赋值。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1="gh";
string str2="abcde ";
str1.assign(str2,3,1);
cout<<str1<<endl;
return 0;
}
<string.h>是旧的C 头文件,对应的是基于char*的字符串处理函数; <string>
是包装了std 的C++头文件,对应的是新的string 类(看下文); <cstring>是对
应于旧C 头文件的std 版本。