3

C++ string 基本用法

 1 year ago
source link: https://blog.51cto.com/u_15740457/5983491
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一、C++string的创建方式

1、string的头文件

#include <iostream>//等价于C语言中的#include<stdio.h>
#include <cstring>
#include <string.h>
//以上两种都是C语言的string头文件
#include <string>//真正的C++头文件

在C++中,#include<string>才是真正的C++头文件!

2、补充小知识

在C++结构体中是可以创建函数的,并且引用方法跟正常变量一样。

#include <iostream>
using namespace std;
struct MM
{
int age;
void print()
{
cout<<age<<endl;
}
};
int main()
{
//如何访问函数
MM mm;
mm.age=1001;
mm.print();
return 0;
}

3、常用的创建string方式

int main()
{
string str1 = "str1";
cout << str1 << endl;//调用构造函数
string str2;
str2 = "str2";
cout << str2 << endl;//调用拷贝构造函数
string str3 = ("str3");
cout << str3 << endl;
return 0;
}

输出结果:

C++ string 基本用法_#include

4、少见的初始化方式

string str4(4,'x');

用4个字符初始化字符串,输出结果就是xxxx

string str5("iloveyou",1,4);

表示用字符串的从下标1开始的4位初始化str5(在C++中,字符串的下标也是从0开始的,跟C语言的数组一样)

故输出结果就是love

二、C++ string与C语言char*的区别

1、最本质的区别

在于C++string定义的字符串中没有\0!

char arr[]="iloveyou";
string str5="iloveyou";
cout<<str5.size()<<endl;
cout<<str5.length()<<endl;

C++中两种输出字符串长度的函数——length(),size()

上面的代码前者用char定义的一共有8+1个字符,二下面用string定义的输出结果就是8个字符

2、如何访问string定义的字符串?

首先外面不能用C语言里面的printf()函数访问,因为本质上string类型不是字符串,

C++ string 基本用法_string详解_02

若强行访问,输出结果就是乱码!

C++string为我们提供访问字符串的接口——data(),c_str();

这两个函数将string类型转换为字符串类型,方便我们利用printf函数打印。

int main()
{
string str1 = "str1";
cout << str1 << endl;
printf("%s\n", str1);
printf("%s\n", str1.data());
printf("%s\n", str1.c_str());
return 0;
}

C++ string 基本用法_string详解_03

三、C++ string的基本操作

1、字符串比较

在C++中,字符串比较的规则与C语言相同。

(1)字符串如何比较

问:“123”和“13”相比,哪个字符串大?

答:“13”更大,因为从左到右比较,字符按照ASCII码值比较,第一个字符都是‘1’,第二个字符‘3’比‘2’大,所以“13”比“123”大。

在C++中,可以cout直接输出判断字符串的比较

(2)cout比较实操

int main()
{
string first = "123";
string second = "13";
cout << (first < second) << endl;
cout << (first != second) << endl;
cout << (first > second) << endl;
return 0;
}
  • 因为返回值的问题,所以要用()把比较的式子括起来
  • 返回值是bool类型,若是真,返回1,假就返回0

(3)调用比较成员的函数compare

cout << first.compare(second) << endl;

巧记compare返回值

返回值为1:first-second>0 ------>  first>second

返回值为-1:first-second<0 ------>  first<second

返回值为0:first-secnotallow=0 ------>  first=second

2、字符串连接

(1)cout直接法

int main()
{
string first = "i love you,";
string second = "i miss you";
cout << first + second << endl;
return 0;
}

想要把first和second两个字符串进行相加,直接在cout里面使用加法 + ! 

输出结果就是,i love you,i miss you

(2)通过append()函数连接

因为上述cout直接+,只能将两个完整的字符串进行相加,太笨重,而append()函数更加灵活,可以决定连接第几位开始,且数量。

C++ string 基本用法_#include_04
int main()
{
string first = "i love you,";
string second = "i miss you";
cout << first + second << endl;
first.append(second);
cout << first << endl;
first.append(second, 1, 5);
cout << first << endl;
first.append(4, '1');
cout << first << endl;
first.append("i miss you", 1, 5);
cout << first << endl;
return 0;
}
C++ string 基本用法_#include_05

3、字符/字符串查找

string查找类型的函数,如果查找不到,就返回-1。

因此在查找时,一般需要判断返回值是否等于string::nops(值就是-1)

(1)常用的find函数和rfind函数查找

find函数从左往右查找字符串或者字符出现的位置。

rfind函数从右往左查找字符串或者字符出现的位置。

int main()
{
string first = "i love you,";
string second = "i miss you";
if (first.find('o') != -1)
{
cout << first.find('o') << endl;
}
if (first.find("love") != -1)
{
cout << first.find("love")<< endl;
}
return 0;
}

(2)一些不太常用的查找函数

find_first_of:第一次出现的子串(要查找字符串的任意一个字符)左->右

find_last_of:第一次出现的子串(要找字符串的任意一个字符)右->左

find_first_not_of:第一次不是子串的位置(左→右)

find_last_not_of:第一次不是子串的位置(右→左)

举个栗子:
int main()
{
string first = "iloveyou,";
string second = "imissyou";
if (first.find_first_of("you") != -1)
{
cout << (first.find_first_of("you")) << endl;
}
return 0;
}

上面的代码我们查找子串为“you”,但子串是其中的任意一个字符,从左往右最先出现的子串中的任意一个字符的位置。o最先出现,位置是2,输出结果就是2!

4、字符串的替换(replace函数)

C++字符串的替换很灵活

replace函数的参数一般开始是原始想要替换的下标,之后是替换字符的个数。

int main()
{
string first = "iloveyou,";
string second = "imissyou";
first.replace(1, 4,second,1,4);
cout << first << endl;
return 0;
}

输出结果就是imissyou

C++ string 基本用法_string详解_06

5、字符串的删除(erase函数)

int main()
{
string first = "iloveyou,";
string second = "imissyou";
first.erase(0);
cout << first << endl;
second.erase(1, 4);
cout << second << endl;
return 0;
}

第一种使用方法是删除下标0,及以后的字符(注意是后面的字符全部干掉!)

第二种是用方法是删除下标1,之后的4个字符(自定义删除的个数)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK