[ create a new paste ] login | about

Link: http://codepad.org/dZieXiBr    [ raw code | output | fork ]

C++, pasted on Dec 22:
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <locale>


// Добавить после каждого четного элемента элемент со значением 0
template <typename R, typename Pred1, typename T>
void insert_if(R& r, Pred1 pred1, T v)
{
	typename R::iterator it = r.begin();
	for ( ; it != r.end(); ++it)
	{
		if (pred1(*it))
			++it, it = r.insert(it, v);
	}
}

template <typename O, typename V>
O& operator << (O& os, const V& v)
{
	typedef std::ostream_iterator<typename V::value_type> OI;
	std::copy(v.begin(), v.end(), OI(os, " "));
	return os;
}

int main()
{
	setlocale(LC_ALL, "");

	typedef std::vector<int> V;
	V v;

	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);

	std::cout << "До: " << v << std::endl;
	
	insert_if(v, std::not1(std::bind2nd(std::modulus<int>(), 2)), 0);

	std::cout << "После: " << v << std::endl;

	return 0;
}


Output:
1
2
До: [1, 2, 3, 4]
После: [1, 2, 0, 3, 4, 0]


Create a new paste based on this one


Comments: