c++输出越来越多的数字
c++ output increasing numbers
嗨,我有一系列股价,但我只想在股价上涨时输出。
例如,如果我有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;
}
实例
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次。
相关文章:
- 如何通过使用 2 位或更多数字的 XOR 运算符来执行此操作C++问题
- 将最多 50 位数字的 2 个数字相乘
- 如何在c++中将字符串中的所有数字保存到多维数组中
- 鸽子洞/多个数字
- 如何执行此程序,将键入的数字显示为数字值的多次
- 将一个数字拆分为多个数字,每个数字只有一个有效位
- 在 std::unordered_set <double>中插入多个非数字
- 如何在 openCL 的内核调用中指定数字多维
- C++ 如何找出数组中最多的连续数字?
- 如何将字符串中的多个数字转换为整数
- 如何避免向类中添加越来越多的状态?
- 生成最多给定数字 N 的步进数字
- 为什么这段代码输出这么多数字
- 如何在 c++ 中使用 cin.get() 给出更多数字?
- 迭代器越来越多,并导致SEG故障
- 使用单个数字表示多个变量
- 为什么用“std::chrono”进行时差测量时,与“double”类型相比,用“float”类型给出的有效数字更多
- 在越来越多的坐标范围内运行prim的最快方法
- VTK使用越来越多的内存,变得很慢
- c++输出越来越多的数字