我想初始化地圖 - 對象「身份證」與身份從0到n-1,即什麼是創建STL身份映射的最簡單方法?
id[0] = 0
id[1] = 1
.
.
id[n-1] = n-1
有沒有一種簡單的方法 - 一個內膽,地圖對象中的方法,只是一件非常簡單的事 - 那是不是?
我想初始化地圖 - 對象「身份證」與身份從0到n-1,即什麼是創建STL身份映射的最簡單方法?
id[0] = 0
id[1] = 1
.
.
id[n-1] = n-1
有沒有一種簡單的方法 - 一個內膽,地圖對象中的方法,只是一件非常簡單的事 - 那是不是?
你可以使用構造的
template <class InputIterator>
map(InputIterator f, InputIterator l,
const key_compare& comp)
形式,但你需要建立一個擔任過你想要的範圍內發電機功能的InputIterator的。這比單單使用for循環要多得多。
有什麼不對
for(unsigned int i = 0; i < n; ++i)
id[i] = i;
這似乎有點怪異使用地圖的鍵是一個簡單的指標。你確定你不能使用矢量嗎?這樣做,你可以使用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循環了巨大的收益。
查找出的std ::絲毫的數字圖書館(的C++ 0x功能)的一部分:
template <ForwardIterator Iter, HasPreincrement T>
requires OutputIterator<Iter, const T&>
void iota(Iter first, Iter last, T value);
我期待的是STL提供有利於這個循環的方法。 – secr 2009-02-13 13:45:41