首页 > 编程语言 > 详细

c++模板库字符串的基本读取处理

时间:2020-01-29 16:59:28      阅读:67      评论:0      收藏:0      [点我收藏+]
//c++中字符串的处理获取一行函数
#include <iostream>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    cout<<s<<endl;
}
//c++中对于操作符重载的一些现象
#include <iostream>
using namespace std;
int main()
{
    string s1,s2;
    int a=5;
    s1+="hello";//根据c++的重载,会将hello加入到字符串中
    s1+=" world!" ;//与上面的规则相同
    s1+=b;//可以加入单独的字符,不一定为字符串 
    s1+=98;//如果加数字的话将会对应的看作为ascii码,从而转换为对应的字符 
    cout<<s1<<endl;
    s2+=(a+0);//想把常量a所代表的数字加到字符串中
    cout<<s2<<endl; 
    return 0;
}
//c++中字符串的排序 
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
    string s="adefcbg";
    cout<<*s.begin()<<endl;//输出字符串的第一个字母
    cout<<*--s.end()<<endl;//s.end()代表了字符串的末尾‘/0‘,因此需要前移一位才能读到‘g’
    sort(s.begin(),s.end());//定位好开始和结束的位置之后即可以实现排序 
    cout<<s<<endl;
    return 0;
}

 

//c++中字符串的删除 
#include <iostream>
using namespace std;
int main()
{
    string s="adefcbg";
    s.erase(s.begin());//调用erase删除字符串的第一个字符
    s.erase(--s.end());//调用erase删除字符串的最后一个字符 
    cout<<s<<endl;
    return 0;
}

 

 


 

c++模板库字符串的基本读取处理

原文:https://www.cnblogs.com/zmachine/p/12240639.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!