2

0 から n-1 までの ID を持つオブジェクト "id" のマップを初期化したいと思います。

 id[0] = 0
 id[1] = 1
 .
 .
 id[n-1] = n-1

簡単な方法はありますか - ワンライナー、マップオブジェクト内のメソッド、単に本当に単純なもの - それはそれを行いますか?

4

4 に答える 4

5

何が問題になっていますか

for(unsigned int i = 0; i < n; ++i)
    id[i] = i;
于 2009-02-13T12:41:18.190 に答える
3

キーが単純なインデックスであるマップを使用するのは少し奇妙に思えます。ベクトルを使用できないと確信していますか? そうすることでboost::counting_iterator、ベクトルを埋めるために使用できます。

std::vector<int> v(boost::counting_iterator<int>(0), 
                   boost::counting_iterator<int>(N-1));
// v is filled with integers from 0 to N-1

ただし、単純な for ループと比較して、これが大きな利点であるかどうかはわかりません。

于 2009-02-13T14:28:23.877 に答える
1

数値ライブラリの一部としてstd::iotaを探してください(C ++ 0x機能):

template <ForwardIterator Iter, HasPreincrement T>
requires OutputIterator<Iter, const T&>
void iota(Iter first, Iter last, T value);
于 2009-02-16T19:08:09.470 に答える