gpt4 book ai didi

c++ - 如何保存 min_element 结果的位置?

转载 作者:太空狗 更新时间:2023-10-29 20:00:02 26 4
gpt4 key购买 nike

如何保存 min_element 的值?它说它是一个前向迭代器,但我似乎无法弄清楚如何保存(分配给一个变量)它。我希望能够通过 vector 中的位置访问它。我所能找到的只是使用实际元素的示例(使用 *min_element() )。我试过了

iterator< forward_iterator_tag, vector<string> > min_word_iterator = min_element(first_words_in_subvecs.begin(), first_words_in_subvecs.end());

但这没有用。我将用不同的元素替换该索引处的元素。

最佳答案

使用这个:

std::vector<T>::iterator minIt = std::min_element(v.begin(),v.end());
//where T is the type of elements in vector v.

T minElement = *minIt; //or T & minElement = *minIt; to avoid copy!

在 C++11 中(如果你的编译器支持 auto 关键字),那么:

auto minIt = std::min_element(v.begin(), v.end());
//type of minIt will be inferred by the compiler itself

T minElement = *minIt; //or auto minElement = *minIt;
//or auto & minElement = *minIt; to avoid copy

关于c++ - 如何保存 min_element 结果的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9256173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com