C++ String

1.2 std::string

std::string(字符串),是C++标准库中用于表示和字符串的类,它提供了许多成员函数和操作符,用于处理字符串的各种操作。当我们使用std::string的时候,我们需要提前包含头文件:

#include <string>
using namespace std;		//如果没有这句,我们在使用时必须指明命名空间std::string	

1.2.1 string的定义

std::string可以像普通类型一样进行定义。

std::string str;

1.2.2 string的初始化

std::string可以直接使用字符串字面量来进行初始化,或者使用另一个字符串变量来进行初始化,例如:

std::string str1 = "Hello, World!";		  // 使用字符串字面量来初始化
std::string str2 = str1;				  // 使用另一个字符串变量来初始化

举个能实际运行的例子:

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

int main() {
    std::string str1 = "Hello World!";
    std::string str2 = str1;
    cout << str1 << endl << str2;
    return 0;
}

运行结果:

Hello World!
Hello World!

1.2.3 string中的元素的访问和修改

std::string有两种访问和修改字符的方式,std::string支持原地修改,但std::string不支持切片操作,可以使用std::string::substr()方法来代替。

(1)使用[]进行元素访问和修改

举个例子:

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

int main() {
	string str = "Hello World!";
	cout << str << endl;
	cout << str[0] << endl;			//使用[]进行元素访问
	str[0] = 'C';					//使用[]进行元素修改
	cout << str;
	return 0;
}

运行结果:

Hello World!
H
Cello World!
(2)使用at进行元素修改和访问

举个例子:

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

int main() {
    std::string str = "Hello World!";
    cout << str << endl;
    cout << str.at(0) << endl; // 使用at进行元素访问
    str.at(0) = 'C';           // 使用at进行元素修改
    cout << str;
    return 0;
}

运行结果:

Hello World!
H
Cello World!

1.2.4 string的遍历

(1)range-based for循环遍历
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "Hello, World!";
    for (char ch : s)
    {
        cout << ch << " ";
    }
    return 0;
}
(2)使用迭代器遍历
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "Hello, World!";
    for (string::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << " ";
    }
    return 0;
}
(3)使用下标遍历
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "Hello, World!";
    for (int i = 0; i < s.size(); i++)
    {
        cout << s[i] << " ";
    }
    return 0;
}

1.2.5 string中连接字符串

std::string可以像Java中string一样进行使用+进行拼接,并且可以使用append()方法在末尾添加字符。

(1)使用+进行拼接,+会返回一个新的字符串
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str1 = "Hello";
    cout << str1 << endl;
    string str2 = " World!";
    cout << str2 << endl;
    cout << str1 + str2; // 使用+进行字符串拼接
    return 0;
}

运行结果:

Hello
World!
Hello World!
(2)使用append()进行拼接,在原字符串的基础上进行添加
#include <iostream>
#include <string>
using namespace std;

int main()
{
    std::string str1 = "Hello";
    cout << str1 << endl;
    std::string str2 = " World!";
    cout << str2 << endl;
    str1.append(str2); // 使用append进行字符串拼接
    cout << str1;
    return 0;
}

运行结果:

Hello
World!
Hello World!

1.2.6 string中的常用函数

length():获取字符串的长度

size():获取字符串的长度

substr():获取字符串的子串,第一个参数是索引,第二个参数是子串的长度

find():查找子串的位置

resize():直接调整string的长度

stoi():将单个字符串转换成int类型

stoll():将单个字符串转换成long long类型

push_back():将单个字符拼接在末尾

pop_back():弹出末尾的字符

(1)length()和size()
#include <iostream>
#include <string>
using namespace std;

int main()
{
    std::string str = "Hello World!";
    cout << str.length() << " " << str.size();
    return 0;
}

运行结果:

12 12
(2)substr()

substr()是用于获得子串的函数,第一个位置是索引,第二个位置是子串的长度

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

int main()
{
    std::string str = "Hello World!";
    cout << str << endl;
    cout << str.substr(0, 4);
    return 0;
}

运行结果:

Hello World!
Hell
(3)find()

find()是用于查找子串的位置,返回子串首元出现在母串中的位置。

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

int main()
{
    std::string str = "Hello World!";
    std::string substr = "orld";
    cout << str.find(substr) << endl;
    return 0;
}

运行结果:

7

返回的是orld在字符串Hello World!中首元的位置,在下标7的地方。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值