STL之string常用库函数笔记

时间:2020-04-08 18:03:54   收藏:0   阅读:72

STL之string常用库函数笔记

1.string的构造函数形式

2.string的大小和容量

#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的字符串比较

#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的插入

    // 尾插一个字符
    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的拼接

//方法一: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的遍历:借助迭代器 或者 下标法

#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的删除

#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的字符替换

#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 算法

#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

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的排序

#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)方法:返回一个从指定位置开始,并具有指定长度的子字符串。

参数:

#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.

原文:https://www.cnblogs.com/Yqifei/p/12660178.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!