在 valarray 上 "count" 的 STL 算法的返回类型是什么
What is the return type of the STL algorithm "count", on a valarray
我在Windows 7 64bit
机器上使用Visual Studio 2010 Pro
,我想在valarray
:上使用count
(来自<algorithm>
标头)
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);
}
上面程序的输出是正确的:
4
但是,我想将值分配给一个变量,并且使用int
会导致编译器发出关于精度损失的警告。由于valarray
没有迭代器,我不知道如何使用iterartor::difference_type
。
这有可能吗?
Num
的正确类型为:
typename iterator_traits<bool*>::difference_type
Num=count(&v[0],&v[10],true);
原因是count
总是返回:
typename iterator_traits<InputIt>::difference_type
你的InputIt
是指向bool:的指针
&v[0]; // is of type bool*
&v[10]; // is of type bool*
对我来说,iterator_traits<bool*>::difference_type
计算为long
,所以你也可以简单地使用:
long Num=count(&v[0],&v[10],true);
然而,我不得不承认,我没有明确地在Visual Studio 2010 Pro
下测试它。
相关文章:
- 如何获取std::result_of函数的返回类型
- 奇怪的结构&GCC&clang(void*返回类型)
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 函数作为模板参数,是否对返回类型强制约束
- C++中函数的向量返回类型引发错误
- 检查函数返回类型是否与STL容器类型值相同
- 为什么返回类型中需要typename?C++
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 推导 std::vector::back() 的返回类型
- 在 c++ 中将函数返回类型指定为模板参数
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 对于具有引用返回类型的搜索算法,默认返回值应该是什么?
- 在 valarray 上 "count" 的 STL 算法的返回类型是什么