其他未完成

This commit is contained in:
2023-08-14 17:20:39 +08:00
parent 9290e4c051
commit 4c986179b4
65 changed files with 2650 additions and 11 deletions
+32
View File
@@ -0,0 +1,32 @@
#include <iostream>
#include <set>
using namespace std;
template <typename T>
void print(const set<T> &s)
{
for (typename set<T>::const_iterator it = s.begin(); it != s.end(); ++it)
cout << *it << ' ';
cout << endl;
}
template <typename T>
void print(const multiset<T> &s)
{
for (typename multiset<T>::const_iterator it = s.begin(); it != s.end(); ++it)
cout << *it << ' ';
cout << endl;
}
int main()
{
int m[] = {1, 2, 3, 2, 3, 4};
set<int> s(m, m + 6);
print(s); // set 会自动去重,输出 1 2 3 4
multiset<int> ms(m, m + 6);
print(ms); // multiset 不会自动去重,输出 1 2 2 3 3 4
return 0;
}
+50
View File
@@ -0,0 +1,50 @@
#include <iostream>
#include <set>
using namespace std;
template <typename T>
void print(const set<T> &s)
{
for (typename set<T>::const_iterator it = s.begin(); it != s.end(); ++it)
cout << *it << ' ';
cout << endl;
}
int main()
{
int m[] = {5, 12, 16, 21, 22, 27, 29, 30};
set<int> s(m, m + 8);
print(s); // set 键值自动排序,且不重复
// 查看大于等于 22 值的所有元素
cout << "查看大于等于 22 值的所有元素: " << endl;
set<int>::iterator it = s.lower_bound(22); // 返回第一个大于等于 22 的元素的迭代器
while (it != s.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
// 查找小于等于 22 值的所有元素
cout << "查找小于等于 22 值的所有元素: " << endl;
// it = s.upper_bound(22); // 返回第一个大于 22 的元素的迭代器
// for (; it != s.begin();)
// {
// --it;
// cout << *it << ' ';
// }
// cout << endl;
// 查找小于等于 22 值的所有元素(另一种写法)
it = s.lower_bound(22);
while (it != s.begin())
{
--it;
cout << *it << ' ';
}
cout << endl;
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
// 自定义排序
#include <iostream>
#include <set>
using namespace std;
// 自定义规则
template <typename T>
class MySort
{
public:
bool operator()(const T &a, const T &b)
{
return a > b; // 从小到大排序
}
};
int main()
{
// 使用 set 时,添加自定义排序规则
set<int, MySort<int> > s1;
s1.insert(20);
s1.insert(30);
s1.insert(1);
s1.insert(8);
set<int, MySort<int> >::iterator it = s1.begin(); // <int, MySort<int>> 可以省略
while (it != s1.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
return 0;
}
+59
View File
@@ -0,0 +1,59 @@
// 自定义排序
#include <iostream>
#include <set>
using namespace std;
// 自定义规则
template <typename T>
class MySort
{
public:
bool operator()(const T &a, const T &b)
{
return a > b; // 从小到大排序
}
};
class Student
{
public:
string name;
int age;
float score;
public:
Student(const string &name, int age, float score)
{
this->name = name;
this->age = age;
this->score = score;
}
};
class MyStudentByAgeSort
{
public:
bool operator()(const Student &s1, const Student &s2)
{
return s1.age > s2.age;
}
};
class MyStudentByScoreSort
{
public:
bool operator()(const Student &s1, const Student &s2)
{
return s1.score > s2.score;
}
};
int main()
{
Student[] *stus = new Student[3]{Student("张三", 18, 100), Student("李四", 20, 90), Student("王五", 19, 95)};
set<Student, MyStudentByAgeSort> s2;
s2............................
return 0;
}
+12
View File
@@ -0,0 +1,12 @@
#include <iostream>
#include <set>
using namespace std;
int main()
{
// pair 是一个模板类,有两个模板参数,
// 用于存储两个数据,可以分别指定两个数据的类型
pair<int, string> p1(1, "张三");
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
// 对组(pair)
// 将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可
// 以分别用 pair 的两个公有属性 first 和 second 访问。
// 类模板:template<class T1, class T2> class pair
// 用法一:
// pair<string, int> pair1(string("name"), 20);
// cout << pair1.first << endl;
// cout << pair1.second << endl;
// 用法二:
// pair<string, int> pair2 = make_pair("name", 30);
// cout << pair2.first << endl;
// cout << pair2.second << endl;
// 用法三:
// pair<string, int> pair3 = pair2; // 拷贝构造函数
// cout << pair3.first << endl;
// cout << pair3.second << endl;
// 如:
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
pair<int, string> p1(1, "disen");
pair<int, string> p2 = make_pair(2, "lucy");
cout << "id = " << p1.first << ", name = " << p1.second << endl;
cout << "id = " << p2.first << ", name = " << p2.second << endl;
return 0;
}