第十八章 3string型字符串的赋值

论坛 期权论坛 脚本     
匿名技术用户   2021-1-8 05:17   21   0
#include <iostream>
#include <string>
using namespace std;
int main()
{
 //char ch1[] = "give ne";
 //char ch2[] = "a cup";
 //ch1 = ch2; 这样是错误的,你不能把一个数组名赋值给另一个数组名
 //strcpy(ch1,ch2);
 //strcpy(ch1,"a cup");//第二个参数可能是常量字符数组
 //第一: strcpy会将ch2中的所有字符,包括结束标志'\0'一块复制到ch1中去
 //第二: 不可以直接对char型字符串数组进行赋值操作
 // a = b; //不行
 //而只能用strcpy函数,或者对每个数组元素一个个的赋值
 //cout<<ch1<<endl;
 //cout<<ch2<<endl;
    
 
 string str1="give me";
 string str2="a cup";
 //str1 = str2;
 cout<<"str1:"<<str1<<endl;
 cout<<"str2:"<<str2<<endl;
 
 
 str1.assign(str2,3,1);
    cout<<"str1:"<<str1<<endl;
 cout<<"str2:"<<str2<<endl;
 
 return 0;
}

  

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP