一.string类型简介
string类型译为“字符串”,和字符数组差不多,但多了一些函数,更方面、快捷。
string类型不在#include<iostream>下,调用string类型代码如下:
#include<string>
二.string的读入
读入string类型有两种情况:
1.无空格
这种情况最好办,直接用cin函数就可以了。
下面是一个示例代码,输入什么输出什么:
#include<iostream>
#include<string>
using namespace std;
int main{
string s;
cin>>s;
cout<<s;
}
输出:
2.有空格
其实这也较为简单,不过要用一个新函数:getline()。
C++中的getline函数可以用来从输入流中读取一行字符串。它的基本语法如下:
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
getline(cin,s);
cout<<s;
return 0;
}
在该例子中,我们在头文件中引入了iostream和string库。在主函数中,我们定义了一个名为s的字符串变量,然后调用getline函数从标准输入流(cin)中读取一行字符串,并将其存储在字符串变量s中。最后,我们输出这个字符串。
三、string的遍历
1.位数函数string.size()
当遍历一个string的字符串,必要的就是要知道位数。string类型怎么获得位数呢?用size()函数!
C++中string.size()函数是一个非常常用的函数,它的用法是获取容器的元素个数。
对于string,可以使用size()函数获取它们的大小。
示例使用:
#include<iostream>
#include<string>
using namespace std;
int main(){
string s="hello world";
int x=s.size();
cout<<x;
}
输出结果:
11
需要注意的是,string.size()函数返回的是一个无符号整型,因此不要用负数与size()的结果进行比较。
2.查找字符函数find()
C++中的String类型有一个find函数,其用法如下:
size_t find(const string& str, size_t pos = 0) const noexcept;
其中,第一个参数是要查找的子字符串,第二个参数是查找的起始位置,默认为0。该函数返回值为子字符串在原字符串中第一次出现的位置,如果没有找到,则返回-1。
使用示例:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello, world!";
string str2 = "world";
int pos = str1.find(str2);
if (pos != string::npos) {
cout << "Found at position " << pos << endl;
} else {
cout << "Not found." << endl;
}
return 0;
}
输出:
Found at position 7
在上述示例中,我们在字符串str1中查找子字符串str2,发现其在位置7处第一次出现。
如果我们想往后找第二个,可以在后面加一个下标,表示在这个下标以后查找。
四、string数据的添加和删除
1、string的添加
string函数添加有两种方法,都可以将元素相加。
Ⅰ、“+”号
string类型不同于字符数组,它可以直接将两个元素相加,但是只能将元素添加到末尾,有局限性。
下面是示例代码:
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1,s2,s3;
s1="hello ";
s2="world!";
s3=s1+s2;
cout<<s3;
return 0;
}
这个代码会输出:
hello world!
Ⅱ、insert()函数
C++中的string类提供了insert()函数,用于在字符串中插入字符或字符串。其函数原型为:
string insert (size_t pos, const string& str);
string insert (size_t pos, const string& str, size_t subpos, size_t sublen);
string insert (size_t pos, const char* s);
string insert (size_t pos, const char* s, size_t n);
string insert (size_t pos, size_t n, char c);
参数说明:
- pos:插入位置,即在哪个位置插入字符串
- str:要插入的字符串
- subpos:要插入的子字符串的起始位置
- sublen:要插入的子字符串长度
- s:要插入的字符数组
- n:要插入的字符数组长度
- c:要插入的字符
例如,在字符串str的位置6插入字符串"hello",可以这样使用insert()函数:
string str = "world";
str.insert(6, "hello");
cout << str; // 输出"worldhello"
或者在字符串str的位置2插入字符'x',可以这样使用:
string str = "hello";
str.insert(2, 1, 'x');
cout << str; // 输出"hexllo"
2、string类型的删除
Ⅰ、erase()函数
C++的string类型的erase()函数用于删除字符串中的字符或子串。函数的用法如下:
string.erase(pos, n);
其中,pos是要删除的第一个字符的位置,n是要删除的字符数。如果不指定n,则默认删除从pos开始到末尾的所有字符。例如:
string str = "Hello World";
str.erase(2, 3); // 删除从位置2开始的3个字符,即 "llo"
cout << str << endl; // 输出 "He World"
3、string的替换
Ⅰ、replace()函数
C++中的string类提供了replace()函数,它用来替换字符串中的子串,其用法如下:
string str = "hello world";
str.replace(start_position, length, new_str);
其中,start_position为要替换子串的起始位置,length为要替换子串的长度,new_str为替换后的新字符串。
举个例子,比如要将字符串"hello world"中的"world"替换成"universe",可以这样写:
string str = "hello world";
str.replace(6, 5, "universe");
cout << str << endl; // 输出:hello universe
注意,替换后的新字符串的长度可以与原字符串的长度不同,因为替换的子串可以是任意长度。
五、与string有关的类型转换
1.string转其他类型
Ⅰ、string转char类型
可以使用string类的构造函数来将char类型转化为string类型,例如:
char c = 'a';
string str = string(1, c);
其中,构造函数的第一个参数是要重复的字符数量,第二个参数是要重复的字符。
如果要将一个包含多个字符的char数组转化为string类型,可以使用字符串字面值来初始化string对象,如下所示:
char arr[] = "hello";
string str = arr;
2.string转数据类型
Ⅰ、int(Stoi()函数)
C++的stoi()函数是将字符串转换为整数的函数。该函数在头文件< string >中定义,主要的用法格式如下:
int stoi (const string& str, size_t* pos = 0, int base = 10);
其中,str为要转换的字符串,pos存储的是转换结束的位置,base表示要转换的进制数,默认为10进制。
例如,将字符串"123"转换为整数可以使用以下代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "123";
int num = stoi(str);
cout << num << endl;
return 0;
}
其输出结果为:
123
需要注意的是,如果要转换的字符串不是合法的数字格式,如转换失败,函数会抛出std::invalid_argument异常。
Ⅱ、long类型(stol()函数)
C++中的stol()函数用于将字符串转换为长整型。它的定义如下:
long int stol (const string& str, size_t* idx = 0, int base = 10);
其中,str表示要转换的字符串;idx是一个可选参数,用于存储转换后的字符串到哪个位置,如果转换失败,则idx会指向转换失败的字符位置;base是表示数字的进制,默认为10进制。
使用stol()函数的示例代码如下:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "100";
long int num = stol(str);
cout << "num = " << num << endl;
return 0;
}
运行结果为:
num = 100
Ⅲ、long long类型(stoll()函数)
C++的stoll()函数用于将字符串转换为long long类型的整数。它的语法如下:
long long std::stoll(const std::string& str, std::size_t* pos = 0, int base = 10);
其中,str是要转换的字符串;pos是一个可选参数,用于指定转换后第一个非数字字符的位置;base是一个可选参数,用于指定转换时的进制,默认为10进制。
例如:
#include <iostream>
#include <string>
int main() {
std::string str = "1234567890123456789";
long long num = std::stoll(str);
std::cout << "num = " << num << std::endl;
return 0;
}
输出:
num = 1234567890123456789
需要注意的是,如果str不能被转换为long long类型的整数,则stoll()函数会抛出std::invalid_argument异常。另外,如果转换得到的整数超出了long long类型的范围,则stoll()函数会抛出std::out_of_range异常。
3、其他类型转string类型
Ⅰ、to_string()函数
C++中的to_string()函数用于将数字类型的数据转换为字符串类型。它将一个int、float、double或long long类型的值作为参数,并返回一个对应的string类型的值。
语法如下:
string to_string(int val);
string to_string(long val);
string to_string(long long val);
string to_string(unsigned val);
string to_string(unsigned long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);
例如,以下代码将数字类型的值转换为字符串类型并输出:
#include <iostream>
using namespace std;
int main() {
int num = 123;
string str = to_string(num);
cout << "The converted string is: " << str << endl;
return 0;
}
输出为:
The converted string is: 123
需要注意的是,如果要将其他类型的数据转换为字符串类型,例如字符型、布尔型等,需要使用string类型的构造函数或字符串流进行转换。例如:
char ch = 'A';
string str = string(1, ch); // 使用构造函数
cout << "The converted string is: " << str << endl;
bool flag = true;
stringstream ss;
ss << flag;
string boolstr = ss.str(); // 使用字符串流
cout << "The converted string is: " << boolstr << endl;
输出为:
The converted string is: A
The converted string is: 1
六、总结
string类型较Char数组多了一些函数,所以好用一些。
你学会了吗?