【C++】string(含字符串数组)相关⽤法前⾔
1、在 Leetcode 做题时,便想顺道总结下 string 的⼀些⽤法,免得忘了。
2、博主⽤的是线上⽹页来运⾏ C++ 代码,感兴趣的朋友可以⽤,挺简洁的。
3、遇到感兴趣的 string ⽤法,本⽂相应增加修改。
⼀、截取字符串中的字符 substr
1、常⽤格式:
// 默认截取从 0 到 npos.
// 重载原型为string substr(_off = 0,_count = npos);
// npos ⼀般表⽰为 string 类中不存在的位置, _off表⽰字符串的开始位置,_count截取的字符的数⽬
substr()
// 设字符串长度为 n,从字符串下标为 3 开始,向后截取到下标 n - 1 为⽌
substr(3)
// 从字符串下标为 0 开始,向后截取 x 位
substr(0, x)
2、演⽰代码
#include<string>
#include<iostream>
using namespace std;
int main()
{
string str="Welcome to my blog";
cout<<str.substr()<<endl;
cout<<str.substr(3)<<endl;//截取 str[5] 到结尾
cout<<str.substr(0,3)<<endl;//以 str[0]为始,向后截取3位(包含x[0])
cout<<str.size()<<endl;  // 获取 str 长度
}
⼆、获取 string 字符串长度
1、常⽤⽅式:
// ⽤于获取字符串的长度,可⽤ str.size();
string str = "welcome";
cout<< str.size() <<endl;  // 7,第⼀种⽅法
cout<< str.length() <<endl;  // 7,第⼆种⽅法
cout<< strlen(str.c_str()) <<endl;  // 7,第三种⽅法,常⽤于 C 语⾔
/*
c_str()函数返回⼀个指向C字符串的指针常量,
指向的内容是字符串对象的当前内容加上⼀个额外的终⽌字符(‘\ 0’)。
因为在c语⾔中没有string类型,
所以必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
*/
// ⽤于获取字符串数组的长度,可⽤ sizeof(strs)/sizeof(string)
string strs[] = {"Welcome", "to", "my", "blog"};
cout<< strs->size() <<endl;  // 等价于strs[0].size(),答案为 7
cout << strs[0].size() << endl; // 7
cout << strs[1].size() << endl; // 2
cout<< sizeof(strs) <<endl;  // 128
cout<< sizeof(strs)/sizeof(string) <<endl;  // 4,求字符串数组 strs 的长度,第⼀种⽅法cout<< sizeof(strs)/sizeof(strs[0]) <<endl;  // 4,第⼆种⽅法
// cout << strs[1][0] <<endl;  // 输出 t
2、演⽰
三、排序
1、⽤ sort 、reverse 排序
sort(first_pointer, first_pointer + n, cmp)
// 该函数可以给数组,或者链表list、向量排序。
// 参数1:数组的⾸地址,⼀般写上数组名就可以,因为数组名是⼀个指针常量。// 参数2:⾸地址加上数组的长度 n(代表尾地址的下⼀地址)。
// 参数3:默认不填按数组升序排序。
2、上代码
#include <iostream>
#include <algorithm>
#include<cstring>
using namespace std;
int main()
{
string str = "welcome";
sort(str.begin(), d());  // 头⽂件记得加上 #include <algorithm> cout<< str.c_str() <<endl;
reverse(str.begin(),d());  // 反向排序
cout<< str.c_str() <<endl;
// 字符串数组
string strs[] = {"Welcome", "to", "my", "blog"};
int n = sizeof(strs)/sizeof(strs[0]);
sort(strs, strs+n);  // 正序
for(int i = 0; i < n; i++){
cout<< strs[i] <<' ';
}
cout<<endl;
reverse(strs, strs+n);  // 反序
for(int i = 0; i < n; i++){
cout<< strs[i] <<' ';
}
}
/* 输出:
ceelmow
womleec
Welcome blog my to
to my blog Welcome
*/
3、演⽰
4、还有⼀种⽅法,参考了
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
void sort_string(string *in_array, int n, string *out_array)
{
vector<string> strArray;
int i, j = 0;
for (int i = 0; i < n; i++)
{
strArray.push_back(in_array[i]);
/
/ push_back 函数将⼀个新的元素加到vector的最后⾯,位置为当前最后⼀个元素的下⼀个元素
}
sort(strArray.begin(), d()); // 正序
vector<string>::iterator st;
// string字符串对象的迭代器iterator实现其中之⼀的格式(正向迭代器):
// 容器类名::iterator  迭代器名;
for (st = strArray.begin(); st != d(); st++)
{
//cout << *st << endl;  //打印结果
out_array[j++] = *st;
}
}
int main()
{
//string str[4] = {"Welcome", "to", "my", "blog"};
string str[5];
int i = 0;
while(cin >> str[i++]){
if(i == 4) break;
}
string str_out[4];
sort_string(str, 4, str_out);
for (int j = 0; j < 4; j++)
cout << str_out[j] << ' ';
}
// 输出:c++中string的用法
// Success #stdin #stdout 0s 5696KB
// blog my to welcome
5、Leetcode,翻转字符串⾥的单词,下⾯是⼀位⼤佬写的解法,感兴趣的朋友可以点下⾯链接去了解

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。