5

私はマシンで使用Visual Studio 2010 Proしており、(ヘッダーから)でWindows 7 64bit使用したい:count<algorithm>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); 
}

上記のプログラムの出力は正しいです。

4

ただし、変数に値を代入したいのですが、使用するとint、精度が失われるというコンパイラの警告が表示されます。valarrayにはイテレータがないため、 の使用方法がわかりませんiterartor::difference_type

これはどういうわけか可能ですか?

4

1 に答える 1

3

の正しいタイプは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ただし、明示的にテストしていないことを認めなければなりません。

于 2015-11-08T12:29:26.720 に答える