STL之string常用库函数笔记

STL之string常用库函数笔记

  • [x] string的构造函数形式
  • [x] string的大小和容量
  • [x] string的字符串比较
  • [x] string的插入
  • [x] string的拼接
  • [x] string的遍历
  • [x] string的删除
  • [x] string的字符替换
  • [x] string大小写转换
  • [x] string的查找
  • [x] string的排序
  • [x] string的分割

1.string的构造函数形式

  • string s (str) :生成字符串s为str的复制品

  • string s(str, str_begin, str_len):将字符串str中从下标str_begin开始、长度为strlen的部分作为字符串s的初值。

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string a("0123456789");
        string b(a,0,5);
        string c(a,1,8);
        cout<<a<<endl<<b<<endl<<c<<endl;
        return 0;
    }
    
    /*
    0123456789
    01234
    12345678
    
    Process returned 0 (0x0)   execution time : 0.120 s
    Press any key to continue.*/
    

2.string的大小和容量

  • size()和length():返回string对象的字符个数,他们执行效果相同。
  • max_size():返回string对象最多包含的字符数,超出会抛出length_error异常
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a("0123456789");
    cout<<a.length()<<endl;
    cout<<a.size()<<endl;
    cout<<a.max_size()<<endl;
    return 0;
}
/*
10
10
2147483647

Process returned 0 (0x0)   execution time : 0.112 s
Press any key to continue.*/

3.string的字符串比较

  • 比较操作符: >, >=, <, <=, ==, !=
        这些操作符根据“当前字符特性”将字符按字典顺序进行逐一比较,字典排序靠前的字符小,比较的顺序是从前向后比较,遇到不相等的字符就按这个位置上的两个字符的比较结果确定两个字符串的大小(前面减后面)
  • 成员函数compare()
        支持多参数处理,支持用索引值和长度定位子串进行比较,前面减去后面的ASCII码,>0返回1,<0返回-1,相同返回0;
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a("0123456789");
    string b("01245");
    string c("acv");
    cout<<a.compare(c)<<endl;
    cout<<b.compare(c)<<endl;
    cout<<a.compare(b)<<endl;
    if(a<b)
        cout<<"a<b"<<endl;
    return 0;
}
//-1
-1
-1
a<b

Process returned 0 (0x0)   execution time : 0.069 s
Press any key to continue.

4.string的插入

  • string的插入:push_back() 和 insert();
    // 尾插一个字符
    s1.push_back('a');
    s1.push_back('b');
    s1.push_back('c');
    cout<<"s1:"<<s1<<endl; // s1:abc

    // insert(pos,char):在制定的位置pos前插入字符char
    s1.insert(s1.begin(),'1');
    cout<<"s1:"<<s1<<endl; // s1:1abc

5.string的拼接

  • string拼接字符串:append() 、 +;
//方法一:append()
string s1("abc");
s1.append("def");
cout<<"s1:"<<s1<<endl; // s1:abcdef

// 方法二:+ 操作符
string s2 = "abc";
/*s2 += "def";*/
string s3 = "def";
s2 += s3.c_str();
cout<<"s2:"<<s2<<endl; // s2:abcdef

6.string的遍历:借助迭代器 或者 下标法

  • 正向迭代器 str.begin()、str.end()
  • 反向迭代器 str.rbegin()、str.rend()
  • 下标
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s1("abcdef");
    // 下标
    for(int i=0;i<s1.length();i++)
        cout<<s1[i];
    cout<<endl;
    // 正向迭代器
    string::iterator iter = s1.begin();
    for( ; iter < s1.end() ; iter++)
    {
        cout<<*iter;
    }
    cout<<endl;
    // 反向迭代器
    string::reverse_iterator riter = s1.rbegin();
    for( ; riter < s1.rend() ; riter++)
    {
        cout<<*riter;
    }
    cout<<endl;
    return 0;
}
// 
abcdef
abcdef
fedcba

Process returned 0 (0x0)   execution time : 0.075 s
Press any key to continue.

7.string的删除

  • iterator erase(iterator p):删除字符串中p所指的字符
  • iterator erase(iterator first, iterator last):删除字符串中迭代器区间 [first, last) 上所有字符
  • string.erase(size_t pos, size_t len):删除字符串中从索引位置 pos 开始的 len 个字符
  • void clear():删除字符串中所有字符
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s1("abcdef");
    s1.erase(s1.begin()+1);
    cout<<s1<<endl;
    string s2("I Love China");
    s2.erase(s2.begin()+1,s2.end()-1);
    cout<<s2<<endl;
    string s3("I Love China");
    s3.erase(1,7);
    cout<<s3<<endl;
    return 0;
}
// 
     > cd "f:\Yqifei_javacode\" ; if ($?) { g++ qi.cpp -o qi } ; if ($?) { .\qi }
acdef
Ia
Ihina
PS F:\Yqifei_javacode>

8.string的字符替换

  • string& replace(size_t pos, size_t n, const char s):将当前字符串从pos索引开始的n个字符,替换成字符串s
  • string& replace(size_t pos, size_t n, size_t n1, char c):将当前字符串从pos索引开始的n个字符,替换成n1个字符c
  • string& replace(iterator i1, iterator i2, const char s):将当前字符串[i1,i2)区间中的字符串替换为字符串s
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s1("hello,world!");
    string s2("hello,world!");
    string s3("hello,world!");
    s1.replace(6,6,"girl");
    s2.replace(0,12,"boy,girl");
    cout<<s1<<endl;
    cout<<s2<<endl;
    return 0;
}
//
hello,girl
boy,girl

9.string大小写转换:tolower() 和 toupper() 或者 STL中的 transform 算法

  • tolower(char) 和 toupper(char) :将字符进行大小写转换
  • transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func):transform [_First, _Last) with _Func
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s1("hello,world!");
    for(int i=0;i<s1.size();i++){
        s1[i]=toupper(s1[i]);
    }
    cout<<s1<<endl;
    string s2("hello,world!"),s3;
    transform(s2.begin(),s2.end(),s2.begin(),::toupper);
    cout<<s2<<endl;
    return 0;
}
// 
HELLO,WORLD!
HELLO,WORLD!

10.string的查找:find

  • find (const char* s, size_t pos):在当前字符串的pos索引位置开始,查找子串s,返回找到的位置索引
  • find (char c, size_t pos):在当前字符串的pos索引位置开始,查找字符c,返回找到的位置索引
string s("dog bird chicken bird cat");

//字符串查找-----找到后返回首字母在字符串中的下标
// 1. 查找一个字符串
cout << s.find("chicken") << endl;        // 结果是:9

// 2. 从下标为6开始找字符'i',返回找到的第一个i的下标
cout << s.find('i', 6) << endl;            // 结果是:11

11.string的排序

  • sort(iterator iter1, iterator iter2):对[iter1, iter2)进行排序

    ps:for(auto x:a) cout<<x<<" "这是基于范围的for循环,只有C++11版本以上才支持。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a("sdfergrggqeiufn");
    sort(a.begin(),a.end());
    for(auto x:a)
        cout<<x<<" ";
    cout<<endl;
    return 0;
}
//
d e e f f g g g i n q r r s u

Process returned 0 (0x0)   execution time : 0.020 s
Press any key to continue.

12.substr(start, length)方法:返回一个从指定位置开始,并具有指定长度的子字符串。

参数:

  • start:必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 0。
  • length:可选项。返回的子字符串中包含的字符数。
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s = "12345";
	string s1 = s.substr(3);
	string s2 = s.substr(2, 3);
	cout << s1 << ' ' << s2;
    return 0;
}
//
45 345
Process returned 0 (0x0)   execution time : 0.020 s
Press any key to continue.

猜你喜欢

转载自www.cnblogs.com/Yqifei/p/12660178.html