c++中string类型的讲解

string数据类型

在C++中,字符串(String)是一种非常重要的数据类型,用于存储和操作文本。C++提供了多种方式来处理字符串,包括C风格的字符串(字符数组)和std::string类。在这里,我们将主要关注std::string类,因为它提供了更安全、更方便的接口来处理字符串数据。

1. std::string类的基本使用

在C++中,std::string是标准库中的一个类,用于表示和操作字符串。要使用它,你需要包含头文件<string>

#include <iostream>
#include <string>
​
int main() {
    std::string greeting = "Hello, World!";
    std::cout << greeting << std::endl;
    return 0;
}

2. 访问字符串中的字符

你可以像使用数组那样访问字符串中的单个字符,使用下标操作符[]

std::string s = "Hello";
char firstChar = s[0];  // 'H'
char secondChar = s[1]; // 'e'

3. 修改字符串

std::string对象是可修改的。你可以改变字符串中的字符或者使用+运算符添加新的字符或字符串。

std::string s = "Hello";
s[0] = 'J';             // s变为"Jello"
s += ", World!";        // s变为"Jello, World!"

4. 字符串长度和容量

使用length()size()成员函数可以获取字符串的长度。capacity()函数可以返回字符串分配的存储空间大小。

std::string s = "Hello";
std::cout << "Length: " << s.length() << std::endl; // 输出字符串长度
std::cout << "Capacity: " << s.capacity() << std::endl; // 输出字符串容量

5. 字符串的比较、赋值和拼接

字符串可以使用==!=<>等运算符进行比较。你可以使用=赋值给另一个字符串,使用+append()方法进行字符串拼接。

std::string a = "Hello";
std::string b = "World";
std::string c = a + " " + b; // "Hello World"
bool isEqual = (a == b);     // false

6. 查找和替换

std::string提供了查找和替换字符串中子串的方法,如find()replace()

std::string s = "Hello, World!";
size_t pos = s.find("World");  // pos = 7
if (pos != std::string::npos) {
    s.replace(pos, 5, "C++");  // s变为"Hello, C++!"
}

7. 子串

使用substr()方法可以从字符串中获取子串。

std::string s = "Hello, World!";
std::string sub = s.substr(7, 5); // "World"

8. 其他有用的成员函数

std::string类还提供了其他很多有用的成员函数,如empty()用于检查字符串是否为空,clear()用于清空字符串内容,insert()用于在指定位置插入字符串等。

通过学习和实践这些基本操作,你将能够有效地在C++中使用字符串进行各种操作。实际编程中,字符串的使用非常频繁,掌握这些基础知识对于提高编程能力至关重要。

9.代码展示

#include<iostream>//标准输入输出流库
#include<string>

int main() {//一个项目中只能有一个main函数

	//--------------------------------------------------

	//hello world项目
	std::cout << "hello world" << std::endl;
	std::cout << std::endl;

	//--------------------------------------------------

	//基础数据类型
	int a = 47;
	float b = 13.1;
	double c = 34.356;
	char d = 'd';
	bool e = true;

	std::cout << "int:" << a << std::endl;
	std::cout << "float" << b << std::endl;
	std::cout << "double" << c << std::endl;
	std::cout << "char" << d << std::endl;
	std::cout << "bool" << e << std::endl;
	std::cout << std::endl;

	//--------------------------------------------------

	//string类型
	//先引入头文件
	std::string hello = "hello world";
	std::cout << std::endl;
	//字符串可以操作具体某个字符
	std::cout << hello << std::endl;
	std::cout << hello[0] << hello[1] << std::endl;
	std::cout << std::endl;
	//字符串的字符可以改
	hello[0] = 'k';
	std::cout << hello << std::endl;
	std::cout << std::endl;
	//字符串的长度或者容量
	std::string s1 = "hello";
	std::cout << "length:" << s1.length() << std::endl;
	std::cout << "length:" << s1.size() << std::endl;//长度
	std::cout << "Capacity:" << s1.capacity() << std::endl;//容量
	std::cout << std::endl;
	//字符串的加减
	std::string stra = "Hello1";
	std::string strb = "2World";
	std::string strc = stra + " " + strb; // "Hello World"
	std::cout << strc << std::endl;
	std::cout << std::endl;
	//find,size_t,replace
	std::string s2 = "Hello,World!";
	size_t pos = s2.find("World");  // pos = 6
	std::cout << pos << std::endl;
	if (pos != std::string::npos) {
		s2.replace(pos, 5, "C++");  // s变为"Hello, C++!"
		std::cout << s2 << std::endl;
	}
	std::cout << std::endl;
	
	return 0;
}

  • 24
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值