string的总结;

输入输出;

1.

cin.getline();属于iostream

char m[20];
cin.getline(m,5);//输入名称和长度;
cout<<m<<endl;

2.getline()属于string;

string s;
getline(cin,s);
cout<<str<<endl;

3.读掉换行

str="\n";
getline(cin,str);

                                                string

1.c_str()

1.
const char* c; 
string s="1234"; 
c = s.c_str(); 
cout<<c;


2.
string s = "Hello World!";
printf("%s", s.c_str()); //输出 "Hello World!"

2.c语言里面 ,所以要转换为数组,atoi(),atof()为double

跳过前面的空格字符串,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串‘\0’时才结束转换,并将结果返回
const char* c; 
string s;
cin>>s;
c=s.c_str();

int n=atoi(c);
cout<<n<<endl;
输入"-100"输出-100;

                                                        string

1.find() 

	string s="ifisfs";
	if(s.find("is")!=string::npos)
	cout<<s.find("is");//输出2 

2.erase()

string str = "welcome to my blog";

//s.erase(pos,n)           把字符串s从pos开始的n个字符删除
str.erase(11,3);           // str = "welcome to blog"

str.erase(2) 删除2以后的字符;  左闭右开

3.

string str = "The apple thinks apple is delicious";

//s.substr(pos,n)                      得到字符串s位置为pos后面的n个字符组成的串
string s1 = str.substr(4,5);           // s1 = "apple"

//s.substr(pos)                        得到字符串s从pos到结尾的串
string s2 = str.substr(17);            // s2 = "apple is delicious"
  左闭右开

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值