[英]c++ output increasing numbers
嗨,我有一系列股價,但我只想在股價上漲時輸出它們。
例如,如果我有 1、1、1、2、2、2、3、3、3、4、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;
}
sort
是unique
工作所必需的。
#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 次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.