C++ String类常用操作总结

1 构造函数

  • string():构造空字符串。
  • string(int length, char ch): 以length为长度的ch字符的拷贝(即length个ch)。
  • string(const char *str): 以str为初值(长度任意)。
  • string(string &str, int index, int length): 以index为索引开始的子串,长度为length。
  • 还可使用赋值来构造字符串。

举例:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str1(5, 'c');  // 构造一个长度为5,字符为 'c' 的字符串
    cout << str1 << endl; // 输出 ccccc
    
    string str2("abcdef");  // 以字符串 "bbbbb" 构造 str2
    cout << str2 << endl;  // 输出 abcdef
    
    string str3(str2, 2, 2);  // 以字符串 str2 的索引为2,长度为2的字符串;
    cout << str3 << endl;     // 输出 cd

	string str4 = "aaaaa";  // 使用赋值构造字符串
	cout << str4 << endl;   // 输出 aaaaa
    
    return 0;
}

2 操作符

可以使用 ==><>=<=!=等操作符比较字符串;可以使用 ++= 操作符连接两个字符串;可以使用[]获取特定字符;

举例:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string a = "abc";
    string b = "bcd";
    
    // 比较,若表达式为真返回1,为假返回0
    // 字符串比较是按字典序来比的 
    cout << (a > b) << endl;   // 0
    cout << (a < b) << endl;   // 1
    cout << (a >= b) << endl;  // 0
    cout << (a <= b) << endl;  // 1
    cout << (a != b) << endl;  // 1
    
    // 拼接 +/+=
    cout << a + b << endl;     // abcbcd
    b += a;
    cout << b << endl;         // bcdabc
    
    // []取索引下标处的值
    cout << b[1] << endl;      // c
    
    return 0;
}

3 操作


  • append(): 末尾添加,添加情况 strstr, index, lenstr, numnum, ch,这些是apennd的参数顺序。
  • 以下从上面四种添加情况进行举例:
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // 由于append直接对a字符串后进行追加,会改变原有内容,为了测试每次操作后改回"abc"
    string a = "abc";
    // 添加一个字符串 (str)
    cout << a.append("abc") << endl; // abca
    a = "abc"; 
    // 添加一个字符串,从索引1开始,长度为2  (str,index,num)
    cout << a.append("abc", 1, 2) << endl; // abcbc
    a = "abc";
    // 添加一个字符串,从索引0开始,长度为2  (str,num)
    cout << a.append("abc", 2) << endl; // abcab
    a = "abc";
    // 添加一个5个字符'e'  (num, ch)
    cout << a.append(5, 'e') << endl; // abceeeee
    
    return 0;
}

  • at(): 该函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at()将报告"out of range"错误,并抛出out_of_range异常。

举例:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "ABCDEFG";
    char ch = str.at(4); 
    cout << ch << endl;  // 输出 E
    
    return 0;
}

  • begin() & end(): begin()函数返回一个迭代器,指向字符串的第一个元素;end()函数返回一个迭代器,指向字符串的最后一个元素。常用于sort等函数。

举例:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str1 = "abasfzxvef";
    sort(str1.begin(), str1.end());
    cout << str1 << endl;  // 输出 aabeffsvxz 
    
    return 0;
}

  • compare():以多种方式比较本字符串和str,返回情况:
返回值情况
-1this < str
0this == str
1this > str

举例:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str1 = "abasfzxvef";
    string str2 = "bbbbb";
    cout << str2.compare(str1) << endl;  // 1 此时this > str
    cout << str1.compare(str2) << endl;  // -1 此时this < str
    
    string str3 = "bbbbb";
    cout << str2.compare(str3) << endl;  // 此时this == str
    
    return 0;
}

  • bool empty(): 如果字符串为空则empty()返回真(true),否则返回假(false).

举例:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str1 = "abc";
    cout << str1.empty() << endl;  // false 0
    
    string str2;
    cout << str2.empty() << endl;  // true 1
    
    return 0;
}

  • erase():

(1) iterator erase( iterator pos ):删除pos指向的字符,返回指向下一个字符的迭代器;
(2) iterator erase( iterator start, iterator end ):删除从start到end的所有字符,返回一个迭代器,指向被删除的最后一个字符的下一个位置;
(3)basic_string &erase( size_type index = 0, size_type num = npos ):删除从index索引开始的num个字符,返回*this

参数index 和 num 有默认值, 这意味着erase()可以这样调用:只带有index就删除index后的所有字符,或者不带有任何参数删除所有字符.

举例:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s("abcdefghijklmnopqrstuvwxyz");
    cout << "原字符串:" << s << endl;  // abcdefghijklmnopqrstuvwxyz
  
    s.erase(10,14);  // 删除从10开始,长度为14的字符串
    cout << "现字符串:" << s << endl;  // abcdefghijyz

    s.erase(5);  // 删除从索引5开始的字符串
    cout << "现字符串:" << s << endl;  // abcde

    s.erase();  // 删除全部
    cout << "现字符串:" << s << endl;  // 删完了 无
    
    return 0;
}

未完待续

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值