简体   繁体   English

基于最大值索引 C++ 从 2 个向量中提取值

[英]Extracting values from 2 vectors based on the max value index C++

I'm a newbie programmer trying to get used to work with vectors.我是一个新手程序员,试图习惯使用向量。 In the following code I was able to find the max value of vector "V" and return it to main.在下面的代码中,我能够找到向量“V”的最大值并将其返回到 main。 Instead of that I need to return the value from the other vector corresponding to the max value index.而不是我需要从对应于最大值索引的另一个向量返回值。 In this case for vector "V" max value is 65.25, I want the function to return 0.05 from vector "freq" (same index).在这种情况下,向量“V”最大值为 65.25,我希望函数从向量“freq”(相同索引)返回 0.05。 These values come from previous calculations using matrixes, adding the results to the vector with the push_back method, I just need to extract the 0.05 for further operations.这些值来自之前使用矩阵的计算,使用 push_back 方法将结果添加到向量中,我只需要提取 0.05 以进行进一步操作。 Help is very well appreciated.非常感谢帮助。

#include <iostream>
#include <vector>
#include <cmath>
#include <cfloat>

using namespace std;
double maxAt(vector<double> &Lvec); // MaxL value func prototype


int main() {

    vector <double> freq = {0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07};
    vector <double> V ={0, 0, 0, 0, 65.25, 0,6};
    double MaxV = maxAt(V);
    cout << MaxV << endl;
    return 0;
}



double maxAt(vector<double> &V) {
    double Lmax = DBL_MIN;
    for (auto val : V) {
         if (Lmax < val) Lmax = val;
    } 
    return Lmax;
}

There is no need to invent your own function that searches the maximum.没有必要发明自己的搜索最大值的函数。 You can use standard features.您可以使用标准功能。

Here you are.这个给你。

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main() 
{
    std::vector<double> freq = { 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07 };
    std::vector<double> V = { 0, 0, 0, 0, 65.25, 0,6 };

    auto it = std::max_element( std::begin( V ), std::end( V ) );

    std::cout << *it << " -> " 
              << *std::next( std::begin( freq ), std::distance( std::begin( V  ), it ) )
              << '\n';

    return 0;
}

The program output is程序输出是

65.25 -> 0.05

If to use your function then you should change it the following way as it is shown in the demonstrative program below.如果要使用您的功能,那么您应该按照以下方式更改它,如下面的演示程序所示。

#include <iostream>
#include <vector>

auto maxAt( const std::vector<double> &V ) 
{
    std::vector<double>::size_type max = 0;

    for ( std::vector<double>::size_type i = 1; i < v.size(); i++  ) 
    {
         if ( V[max] < V[i] ) max = i;
    } 

    return max;
}

int main() 
{
    std::vector<double> freq = { 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07 };
    std::vector<double> V = { 0, 0, 0, 0, 65.25, 0,6 };

    auto pos = maxAt( V );

    std::cout << V[pos] << " -> " 
              << *freq[pos]
              << '\n';

    return 0;
}

The program output is the same as shown above程序输出与上图相同

65.25 -> 0.05

You could do:你可以这样做:

double maxAt(vector<double> &V, vector<double> &freq) {
    double Lmax = DBL_MIN;
    double Lfreq = DBL_MIN;
    for (size_t i = 0; i < V.size(); ++i) {
         if (Lmax < V[i]) {
             Lmax = V[i];
             Lfreq = freq[i];
         }
    } 
    return Lfreq;
}

Also, see here for an answer that uses standard algorithms: Finding the position of the max element另外,请参阅此处以获取使用标准算法的答案: 查找最大元素的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM