#include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <numeric> double myfunction(double num) { return exp(num); } template <typename T> void softmax(const typename::std::vector<T> &v, typename::std::vector<T> &s){ double sum=0.0; transform(v.begin(), v.end(), s.begin(), myfunction); sum=accumulate(s.begin(), s.end(), sum); for(size_t i=0; i<s.size(); ++i) s.at(i)/=sum; } int main() { double a[]={1.0, 3.0, 2.0}; std::vector<double> v_a(a, a+sizeof a/sizeof a[0]), v_b(v_a); std::vector<double>::const_iterator it=v_a.begin(); for(; it!=v_a.end(); ++it) { std::cout<<*it<<" "; } std::cout<<std::endl; softmax(v_a, v_b); it=v_b.begin(); for(; it!=v_b.end(); ++it) { std::cout<<*it<<" "; } std::cout<<std::endl; return 0; }