【C++ 】string类常见用法总结,超有用,算法竞赛必备

1.头文件
#include<string>
2.构造函数
string s1;  		// si = ""
string s2("Hello"); // s2 = "Hello"
string s3(4, 'K');  // s3 = "KKKK"
string s4("12345", 1, 3);  //s4 = "234",即 "12345" 的从下标 1 开始,长度为 3 的子串
3.对 string 对象赋值
string s1, s2, s3;
s1 = "Hello";  	// s1 = "Hello"
s2 = 'K';  		// s2 = "K”
cin >> s3;  	//s3从标准输入得到

tring s1("12345"), s2;
s3.assign(s1);  		// s3 = s1
s2.assign(s1, 1, 2); 	// s2 = "23",即 s1 的子串(1, 2)
s2.assign(4, 'K');  	// s2 = "KKKK"
s2.assign("abcde", 2, 3);  // s2 = "cde",即 "abcde" 的下标 2 开始,长度为 3 的子串
4.string 和 char */char[]字符串 相互转化

将 char* 或者 char[] 转换为 string

string str;
const char *s = "qwert";
str=s //直接转化

将string 转换为 char* 或者 char[]

char *p;
string str = "aqwea";
p = str.c_str();
//或者
p = str.data();
5.string长度
str.sizeof();
6.string 转 int、float、double

超级实用!
当然也可以把整数 浮点数转为字符串,将流的输入和输出对调一下即可。 当要进行多次数据类型转换时,需要先使用 clear() 清空流,不然之前的数据会影响下一次数据类型转换。

#include <string>
#include <sstream>  // 包含头文件

int main()
{
    std::stringstream str2digit;
    std::string sint='1', sfloat='1.1', sdouble='1.2';
    int dint;
    float dfloat;
    double ddouble;
 
    str2digit << sint; str2digit >> dint;  // string to int
    str2digit.clear();
    str2digit << sfloat; str2digit >> dfloat;  // string to float
    str2digit.clear();
    str2digit << sdouble; str2digit >> ddouble;  // string to double

    std::cout << dint << ", " << dfloat << ", " << ddouble << std::endl;

    return 0;
}
//本代码来源:知乎
//作者:赵剑行
//链接:https://www.zhihu.com/question/355533269/answer/1379154630
7.string 的 push_back 和 pop_back

这个比较实用,可以实现在string字符串尾部添加或删除一个字符

string a="123";
1.在字符串末尾添加一个字符
a.push_back('3'); //结果为 a="1233";
2.在字符串末尾删除一个字符
string a="123";
a.pop_back(); //结果为 a="12";
8.string 的子串
string s1 = "this is ok";
string s2 = s1.substr(2, 4);  // s2 = "is i"
s2 = s1.substr(2);  // s2 = "is is ok"
9.交换两个 string 对象的内容
string s1("West”), s2("East");
s1.swap(s2);  // s1 = "East",s2 = "West"
10.find成员函数查找子串和字符
各种find 比较作用
find从前往后查找子串或字符出现的位置
rfind从后往前查找子串或字符出现的位置
find_first_of从前往后查找何处出现另一个字符串中包含的字符。例如: s1.find_first_of(“abc”); //查找s1中第一次出现”abc”中任一字符的位置
find_last_of从后往前查找何处出现另一个字符串中包含的字符。
find_first_not_of从前往后查找何处出现另一个字符串中没有包含的字符
find_last_not_of从后往前查找何处出现另一个字符串中没有包含的字符
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1("Source Code");
    int n;
    if ((n = s1.find('u')) != string::npos) //查找 u 出现的位置
        cout << "1) " << n << "," << s1.substr(n) << endl;
    //输出 l)2,urce Code
    if ((n = s1.find("Source", 3)) == string::npos)
        //从下标3开始查找"Source",找不到
        cout << "2) " << "Not Found" << endl;  //输出 2) Not Found
    if ((n = s1.find("Co")) != string::npos)
        //查找子串"Co"。能找到,返回"Co"的位置
        cout << "3) " << n << ", " << s1.substr(n) << endl;
    //输出 3) 7, Code
    if ((n = s1.find_first_of("ceo")) != string::npos)
        //查找第一次出现或 'c'、'e'或'o'的位置
        cout << "4) " << n << ", " << s1.substr(n) << endl;
    //输出 4) l, ource Code
    if ((n = s1.find_last_of('e')) != string::npos)
        //查找最后一个 'e' 的位置
        cout << "5) " << n << ", " << s1.substr(n) << endl;  //输出 5) 10, e
    if ((n = s1.find_first_not_of("eou", 1)) != string::npos)
        //从下标1开始查找第一次出现非 'e'、'o' 或 'u' 字符的位置
        cout << "6) " << n << ", " << s1.substr(n) << endl;
    //输出 6) 3, rce Code
    return 0;
}
11.替换子串

replace 成员函数可以对 string 对象中的子串进行替换,返回值为对象自身的引用。例如:

string s1("Real Steel");
s1.replace(1, 3, "123456", 2, 4);  //用 "123456" 的子串(2,4) 替换 s1 的子串(1,3)
cout << s1 << endl;  //输出 R3456 Steel
string s2("Harry Potter");
s2.replace(2, 3, 5, '0');  //用 5 个 '0' 替换子串(2,3)
cout << s2 << endl;  //输出 HaOOOOO Potter
int n = s2.find("OOOOO");  //查找子串 "00000" 的位置,n=2
s2.replace(n, 5, "XXX");  //将子串(n,5)替换为"XXX"
cout << s2 < < endl;  //输出 HaXXX Potter
12.删除子串

erase 成员函数可以删除 string 对象中的子串,返回值为对象自身的引用。例如:

string s1("Real Steel");
s1.erase(1, 3);  //删除子串(1, 3),此后 s1 = "R Steel"
s1.erase(5);  //删除下标5及其后面的所有字符,此后 s1 = "R Ste"
13.插入字符串

insert 成员函数可以在 string 对象中插入另一个字符串,返回值为对象自身的引用。例如:

string s1("Limitless"), s2("00");
s1.insert(2, "123");  //在下标 2 处插入字符串"123",s1 = "Li123mitless"
s1.insert(3, s2);  //在下标 3 处插入 s2 , s1 = "Li10023mitless"
s1.insert(3, 5, 'X');  //在下标 3 处插入 5 个 'X',s1 = "Li1XXXXX0023mitless"

参考资料: http://c.biancheng.net/view/400.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值