【发布时间】:2021-02-28 07:46:31
【问题描述】:
#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
类型'
get_next_model2
为什么会出现这个错误?
我正在使用
g++10.2- 使用
g++ -std=c++20 file.cpp运行
我需要
- 从
Models的向量中找到next_model - 将
next_model.next_event_time设置为0
在我作为副本返回之前Model get_next_model(std::vector<Model> &models)。
但是使用它我正在修改副本的.next_eventtime`。
【问题讨论】:
-
@PatrickRoberts 很抱歉,谢谢您可以将此评论作为对the question i deletedplease 的答复吗?
-
这只是为了您的利益,您不需要取消删除问题。反正我真的没准备好写详细的解释。