back_inserter和back_insert_iterator

back_inserter是back_insert_iterator的一个成员函数, 本质上是push_back进行操作的, 返回值back_insert_iterator, 并实现其自增.
	std::vector firstvector, secondvector;
	for (int i=1; i<=5; i++)
	{ 
		firstvector.push_back(i); 
		secondvector.push_back(i*10);
	}

	// 在firstvector向量后面加入secondvector的值.
	std::copy(secondvector.begin(), secondvector.end(), std::back_inserter(firstvector));

	std::vector::iterator it;
	for ( it = firstvector.begin(); it!= firstvector.end(); ++it )
		std::cout << *it << " ";
	std::cout << std::endl;
下面是一个back_insert_iterator的例子
	std::vector vec;
	for (int i = 0 ; i < 3; ++i)
		vec.push_back(i);

	std::vector ::iterator vIter;
	std::cout << "The initial vector vec is: ( ";
	for (vIter = vec.begin(); vIter != vec.end(); vIter++)
		std::cout << *vIter << " ";
	std::cout << ")." << std::endl;

	// Insertions can be done with template function
	std::back_insert_iterator > backiter(vec);
	*backiter = 30;
	backiter++;
	*backiter = 40;

	// Alternatively, insertions can be done with the
	// back_insert_iterator member function
	std::back_inserter(vec) = 500;
	std::back_inserter(vec) = 600;

	std::cout << "After the insertions, the vector vec is: ( ";
	for (vIter = vec.begin(); vIter != vec.end(); vIter++)
		std::cout << *vIter << " ";
	std::cout << ")." << std::endl;

你可能感兴趣的:(c++模板)