c++ - 从对象向量返回一个对象作为参考
问题描述
#include <iostream>
#include <vector>
#include <random>
#include <ranges>
#include <algorithm>
#include <chrono>
#include <functional>
namespace ranges = std::ranges;
struct Model
{
double next_event_time;
};
double timeit(int repeats, int items, std::function<void(int)> func)
{
auto begin = std::chrono::steady_clock::now();
for (auto i = 0; i < repeats; i++)
func(items);
auto end = std::chrono::steady_clock::now();
auto dur = (end - begin);
auto total_time = std::chrono::duration_cast<std::chrono::microseconds>(dur);
return total_time.count() / repeats;
}
std::vector<Model> generate_examples(int number)
{
std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(0.0, 1.0);
std::vector<Model> models;
for (auto i = 0; i < number; i++)
{
models.push_back(Model{.next_event_time = distribution(generator)});
}
return models;
}
Model& get_next_model(std::vector<Model> &models)
{
ranges::sort(models, ranges::less{}, [](const Model &x) { return x.next_event_time; });
return models[0];
}
Model& get_next_model2(const std::vector<Model> &models)
{
// Error here
return ranges::min(models, ranges::less{}, [](const Model &x) { return x.next_event_time; });
}
void timeOne(int items)
{
std::vector<Model> models = generate_examples(items);
get_next_model(models);
}
void timeTwo(int items)
{
auto models = generate_examples(items);
get_next_model2(models);
}
int main()
{
const std::string MS_UNIT = "[ms]";
int items = 1000;
int repeats = 10000;
//std::cout << timeit(repeats, items, timeOne) << MS_UNIT << std::endl;
std::cout << timeit(repeats, items, timeTwo) << MS_UNIT << std::endl;
return 0;
}
如果我编译上面的代码,我得到
无法将“Model&”类型的非常量左值引用绑定到“std::ranges::range_value_t<const std::vector&>”类型的右值
在get_next_model2
为什么我会收到这个错误?
我在用
g++10.2
- 运行使用
g++ -std=c++20 file.cpp
我需要
- 从s 的
next_model
向量中查找Model
- 将其设置
next_model.next_event_time
为0
在我作为副本返回之前Model get_next_model(std::vector<Model> &models)
。但是使用它我正在修改.next_event
副本的时间。
解决方案
的重载std::ranges::min
接受按值返回的任意范围。因为它返回一个临时值,所以不能将非常量左值引用绑定到它的返回值。即使可以,该引用也会在get_next_model2
返回后立即悬空。
改为使用std::ranges::min_element
。 min_element
将迭代器返回到范围内的最小值。请注意,如果您想从中返回非常量引用,get_next_model2
则需要接受对您的非常量引用std::vector
,因为const
向量的元素都是它们本身const
。
Model& get_next_model2(std::vector<Model> &models)
{
return *ranges::min_element(
models,
ranges::less{},
[](const Model &x) { return x.next_event_time; }
);
}
推荐阅读
- node.js - 如何从数据库中检索数据并使用 exrpessjs 相应地呈现它
- php - Soap 请求在 SoapUI 中有效,但在 Wordpress 插件中无效
- c# - 如何使用 TextBox.GetPositionFromCharIndex 仅在需要时显示垂直滚动条?
- google-cloud-dataflow - 使用Beam读取记录时重命名列?
- python - tkinter:如何在画布项目上更改光标?
- python - python read_excel 绝对路径
- javascript - 在外部单击纯 JavaScript 上关闭菜单
- opencv - 使用 Makefile 编译时对 opencv 函数的未定义引用
- go - Golang 包未定义
- r - 替换栅格堆栈中所有图层的像素值