Funktor za negaciju

Napisati funktor Negation koji negira vrednost.

Test

#include <iostream>
#include <vector>
#include <algorithm>

namespace matf {

    // Ovde ide vasa implementacija

} // namespace matf

int main(void)
{
	std::vector<int> vec = { 1, -2, 3, -4, 5 };

	std::transform(vec.begin(), vec.end(), vec.begin(), matf::Negation());

	for (const auto &val : vec) {
		std::cout << val << " "; // Ispisuje: -1 2 -3 4 -5
	}
	std::cout << std::endl;

	std::vector<double> dvec = { 1.5, -2.5, 3.5, -4.5, 5.5 };

	std::transform(dvec.begin(), dvec.end(), dvec.begin(), matf::Negation());

	for (const auto &val : dvec) {
		std::cout << val << " "; // Ispisuje: -1.5 2.5 -3.5 4.5 -5.5
	}
	std::cout << std::endl;

	return 0;
}

Rešenje

main.cpp

#include <iostream>
#include <vector>
#include <algorithm>

namespace matf {

	struct Negation {
		template <typename T>
		T operator()(const T &value) const
		{
			return -value;
		}
	};

} // namespace matf

int main(void)
{
	std::vector<int> vec = { 1, -2, 3, -4, 5 };

	std::transform(vec.begin(), vec.end(), vec.begin(), matf::Negation());

	for (const auto &val : vec) {
		std::cout << val << " "; // Ispisuje: -1 2 -3 4 -5
	}
	std::cout << std::endl;

	std::vector<double> dvec = { 1.5, -2.5, 3.5, -4.5, 5.5 };

	std::transform(dvec.begin(), dvec.end(), dvec.begin(), matf::Negation());

	for (const auto &val : dvec) {
		std::cout << val << " "; // Ispisuje: -1.5 2.5 -3.5 4.5 -5.5
	}
	std::cout << std::endl;

	return 0;
}