C/C++ STL String详解

string容器

简介:用于存储字符串,与C语言不同没用硬性规定以\0结尾。

1.构造string

string <name>;  //生成空字符串

string <name> = "hello world";   //生成字符串

char str[20]//创建字符数组

string <name> = str//赋值字符串

#include<string>
#include<iostream>
using namespace std;
int main(){
    char s[20];
    scanf("%s",&s);    
    string str;//定义string类
    str="hello world";//初始化
    str=s;//字符数组直接赋值
    cout>>str;
    return 0;
}

2.length()

返回值为字符串的长度。例:<name>.length()

#include<string>
#include<iostream>
using namespace std;
int main(){
    string str;//定义string类
    str="hello world";//初始化
    str=s;//字符数组直接赋值
    cout>>str.length();//字符串长度
    return 0;
}

3.c_str()和data()

在C++11中,c_str()data()是同样作用,都是用来获取底层字符串的首地址的,其返回值是一个指向C风格字符串的临时指针,不能对其进行操作,内容与string字符串相同。

(1)C和C++字符串的互转 

要用strcpy()函数进行转换。

#include<string>
#include<iostream>
using namespace std;
int main(){
    string str="hello world";
    //定义string类
    char c[20];
    strcpy(c,str.c_str());
    printf("%s",c);
    return 0;
    
}

4.字符串连接 

有两种操作一种是两个字符串直接用加号+连接,另一种是用函数append( )连接。

#include<string>
#include<iostream>
using namespace std;
int main(){
    string str0;
    str="hello world";
    string str1;
    str1="hello";
    str=str0+str1;
    //用加号连接
    str0.append("h");
    //用函数连接
    return 0;
}

5.字符串的比较大小

C语言用strcmp()来比较大小,而C++用compare()来比较大小,同时C++也可以直接用符号>、<、== 来进行大小比较。 

(1)compare()

字典(ASCII)顺序比较,左边的ASCII码值大于右边ASCII码值的返回正数,等于返回0,小于返回负数。

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

int main(){
    string str0="hello world";
    string str1="hello world";
    string str2="j";
    string str3="hello my new world";
    cout<<str0.compare(str1)<<endl;
    cout<<str0.compare(str2)<<endl;
    cout<<str0.compare(str3)<<endl;
    if(str0>stt1)//可用符号直接比大小,也可用str.compare(stt1)比较,结果与strcmp相同
        cout<<str0;
    else 
        cout<<stt1;
}

6.字符串查找find()

其返回值是要查找的子串首次出现第一个字符的下标。

#include<string>
#include<iostream>
using namespace std;
int main(){
    string str1="helloello";    
    int n=str1.find("ell");//查找子串返回下标
    cout<<n;
}

输出:

#因为首次出现ell的e的下标为1。

7.字符串与数值的转换

sstream 库包含ostringstreamistringstreamstringstream这三个类。其中ostringstream用来将数值转换为字符串,istringstream用来将字符串转换成数值,而stringstream二者皆有之。

​(1)stringstream用法 

stringstream的会根据输入<<和输出>>来自动进行字符串和数值的转换。每次进行多种类型转换后都要调用clear(),防止出错。


#include<string>
#include<iostream>
#include<sstream> 
using namespace std;
int main(){
    stringstream stream;//定义流
    string str1="123";    
    int n=1;
   	stream<<str1;
   	stream>>n;
   	stream.clear();//使用stringstream对象实现多种类型的转换,每一次转换之后都必须调用clear()成员函数。 
    cout<<n;
}
​

(2)istringstream和ostringstream的使用 

istringstream:支持>>输出多个数值

ostringstream:支持<<输出字符串(一般可用于大数计算)

例: istringstream——输出多个数值

#include<string>
#include<iostream>
#include<sstream> 
using namespace std;
int main(){
     string str="1 1234 567";
    istringstream istream(str);
    int n;
    istream>>n;
    cout<<n<<endl;
    istream>>n;
    cout<<n<<endl;
    istream>>n;
    cout<<n<<endl;
}

输出:

1

1234

567 

例: ostringstream——输出字符串

#include<string>
#include<iostream>
#include<sstream> 
using namespace std;
int main(){
    int strs=161234567;
    ostringstream ostream;
    ostream<<strs;//放入字符转换流
    string str=ostream.str();//调用函数取出字符串
    cout<<str<<endl;
}

(3)to_string()用法

将非字符串类型的变量转换为字符串类型。

#include<bits/stdc++.h>
using namespace std;
int main(){
    int strs=161234567;
    string str=to_string(strs);
    cout<<str<<endl;
}

 8.字符串替换replace()

<name>.replace(<index>,<n>,<string>):表示从下标index开始连续的n个字符替换成string字符串。

#include<string>
#include<iostream>
using namespace std;
int main(){
    string str="hello";    
    str.replace(1,2,"132");
   //从下标1的字符开始连续的2个字符替换成132 
}

9.字符串切割substr() 

 <name>.substr(<index>,<n>):取出从下标index开始连续的n个字符。

#include<string>
#include<iostream>
using namespace std;
int main(){
    string str="123456";
    string s=str.substr(1,3);
    //从下标1开始的三个字符,也就是123
    cout<<s<<endl;
}

输出:

234 

10. 字符串的删除erase()和清空clear()

erase(<index>,<n>):删除从下标index开始连续的n个字符。

clear():清空字符串。

#include<string>
#include<iostream>
using namespace std;
int main(){
    string str="123456";
    str.erase(1,3);//删除从下标1开始的三个字符。
    cout<<str<<endl;
    str.clear();//清空字符串
    cout<<str<<endl;
}

 输出:

156

<空>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值