求解复数组 中模较大的N个数

//求解复数组 中模较大的N个数
void fianN_Complex(Complex outVec[], int& len, std::vector<int>& index,int N)   // Complex (&outVec)[512]  数组引用  单纯数组做参数退化为指针
{
    std::vector<double> modulus;
    for (size_t i = 0; i < len; i++)
    {
        double temp;
        temp=sqrt(outVec[i].im*outVec[i].im + outVec[i].rl*outVec[i].rl);
        modulus.push_back(temp);
    }

    if (index.size()<N)
    {
        if (index.size()!=0)
        {
            for (size_t i = 0; i < index.size(); i++)
            {
                modulus[index[i]] = 0;
            }    
        }

        int indextemp = 0;
        for (size_t j = 0; j < modulus.size() - 1; j++)
        {
            if (modulus[j]>modulus[j + 1])
            {
                double temp = modulus[j];
                modulus[j] = modulus[j + 1];
                modulus[j + 1] = temp;
            }
            else
            {
                indextemp = j + 1;
            }
        }
        index.push_back(indextemp);
    }

}

猜你喜欢

转载自www.cnblogs.com/lovebay/p/9842262.html