STL(一):string类

本文深入探讨C++标准模板库(STL)中的string类,包括初始化、读写操作、赋值、连接、比较等核心功能,以及成员函数的详细使用方法。

STL(一)

 

string类

C++的标准模板库 Standard Template Library 简称STL是泛型程序设计的最成功应用实例。另外一个成功的实例时Java当中的JCF(Java Collection Framework),为Java的容器框架。关于JCF的问题可以移步到我Java数据结构当中观看。

STL是一种常用的数据结构,如链表,可变长数组,排序二叉树,还有算法(如排序,查找)。STL经过程序员的精心设计,运行效率非常高效,就一些像我一样的小菜鸡写出来的代码速度更快。
 

关于STL的基本概念我这里不做过多的阐述(主要是东西多,看完之后都没心情看我的string类了(不是我不想写哦😏)),想了解可以移步到STL基本概念
 

好啦,那么开始讲讲这个string类了

哦哦,对了,讲完string类之后就不要再char*了😏
 

string的模板类为typedef basic_string string;

头文件为<string>

string对象初始化:

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

int main()
{
    string s1("Hello");
    string month="March";
    string s1(8,'x');
    //– string error1 = ‘c’;  错 
    //– string error2(‘u’);  错
    // – string error3 = 22;  错 
    //– string error4(8); // 错
    //But 可以将字符赋值给string对象
    string s;
    s='n';//赋值
    return 0;
}

example:

#include <iostream>
#include <string>
using namespace std;
int main(){
string s1("Hello");
cout << s1 << endl;
string s2(8,'x');
cout << s2 << endl;
string month = "March";
cout << month << endl;
string s;
s='n';
cout << s << endl;
return 0;
}

 

读取写入操作

1.可以用成员函数length()读取string对象的长度

string s("World");
cout<<s.length()<<endl;

 

2.支持流读取运算符

string obj;
cin>>obj;

3.支持getline函数

string s;
getline(cin ,s);

string对像的赋值
1.用等于号,=赋值。上面也说过了,也可以用单个字符赋值

string s1("Dog"),s2;
s2=s1;

2.用assign成员函数复制,也可以部分复制

string s1("Dog"),s2,s3;
s2.assign(s1);//全复制
s3.assign(s1,1,3);//下标为1开始复制3个字符给s3

3.中括号运算符单个字符复制

s2[5]=s1[3]='a';

4.逐个访问string对象

string s1("Hello!");
for(int i=0;i<s1.length();i++)
cout<<s1.at(i)<<endl;

说明一下at会做范围检查,若越界就会抛出out_of_range异常
在这里插入图片描述
但是[]不会帮你检查,但是捏,这个一般用中括号就好了。

 
string对象的连接
1.+号连接

string s1("good "), s2("morning! ");
s1 += s2;
cout << s1;

2.用成员函数append连接字符串

string s1("good "), s2("morning! ");
s1.append(s2);
cout << s1;
s2.append(s1, 3, s1.size());//s1.size(),s1字符数
//!!! 下标为3开始,s1.size()个字符,如果字符串内没有足够字符,则复制到字符串最后一个字符
cout << s2;

 
比较string
1.所有关系运算符都可以用上,返回值是bool类型
example:

string s1("hello"),s2("hello"),s3("hell");
bool b = (s1 == s2); 
cout << b << endl;
b = (s1 == s3); 
cout << b << endl;
b = (s1 > s3); 
cout << b << endl;

2.使用成员函数compare比较string大小

string s1("hello"),s2("hello"),s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1,2,s3,0,3); //s1 1-2; s3 0-3!!!
int f5 = s1.compare(0,s1.size(),s3);//s1 0-end!!!
cout << f1 << endl << f2 << endl << f3 << endl; 
cout << f4 << endl << f5 << endl;

output:
在这里插入图片描述

子串
成员函数substr

string s1("hello world"), s2;
s2 = s1.substr(4,5); // 下标4开始5个字符
cout << s2 << endl;

output:o wor

交换string
成员函数 swap

string s1("hello world"), s2("really");
s1.swap(s2); 
cout << s1 << endl;
cout << s2 << endl;

寻找string中的字符

1.成员函数find()
– string s1(“hello world”);
– s1.find(“lo”);
– 在s1中从前向后查找 “lo” 第一次出现的地方,如果找到,返
回 “lo”开始的位置,即 l 所在的位置下标。如果找不到,返回
string::npos (string中定义的静态常量)

2.成员函数 rfind()
– string s1(“hello world”);
– s1.rfind(“lo”);
– 在s1中从后向前查找 “lo” 第一次出现的地方,如果找到,返
回 “lo”开始的位置,即 l 所在的位置下标。如果找不到,返回
string::npos 。

example:

string s1("hello worlld");
cout << s1.find("ll",1) << endl;
cout << s1.find("ll",2) << endl;
cout << s1.find("ll",3) << endl; 
// 分别从下标1,2,3开始查找“ll”

3.寻找指定的任意其中一个字符
• 成员函数 find_first_of()
– string s1(“hello world”);
– s1.find_first_of(“abcd");
– 在s1中从前向后查找 “abcd” 中任何一个字符第一次出现的
地方,如果找到,返回找到字母的位置,如果找不到,返回
string::npos。

• 成员函数 find_last_of()
– string s1(“hello world”);
– s1.find_last_of(“abcd");
– 在s1中查找 “abcd” 中任何一个字符最后一次出现的地方,
如果找到,返回找到字母的位置,如果找不到,返回
string::npos

• 成员函数 find_first_not_of()
– string s1(“hello world”);
– s1.find_first_not_of(“abcd");
– 在s1中从前向后查找不在 “abcd” 中的字母第一次出现的地
方,如果找到,返回找到字母的位置,如果找不到,返回
string::npos。

• 成员函数 find_last_not_of()
– string s1(“hello world”);
– s1.find_last_not_of(“abcd");
– 在s1中从后向前查找不在 “abcd” 中的字母第一次出现的地
方,如果找到,返回找到字母的位置,如果找不到,返回
string::npos。

好啰嗦哦,最后来个example吧

#include <string>
using namespace std;

int main()
{
string s1("hello worlld");
cout << s1.find("ll") << endl;
cout << s1.find("abc") << endl;
cout << s1.rfind("ll") << endl;
cout << s1.rfind("abc") << endl;
cout << s1.find_first_of("abcde") << endl;
cout << s1.find_first_of("abc") << endl;
cout << s1.find_last_of("abcde") << endl;
cout << s1.find_last_of("abc") << endl;
cout << s1.find_first_not_of("abcde") << endl;
cout << s1.find_first_not_of("hello world") << endl;
cout << s1.find_last_not_of("abcde") << endl;
cout << s1.find_last_not_of("hello world") << endl;
    return 0;
}

output:

那个好大的数就是npos了

删除string中的字符
成员函数erase()

string s1("hello worlld");
s1.erase(5);
cout << s1;
cout << s1.length();
cout << s1.size();
// 去掉下标 5 及之后的字符
//output:hello55

替换string的字符
成员函数replace()

string s1("hello world");
s1.replace(2,3, “haha");
cout << s1;
//将s1中下标2 开始的3个字符换成“haha”
//output:hehaha world

在string中插入字符
成员函数insert()

string s1("hello world"); 
string s2(“show insert");
s1.insert(5,s2); // 将s2插入s1下标5的位置
cout << s1 << endl;
s1.insert(2,s2,5,3); 
//将s2中下标5开始的3个字符插入s1下标2的位置
cout << s1 << endl;
//output:helloshow insert world  
//heinslloshow insert world

*转换成C语言式char 字符串
1.c.str()

string s1("hello world");
printf("%s\n", s1.c_str());
// s1.c_str() 返回传统的const char * 类型字符串,且该字符串以‘\0’结尾。
//output:hello world

2.data()

string s1("hello world");
const char * p1=s1.data();
for(int i=0;i<s1.length();i++)
printf("%c",*(p1+i));
//s1.data() 返回一个char * 类型的字符串,对s1 的修改可能会使p1出错。

字符串拷贝

成员函数copy()

string s1("hello world");
int len = s1.length();
char * p2 = new char[len+1];
s1.copy(p2,5,0); 
p2[5]=0;
cout << p2 << endl;
// s1.copy(p2,5,0) 从s1的下标0的字符开始制作一个最长5个字符长度的字符串副本并将其赋值给p2。返回值表明实际复制字符串的长度。
//output:hello

实在是好啰嗦哦,最后的最后我们来see see

字符串流处理

• 除了标准流和文件流输入输出外,还可以从string进行输入输出;

• 类似 istream和osteram进行标准流输入输出,我们用istringstream 和 ostringstream进行字符串上的输入输出,也称为内存输入输出。

头文件

#include <iostream>
#include <string>
#include <sstream>

字符串流处理 - 字符串输入流 istringstream 演示

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

int main()
{
string input("Input test 123 4.7 A");
istringstream inputString(input);
string string1, string2;
int i;
double d;
char c;
inputString >> string1 >> string2 >> i >> d >> c;
cout << string1 << endl << string2 << endl;
cout << i << endl << d << endl << c <<endl;
long L;
if(inputString >> L) cout << "long\n";
else cout << "empty\n";
    return 0;
}

output:
在这里插入图片描述

再看一段代码就收工吧

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

int main()
{
ostringstream outputString;
int a = 10;
outputString << "This " << a << "ok" << endl;
cout << outputString.str();
    return 0;
}

输出:
This 10ok

学会程序和算法,走遍天下都不怕

在这里插入图片描述长白山

疫情期间各位小伙伴们注意安全

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值