一、String类的基本概念
二、String类的构造函数
三、String类的成员函数
四、String类的运算符重载
五、String类的总结
C++ String类用法介绍
C++中,String类是一个重要的数据类型,它可以方便地对字符串进行各种操作。本文将介绍String类的基本概念、构造函数、成员函数和运算符重载等方面的内容,并给出一些代码示例。
一、String类的基本概念
String类是C++标准库中的一个类,它封装了一个字符数组,并提供了一系列的方法和运算符来处理字符串。与C语言中的字符串不同,String类不需要在末尾添加’\0’来表示字符串结束,而是通过内部记录字符串长度来实现。这样可以避免一些常见的错误,如缓冲区溢出、空指针等。
String类还具有以下特点:
- String对象可以动态分配内存空间,根据需要增加或减少字符串长度。
- String对象可以直接使用输入输出流进行读写操作,无需转换为字符数组或指针。
- String对象可以和C语言中的字符串互相转换,通过data()、c_str()和copy()等方法实现。
- String对象可以使用标准库中的算法和容器进行处理,如sort()、find()、vector等。
二、String类的构造函数
String类提供了多种构造函数,用于创建不同类型和内容的字符串对象。以下是一些常用的构造函数:
- string():默认构造函数,创建一个空字符串。
- string(const char* s):从C语言中的字符串s初始化一个string对象。
- string(const string& str):复制构造函数,从另一个string对象str初始化一个string对象。
- string(size_type n, char c):创建一个包含n个字符c的string对象。
- string(const char* s, size_type n):从C语言中的字符串s中取前n个字符初始化一个string对象。
- string(const string& str, size_type pos, size_type n):从另一个string对象str中取从pos开始的n个字符初始化一个string对象。
以下是一些构造函数的代码示例:**
#include <iostream>
#include <string>
using namespace std;
int main()
{
// 使用默认构造函数创建空字符串
string s1;
cout << "s1 = " << s1 << endl; // 输出s1 =
// 从C语言中的字符串初始化
string s2("Hello");
cout << "s2 = " << s2 << endl; // 输出s2 = Hello
// 使用复制构造函数
string s3(s2);
cout << "s3 = " << s3 << endl; // 输出s3 = Hello
// 创建包含10个'a'的字符串
string s4(10, 'a');
cout << "s4 = " << s4 << endl; // 输出s4 = aaaaaaaaaa
// 从C语言中的字符串取前3个字符初始化
string s5("World", 3);
cout << "s5 = " << s5 << endl; // 输出s5 = Wor
// 从另一个string对象取从第1个位置开始(下标从0开始)的4个字符初始化
string s6(s2, 1, 4);
cout << "s6 = " << s6 << endl; // 输出s6 = ello
return 0;
}
三、String类的成员函数
String类提供了许多成员函数来对字符串进行操作,如获取长度、修改内容、查找子串等。以下是一些常用的成员函数:
size()或length():返回当前字符串长度(即字符个数)。
empty():判断当前字符串是否为空(即长度为0)。
clear():清空当前字符串内容(即长度变为0)。
- at(size_type i)或operator[](size_type i):返回当前字符串中第i个字符的引用,可以用于修改字符。如果i超出字符串范围,at()会抛出异常,而[]不会。
- append(const string& str)或operator+=(const string& str):在当前字符串末尾追加另一个string对象str的内容。
- assign(const string& str)或operator=(const string& str):用另一个string对象str的内容替换当前字符串内容。
- insert(size_type pos, const string& str):在当前字符串中pos位置插入另一个string对象str的内容。
- erase(size_type pos = 0, size_type n = npos):删除当前字符串中从pos开始的n个字符,如果n为npos(表示最大值),则删除到末尾。
- replace(size_type pos, size_type n, const string& str):用另一个string对象str的内容替换当前字符串中从pos开始的n个字符。
- substr(size_type pos = 0, size_type n = npos):返回当前字符串中从pos开始的n个字符组成的子串,如果n为npos,则返回到末尾。
- compare(const string& str):比较当前字符串和另一个string对象str的大小关系,如果相等返回0,如果小于返回负数,如果大于返回正数。
- find(const string& str, size_type pos = 0):从当前字符串中pos位置开始查找另一个string对象str第一次出现的位置,如果找不到则返回npos。
- rfind(const string& str, size_type pos = npos):从当前字符串中pos位置开始反向查找另一个string对象str第一次出现的位置,如果找不到则返回npos。
- data()或c_str():返回一个指向以’\0’结尾的字符数组(即C语言中的字符串)。
以下是一些成员函数的代码示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
// 创建两个string对象
string s1("Hello");
string s2("World");
// 获取长度
cout << "s1.size() = " << s1.size() << endl; // 输出s1.size() = 5
cout << "s2.length() = " << s2.length() << endl; // 输出s2.length() = 5
// 判断是否为空
cout << "s1.empty() = " << s1.empty() << endl; // 输出s1.empty() = 0(即false)
cout << "s2.empty() = " << s2.empty() << endl; // 输出s2.empty() = 0(即false)
// 清空内容
s1.clear();
cout << "s1.clear(), s1.size() = " << s1.size()<< endl; // 输出s1.clear(), s1.size() = 0
// 修改字符
s2[0] = 'w';
cout << "s2[0] = 'w', s2 = " << s2<< endl; // 输出s2[0] ='w', s2= world
// 追加内容
s1.append("Hello");
cout << "s1.append(\"Hello\"), s1 ="<< s1 <<"endl"; //输出s1.append("Hello"), s1= Hello
s2 += "!";
cout << "s2 += \"!\", s2 = " << s2<< endl; // 输出s2 += "!", s2 = world!
// 替换内容
s1.assign("Hi");
cout << "s1.assign(\"Hi\"), s1 = " << s1<< endl; // 输出s1.assign("Hi"), s1 = Hi
s2 = "World";
cout << "s2 = \"World\", s2 = " << s2<< endl; // 输出s2 = "World", s2 = World
// 插入内容
s1.insert(0, "Hello ");
cout << "s1.insert(0, \"Hello \"), s1 ="<< s1 <<"endl"; //输出s1.insert(0, "Hello "), s1= Hello Hi
// 删除内容
s2.erase(3);
cout << "s2.erase(3), s2 ="<< s2 <<"endl"; //输出s2.erase(3), s2= Wor
// 替换子串
string str("ABC");
str.replace(0, 3, "XYZ");
cout << "str.replace(0, 3, \"XYZ\"), str ="<< str <<"endl"; //输出str.replace(0, 3, XYZ"), str= XYZ
// 获取子串
string sub = str.substr(1);
cout << "sub = str.substr(1), sub ="<< sub <<endl; //输出sub= str.substr(1), sub= YZ
return 0;
}
四、String类的运算符重载
String类还重载了一些运算符,使得对字符串的操作更加方便和直观。以下是一些常用的运算符重载:
- operator+(const string& lhs, const string& rhs):返回一个新的string对象,表示lhs和rhs连接后的字符串。
- operator==(const string& lhs, const string& rhs):判断两个string对象是否相等,即长度相同且每个字符都相同。
- operator!=(const string& lhs, const string& rhs):判断两个string对象是否不等,即长度不同或有任意字符不同。
- operator<(const string& lhs, const string& rhs):判断lhs是否小于rhs,即按字典序比较两个字符串。
- operator>(const string& lhs, const string& rhs):判断lhs是否大于rhs,即按字典序比较两个字符串。
- operator<=(const string& lhs, const string& rhs):判断lhs是否小于等于rhs,即按字典序比较两个字符串。
- operator>=(const string& lhs,const string& rhs):判断lhs是否大于等于rhs,即按字典序比较两个字符串。
- operator<<(ostream& os, const string& str):将一个string对象str输出到输出流os中。
- operator>>(istream& is, string& str):从输入流is中读取一个string对象str,遇到空白字符(如空格、换行等)停止。
以下是一些运算符重载的代码示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
// 创建两个string对象
string s1("Hello");
string s2("World");
// 连接字符串
string s3 = s1 + " " + s2;
cout << "s3 = s1 + \" \" + s2, s3 = " << s3 << endl; // 输出s3 = s1 + " " + s2, s3 = Hello World
// 比较字符串
cout << "s1 == \"Hello\" = " << (s1 == "Hello") << endl; // 输出s1 == "Hello" = 1(即true)
cout << "s2 != \"world\" = " << (s2 != "world") << endl; // 输出s2 != "world" = 1(即true)
cout << "s1 < \"Hi\" = " << (s1 < "Hi") << endl; // 输出s1 < "Hi" = 0(即false)
cout << "\"World\" > s2 ="<< ("World" > s2) <<endl; //输出"World" > s2= 0(即false)
// 输入输出字符串
cin >> s1;
cout << "cin >> s1, s1 ="<< s1 <<"endl"; //输入abc,输出cin >> abc
return 0;
}
五、String类的总结
本文介绍了String类的基本概念、构造函数、成员函数和运算符重载等方面的内容,并给出了一些代码示例。希望本文能对你有所帮助。