萌新不看会后悔的C++string字符串常用知识点总结

时间:2022-07-28
本文章向大家介绍萌新不看会后悔的C++string字符串常用知识点总结,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一. 前言

前面学习字符串的时候简单说了string字符串和C风格字符串的不同,今天来详细的学习一下string字符串 过去学习C的时候,想要使用字符串应该是如下的格式: char a[] = “hello world”; //这里省略了,但编译器会我们自动添加并隐藏 再高级一点: char * b = “hello world”; 输入字符串: scanf("%s", a);//很多C初学者常犯的一个错就是给a取地址,其实这里的a已经是地址了,无需使用&取地址符 输出字符串: cout << a << endl; cout << b << endl; 如果我们想让两个字符串相加: strcat(a, a); 别问我为什么不使用strcat(a,b);因为程序会炸。 cout << a << endl; 或者是复制: strcpy(a, a); 上面所说的是C风格的字符串,C++的标准库增加了string类,string字符串比C语言中的字符串更加方便,更加强大,更加安全。 既然是C的超集,怎么能没有点新东西来替代C呢,嘿嘿。

二. string字符串(正题)

1. 字符串初始化,赋值,拼接,附加

进入今天的正题,string类型被定义在string头文件。

 string str_1 = "hello world";  //拷贝初始化
 string str_2 = { "hello world" }; //拷贝初始化
 string str_3("hello world");  //直接初始化
 string str_4{ "hello world" };  //直接初始化
 //可以使用上面任意一种来初始化string对象,并且string字符串是不保存''的,string对象有自己的成员函数
 //用来记录字符串大小,所以不变判断字符串结尾
 //赋值
 string str;
 char a[20] = { "abcde" };
 str = a; 
 cout << str << endl;
 char *b = { "eeee" };
 str = b;
 //拼接 附加
 cout << str << endl;
 str = str + a;
 cout << str << endl;
 str = str + b;
 cout << str << endl;

事实就是string更加方便,更加安全,更加强大,string兼容了C的字符串

运行结果:

2. 字符串长度的函数

//在C++新增string之前,想要拼接需要使用strcpy(),想要使用附加使用strcat()
 //当然,string对象也是可以使用上述函数,另外在C种操作字符串赋值时存在这样问题:
 char site[10] = { "abcdef" };
 char site_2[10] = { "aaaaa" };
 //strcat(site, site_2);
 //这样将发生错误,无法存储目标大小之外信息导致程序崩溃
 //C为了避免上述情况,提供了strncat()和strcpy()两个函数,函数多了一个参数,就是允许长度的最大值
 //求长度可以使用strlen();
 //新增的string类提供许多成员函数,用来简化繁琐的操作
 //字符串长度的函数:
 string str = "花狗";
 cout << str.size();
 cout << str.length();

size()和length()没有区别,都是返回字符串的字节数

运行结果:

3. string转换为char *

 const char * c = str.c_str();//返回以''结尾的字符串
 const char * ca = str.data();//生产的字符串有没有''不确定
 cout << c << endl;
 cout << ca << endl;

c_str()函数会返回一个以’’结尾的字符串,而data()没有标准,查看两个函数定义:

return pointer to null-terminated nonmutable array 指明c_str会返回带有空字符结尾的字符串,但是data没有提及。

4. 判断string对象是否为空

string str;
if(str.empty())
{
//……
}

5. int类型转string

#include<sstream>
 int number = 123;
 stringstream s;
 s << number;
 string str_1 = s.str(); //第一种
 s >> str_1; //这种写法也可以
 cout << str_1;
 //第二种
 int number_2 = 123;
 string str_3;
 str_3 = to_string(number_2);
 cout << str_3;

这里不单单指int类型,包括整形和浮点型,既然涉及到了stringstream,再说一个例子:

6. string类型转int

string str;
while(getline(cin,str))
{
    int num;
    stringstream s(line);
    while(ss>>x)
    {
        cout<<x<<endl;
    }
}

读者应该看出来了,这是字符串转整形,知识是需要学会灵活运用,学会了正向,也要思考逆向。

7. 向string字符串插入字符

 string str = "hello my name huagou ";
 string str_1 = "is "; 
 str.insert(14, str_1);
 cout << str;
 //insert有很多变体,如果有兴趣可自行查看

8.对string字符串进行查找

 string str = " y hello my name is zhangxv";
 string str_1 = "my";
 if (str.find(str_1)!=string::npos)//find其实还有一个参数为起始查找位置,默认为0,找到会返回下标
 {
  cout << "找到了";
 }
 //是不是C风格的字符串方便多了呢?字符和字符串都是可以查找的。
 //find函数的返回值是整数,假如字符串存在包含关系,其返回值必定不等于npos(一个常数)
 
 //num = s.find_first_of(str) 返回str出现在母串s中的首次出现的位置
 //num = s.find_last_of(str) 返回str出现在母串s中的最后一次出现的位置

不同点: find(): 查找字符串中第一次出现字符c、字符串s的位置; find_first_of(): 查找字符串中字符c、字符数组s中任意一个字符第一次出现的位置。

string字符串就写到这里喽。拜拜!