首页 > 解决方案 > 如何使用Qt计算表中值的频率

问题描述

通常在我的表上我有这些值 (0,0,80,0,0,0,80,80,80,40) 这个表被命名为 qvValues ,我想在频率表上 (5,4,1)

#include<QDebug>
#include <QList>
QList<double> frequencies;
QList<double> qvValues;
for(int i=0;i<qvValues.size();i++)
{

            frequencies.append(qvValues.count( qvValues[i]));


}
qDebug() << frequencies;

标签: c++qt

解决方案


这些值来自您的示例(评论)。

#include <QMap>
#include <QList>
#include <QDebug>

int main(int argc, char *argv[])
{
    QList<int> values;
    QMap<int, int> frequencies;

    values << 0 << 0 << 80 << 0 << 0 << 0 << 80 << 80 << 80 << 40;

    int value;

    foreach ( value, values )
    {
        if ( !frequencies.contains( value ) )
        {
            frequencies.insert( value, values.count( value ) );
            qDebug() << "Value: " << value << " Frequency: " << values.count( value );
        }
    }

    return 0;
}

推荐阅读