C++之 string学习

学习C艹的初衷就是stl与string,这里是重点。

  • string对象的创建
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1;
    string s2 = "c plus plus";
    string s3 = s2;
    string s4 (5, 's');
    return 0;
}
  • string的一些方法
string s = "http://c.biancheng.net";
int len = s.length();
cout<<len<<endl;
//输入输出是正常的,就是C++语法
  • 转换为C风格的代码
string path = "D:\\demo.txt";
FILE *fp = fopen(path.c_str(), "rt");
  • 字符串的拼接
    第一印象是C的string.h中的strcat,这里啥都不用了,集合了方法。
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first ";
    string s2 = "second ";
    char *s3 = "third ";
    char s4[] = "fourth ";
    char ch = '@';

    string s5 = s1 + s2;
    string s6 = s1 + s3;
    string s7 = s1 + s4;
    string s8 = s1 + ch;
    
    cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl;

    return 0;
}

哈哈,字符串可以直接相加,这不就是我以前用的C+嘛???

  • 增删查改

增:

string& insert (size_t pos, const string& str);

很明显,这是string类的一个方法,重载完了,可以按照需要使用,阿门。

删:

string& erase (size_t pos = 0, size_t len = npos);

erase,老橡皮擦了,不需要在C中那样删除后平移加上结束符号,芜湖!
pos 表示要删除的子字符串的起始下标,len 表示要删除子字符串的长度。如果不指明 len 的话,那么直接删除从 pos 到字符串结束处的所有字符(此时 len = str.length - pos)。

  • 提取子字符串
string substr (size_t pos = 0, size_t len = npos) const;

和上面erase差不多,如果pos越界就异常喽,len越界没事,全删除!

  • 字符串查找

find():

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;

str和s是查找的字符串,pos自然就是查找开始的角标。

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.find(s2,5);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

rfind():
rfind() 和 find() 很类似,同样是在字符串中查找子字符串,不同的是 find() 函数从第二个参数开始往后查找,而 rfind() 函数则最多查找到第二个参数处,如果到了第二个参数所指定的下标还没有找到子字符串,则返回一个无穷大值4294967295。

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.rfind(s2,6);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

正好给的6,可以让rfind()方法找到,给5都找不到!

find_first_of() :
find_first_of() 函数用于查找子字符串和字符串共同具有的字符在字符串中首次出现的位置。(说没用吧,有点用,说有用吧,平时用不上。)

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first second second third";
    string s2 = "asecond";
    int index = s1.find_first_of(s2);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}
  • 总结
    总的来说就是增删查改,取到字符串性质,可以修改为C语言的字符串形式。还有一个鸡肋功能find_first_of(),先不说别的,就这个合并字符串的功能就足够我用它了,因为以前用C#编程,我还是挺习惯的。
    毕竟string只是一个类库,说到底就是集合了char*的类库,装载了很多实用的功能。走,去stl!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值