c++输出越来越多的数字

c++ output increasing numbers

本文关键字:数字 越来越多 输出 c++      更新时间:2023-10-16

嗨,我有一系列股价,但我只想在股价上涨时输出。

例如,如果我有1,1,1,2,2,3,3,4,4,5,5,5等,我只想打印1,2,3,4。

我试过设置一个临时的最大值和最小值,但仍然无法获得。现在我只有这个:

for(int h = 0; h < max; h++)
        {
            if(v3[h].getPrice() > 0)
            {
            ofile <<  v[h].getPrice() << ", ";
            }
        }

你想要的是这个

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    // Assign your vector
    int a[] = {1,1,1,2,2,3,3,3,4,4,5,5,5,1,3};
    vector<int> vec(a, a+15);
    // Sort before calling unique
    sort(vec.begin(), vec.end());
    // Impose only one of each
    vector<int>::iterator it;
    it = unique(vec.begin(), vec.end());
    vec.resize( distance(vec.begin(),it) );
    // Output your vector
    for( vector<int>::iterator i = vec.begin(); i!= vec.end(); ++i)
        cout << (*i) << endl;
    return 0;
}

实例

sortunique工作所必需的。

#include <iostream>
using namespace std;
int main()
{
    int a[15] = {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5};
    for (int i=0; i<15; i+=3)
    {
        cout << a[i] <<",";
    }
    return 0;
}

在循环"for(int h=0;h<max;h+=3){}"中将计数器递增3次。