C++ STL学习1

主要内容string vector list

动机

刷leetcode的时候感觉这一块太重要了,还是补一补吧。

注意可以抄这篇:https://blog.csdn.net/q5120192609/article/details/127587508

string

构造

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

// string(); 创建一个空的字符串例如: string str;
// string(const char* s); 使用字符串s初始化
// string(const string& str); 使用一个string对象初始化另一个string对象
// string(int n, char c); 使用n个字符c初始化

void test01()
{
const char* str = "hello world";
string s1; //默认构造
string s2(str);
string s3(s2);
string s4(10, 'a'); // 10个a
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
}

赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

//string& operator=(const char* s); char* 类型字符串赋值给当前的字符串
//string& operator=(const string& s); 把字符串s赋给当前的字符串
//string& operator=(char c); 字符赋值给当前的字符串
//string& assign(const char* s); 把字符串s赋给当前的字符串
//string& assign(const char* s, int n); 把字符串s的前n个字符赋给当前的字符串
//string& assign( const string &s ); 把字符串s赋给当前字符串
//string& assign(int n, char c); 用n个字符c赋给当前字符串

void test01()
{
string s1;
s1 = "hello world";
cout << s1 << endl;

string s2 = s1;
cout << s2 << endl;

string s3;
s3 = 'c';
cout << s3 << endl;

string s4;
s4.assign("hello C++");
cout << s4 << endl;

string s5;
s5.assign("hello C++", 5);
cout << s5 << endl; // s5: hello

string s6;
s6.assign(s5);
cout << s6 << endl;

string s7;
s7.assign(5, 'a'); //s7: aaaaa
}

拼接

主要是 +=append

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
/*
string& operator+=(const char* str); //重载+=操作符
string& operator+=( const char c); //重载+=操作符
string& operator+=( const string& str); //重载+=操作符
string& append( const char *s); //把字符串s连接到当前字符串结尾
string& append(const char*s, int n); //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s); //同operator+=(const string& str)
string&append(const string &s, int pos,int n);//字符串s中从pos开始的n个字符连接到字符串结
*/
void test01()
{
string str1 = "我";
str1 += "不爱玩游戏,爱学习";
cout << str1 << endl;

string str3 = "I";
str3.append("love");
cout << str3 << endl;

string str4 = "learning aaa";
str3.append(str4, 0, 8);
cout << str3 << endl;
}

查找与替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

//查找
void test01()
{
string str1 = "abcdefgde";
int pos1 = str1.find("de");
int pos2 = str1.find("df");
cout << pos1 << endl;
cout << pos2 << endl; // -1
//rfind 从右往左查,但是从左往右计算位置
int pos3 = str1.rfind("de");
cout << pos3 << endl;
}

//替换
void test02()
{
string str1 = "Abcdefg";
str1.replace(1, 3, "1111"); //将第1位的后面3位替换为“1111”
cout << str1 << endl; // A1111efg
}

比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void test01()
{
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2) == 0)
{
cout << "str1 = str2" << endl;
}
else if(str1.compare(str2) > 0) // 1
{
cout << "str1 > str2" << endl;
}
else // -1
{
cout << "str1 < str2" << endl;
}
}

vector

vector即数组

容器:vector 算法:for_each 迭代器:vector<int>::iterator

sort(L1.begin(), L1.end())

list

反转: L1.reverse()

排序: L1.sort()

所有不支持随机访问迭代器的容器不可以使用标准算法;不支持随机访问迭代器的容器内部会提供成员函数

另外,可以通过回调函数设置排序依据,下面是一个设置降序的例子:

1
2
3
4
5
6
7
8
9
L1.sort();  // 排序,默认升序

// 用于设置降序排序的回调函数
bool myCompare(int v1, int v2)
{
// 降序: 第一个数大于第二个数
return v1 > v2;
}
L1.sort(myCompare) // 排序,通过`myCompare`函数设置为降序

下面是一个设置自定义类的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

class Person{
public:
Person(string name,int age,int height){
m_name = name;
m_age = age;
m_height = height;
}

string m_name;
int m_age;
int m_height;
};

bool compare(Person &p1,Person &p2)
{
//年龄相同的按身高降序排列
if (p1.m_age == p2.m_age)
{
return p1.m_height>p2.m_height;
}
else //按年龄升序排列
{
return p1.m_age<p2.m_age;
}
}

int main()
{
Person p1("刘备",35,175);
Person p2("曹操",45,188);
Person p3("孙权",48,170);
Person p4("赵云",25,198);
Person p5("张飞",35,160);
Person p6("关羽",35,200);

list<Person> myL;
myL.push_back(p1);
myL.push_back(p2);
myL.push_back(p3);
myL.push_back(p4);
myL.push_back(p5);
myL.push_back(p6);

for(list<Person>::iterator it = myL.begin();it != myL.end();it++){
cout<<"姓名: "<<it->m_name<<" 年龄 :"<<it->m_age<<" 身高 :"<<it->m_height<<endl;
}

cout << "-----------------" << endl;

//按年纪大小排序
myL.sort(compare);//使用list里面的成员函数,使用仿函数指定排序方法
for(list<Person>::iterator it = myL.begin();it != myL.end();it++){
cout<<"姓名: "<<it->m_name<<" 年龄 :"<<it->m_age<<" 身高 :"<<it->m_height<<endl;
}
return 0;
}