現在、STL (C++) を学習しています。
次のコードでは、整数要素 [0;110) を持つベクトルがあり、25 で割り切れるベクトル要素の数を数えたいと考えています。
プログラムを実行すると、次の出力が表示されます:
1
2
3
4
5
Counter: 0
なぜ0?
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
class dividesby
{
int counter;
public:
int getCounter(){return counter;}
dividesby():counter(0){}
void operator ()(int i)
{
if(i%25==0)
{
counter++;
std::cout<<"\n"<<counter<<"\n";
}
}
};
void main()
{
using namespace std;
vector <int> v;
for(int i=0;i<110;i++)
{
v.push_back(i);
}
dividesby D;
for_each(v.begin(),v.end(),D);
cout<<"Counter: "<<D.getCounter()<<"\n";
}