What is the return type of the STL "count" algorithm, on valarray

I am using Visual Studio 2010 Pro on a Windows 7 64bit machine, and I want to use count (from the <algorithm> header) in valarray :

 int main() { valarray<bool> v(false,10); for (int i(0);i<10;i+=3) v[i]=true; cout << count(&v[0],&v[10],true) << endl; // how to define the return type of count properly? // some_type Num=count(&v[0],&v[10],true); } 

The result of the program above is correct:

 4 

However, I want to assign a value to a variable and use int leads to compiler warnings about loss of precision. Since valarray has no iterators, I cannot figure out how to use iterartor::difference_type .

How is this possible?

+5
source share
1 answer

The correct type for Num :

 typename iterator_traits<bool*>::difference_type Num=count(&v[0],&v[10],true); 

The reason for this is that count always returns:

 typename iterator_traits<InputIt>::difference_type 

and your InputIt is a pointer to bool:

 &v[0]; // is of type bool* &v[10]; // is of type bool* 

For me, iterator_traits<bool*>::difference_type is evaluated to long , so you might as well just be using:

 long Num=count(&v[0],&v[10],true); 

However, I must admit that I did not test it under Visual Studio 2010 Pro explicitly.

+2
source

Source: https://habr.com/ru/post/1235465/


All Articles