/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << ' ';
}
std::cout << std::endl;
}
int main()
{
vector<int> coll;
// create back inserter for coll
// - inconvenient way
back_insert_iterator<vector<int> > iter(coll);
// insert elements with the usual iterator interface
*iter = 1;
iter++;
*iter = 2;
iter++;
*iter = 3;
PRINT_ELEMENTS(coll);
//// create back inserter and insert elements
//// - convenient way
//back_inserter(coll) = 44;
//back_inserter(coll) = 55;
//PRINT_ELEMENTS(coll);
//// use back inserter to append all elements again
//// - reserve enough memory to avoid reallocation
coll.reserve(2*coll.size());
std::copy (coll.begin(), coll.end(), // source
back_inserter(coll)); // destination
PRINT_ELEMENTS(coll);
}
//在 The C++ Standard Library 上摘下来的一段代码。运行在2008下的时候会有问题。
追查了一下 源代码
在 xutility 下面的一段代码。
复制一个元素之后。_First就 == _Last 了,于是报错。
这个还是内部机制有问题。。。