举一个函数对象在stl中应用的例子
时间: 2023-10-03 10:01:50 浏览: 55
在STL中,函数对象被广泛应用于各种算法和容器。举一个例子,我们可以使用函数对象`std::less`来比较两个值的大小关系。这个函数对象可以作为参数传递给STL中的许多算法和容器,比如`std::sort`、`std::set`等等。
例如,下面的代码使用`std::less`和`std::sort`函数来对一个整数数组进行排序:
```c++
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> vec {5, 2, 9, 1, 3};
std::sort(vec.begin(), vec.end(), std::less<int>());
for (auto i : vec)
std::cout << i << " ";
return 0;
}
```
在这个例子中,我们使用了`std::less<int>()`函数对象来比较整数的大小。这个函数对象可以被`std::sort`算法所使用,以决定数组中元素的顺序。最终的输出结果是一个有序的整数数组:1 2 3 5 9。
相关问题
千泽 实验十 STL应用
好的,实验十的内容是关于STL(Standard Template Library)的应用。
STL是C++标准库中的一部分,提供了许多通用的数据结构和算法,包括容器(如vector、list、map等)、迭代器、算法(如排序、查找等)和函数对象等。STL的使用可以大大提高程序的效率和可读性。
下面是一些STL的应用实例。
1. 使用vector容器存储数据
vector是一个动态数组,可以自动调整大小以适应存储数据的需求。以下是一个例子,演示如何使用vector存储一些整数。
```c++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
// 添加数据
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
// 输出数据
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
return 0;
}
```
输出结果为:
```
1 2 3
```
2. 使用list容器存储数据
list是一个双向链表,可以在任意位置进行插入和删除操作。以下是一个例子,演示如何使用list存储一些字符串。
```c++
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<string> lst;
// 添加数据
lst.push_back("hello");
lst.push_back("world");
lst.push_front("hello");
// 输出数据
for (list<string>::iterator it = lst.begin(); it != lst.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
```
输出结果为:
```
hello hello world
```
3. 使用map容器存储数据
map是一个关联数组,可以根据键值进行数据的存储和查找。以下是一个例子,演示如何使用map存储一些学生的信息。
```c++
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> m;
// 添加数据
m["Tom"] = 90;
m["Jerry"] = 80;
m["Bob"] = 70;
// 查找数据
cout << "Tom's score is " << m["Tom"] << endl;
return 0;
}
```
输出结果为:
```
Tom's score is 90
```
以上是STL的一些应用实例,希望对你有帮助。
好的,可以再举一个不同的例子来说明lambda表达式的作用吗?
当然,这里有一个例子来说明 lambda 表达式的另一个应用场景:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {5, 2, 8, 3, 1};
// 使用 lambda 表达式作为排序准则,按照从大到小的顺序排序
std::sort(numbers.begin(), numbers.end(), [](int a, int b) {
return a > b;
});
// 打印排序后的结果
for (int num : numbers) {
std::cout << num << " ";
}
return 0;
}
```
在这个例子中,我们使用 lambda 表达式作为 `std::sort` 函数的第三个参数,来指定排序准则。lambda 表达式接受两个 `int` 类型的参数 `a` 和 `b`,并根据我们定义的比较规则返回 `true` 或 `false`。
在这个例子中,我们定义了一个从大到小的排序规则,因此 `std::sort` 函数会按照从大到小的顺序对 `numbers` 向量进行排序。
运行这段代码会输出:
```
8 5 3 2 1
```
这个例子展示了 lambda 表达式在 STL 算法中的使用,可以根据需要灵活定义函数对象,而无需显式编写一个命名函数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)