c++、c中string、char的常用总结2

gets输入字符串,printf输出字符串

  • c中无string基本类型
  • c++程序操作
#include <iostream>
#include <cstring>//用string 必须引用
#include <stdio.h>//用printf 必须引用
using namespace std;

char ch[20];
gets(ch);//输入,遇到换行符停止输入
string str=ch;//char数组转string型
printf("%s\n",str.data());//str.data()是char*型

c中输出char型数组

char name[12]="zhixun";
printf("%s\n",name);//输出zhixun

char型比较大小

char可以直接用> 、<、 ==这些比较大小

string替换特定字符(replace方法)

c++中string的替换,貌似没找到直接替换字符串的,感觉没Java方便

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>//必须引用
using namespace std;

replace(str.begin(),str.end(),'I','O');//用O替换I,但是这里只能是替换字符,不能是字符串
str.replace(str.find("In"),1,"Out");//str中出现In的第一个位置,把I替换成Out

string与数字的拼接

Java中直接用“+”即可,但是c++中不行

int temp=1;
string str="Sample Output";//想将str与temp拼接
temp++;

printf("%s%d\n",str.data(),temp);//输出的时候就直接拼接了,暂时只知道这个方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值