C++搜索MAX的 vector ,并从第二个 vector 中获得相同的位置

标签 c++ vector

我正在使用 C++,我有 2 个相互关联的 vector :

vector<double> val = {.3,.5,.2,.4};
vector<string> str = {'a','b','c','d'};

我想在 val 中搜索最大值,然后在相同位置从 str 返回字符串:

vector<double>::const_iterator it;
it = max_element(val.begin(), val.end());

那么,我如何在 str 中使用 it 来获取字母呢?

string lettter;
letter = str.at(it-> ????? );

谢谢!!!

最佳答案

怎么样

letter = str.at(it - val.begin());

?

(Rationale)

关于C++搜索MAX的 vector ,并从第二个 vector 中获得相同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14715932/

相关文章:

c++ - 如何在运行时构造 multi_array::index_gen

c++ - 使用结构中定义的 GSL 函数

function - Mapcat 是如何工作的?

c++ - 用于密集数据集 4D vector 快速范围搜索的数据结构

c++ - Visual Studio 中的多个主要 CPP 文件?

c++ - 为什么我不能使用 MinGW 编译 GTK+?

C++ getenv 不更新

c++ - 将多维数组 vector 中单个项目的指针传递给函数c++

c++ - 如果我使用 std :find, 在 vector 对中找到一个元素,我如何才能将 vector 中的值转换为字符串?

c++ - 在 C++ 中使用 vector 给了我一个我无法理解的错误