Qt ------ 自定义QVector中的T

时间:2022-09-06 20:05:53
#ifndef FREQUENCYSPECTRUM_H
#define FREQUENCYSPECTRUM_H #include <QtCore/QVector> /**
* Represents a frequency spectrum as a series of elements, each of which
* consists of a frequency, an amplitude and a phase.
*/
class FrequencySpectrum {
public:
FrequencySpectrum(int numPoints = ); struct Element
{
Element()
: frequency(0.0), amplitude(0.0), phase(0.0), clipped(false)
{ } /**
* Frequency in Hertz
*/
qreal frequency; /**
* Amplitude in range [0.0, 1.0]
*/
qreal amplitude; /**
* Phase in range [0.0, 2*PI]
*/
qreal phase; /**
* Indicates whether value has been clipped during spectrum analysis
*/
bool clipped;
}; typedef QVector<Element>::iterator iterator;
typedef QVector<Element>::const_iterator const_iterator; void reset(); int count() const;
Element& operator[](int index);
const Element& operator[](int index) const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const; private:
QVector<Element> m_elements; }; #endif // FREQUENCYSPECTRUM_H
#include "frequencyspectrum.h"

FrequencySpectrum::FrequencySpectrum(int numPoints)
: m_elements(numPoints)//等于m_elements.resize(numPoints);
{ } void FrequencySpectrum::reset()
{
iterator i = begin();
for ( ; i != end(); ++i)
*i = Element();
} int FrequencySpectrum::count() const
{
return m_elements.count();
} FrequencySpectrum::Element &FrequencySpectrum::operator[](int index)
{
return m_elements[index];
} const FrequencySpectrum::Element &FrequencySpectrum::operator[](int index) const
{
return m_elements[index];
} FrequencySpectrum::iterator FrequencySpectrum::begin()
{
return m_elements.begin();
} FrequencySpectrum::iterator FrequencySpectrum::end()
{
return m_elements.end();
} FrequencySpectrum::const_iterator FrequencySpectrum::begin() const
{
return m_elements.begin();
} FrequencySpectrum::const_iterator FrequencySpectrum::end() const
{
return m_elements.end();
}