I would like to add the peak position of a vector and the peak number but I can't find a way to add the elements and then return it, or write it on console.
#include <iostream>
#include <vector>
using namespace std;
struct PeakData {
vector<int> pos, peaks;
};
PeakData pick_peaks(vector<int> v) {
PeakData result;
for (int i = 1; i < v.size() - 1; i++) {
if ((v[i] > v[i - 1]) && (v[i] > v[i + 1])) {
result.peaks.push_back(v[i]);
result.pos.push_back(i);
}
}
return result;
}
Example: pickPeaks([3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3]) should return {pos: [3, 7], peaks: [6, 3]}
Add this to your main function:
int main()
{
vector<int> a = {3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3};
PeakData stPickPeaks = pick_peaks(a);
vector<int> :: iterator itr;
for(itr = stPickPeaks.pos.begin(); itr<stPickPeaks.pos.end(); itr++)
{
cout <<*itr<<endl;
}
for(itr = stPickPeaks.peaks.begin(); itr<stPickPeaks.peaks.end(); itr++)
{
cout <<*itr<<endl;
}
return 0;
}
Also try to pass the parameters either by reference or pointer.