Prev: how to non-default construct with multiple virtual inheritance??
Next: Access method names for std::random
From: ManicQin on 2 Jun 2010 05:06 Hi, I hope the subject wasn't too obfuscated, Let's say that I have a vector<string> and a function size_t foo(string inVal) { return someNumberRelatedToTheString; } I want to accumulate the return values of foo calls. currently I failed doing it in a one accumulate call so I broke it into transform call that inserts the returns to a vector and than accumulate the vector Is there a way to do it in one accumulate call? thanks. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Paul Bibbings on 2 Jun 2010 17:29 On Jun 2, 9:06 pm, ManicQin <manic...(a)gmail.com> wrote: > Hi, > > I hope the subject wasn't too obfuscated, > Let's say that I have a vector<string> and a function > > size_t foo(string inVal) > { > return someNumberRelatedToTheString; > > } > > I want to accumulate the return values of foo calls. > > currently I failed doing it in a one accumulate call so I broke it > into transform call that inserts the returns to a vector and than > accumulate the vector > > Is there a way to do it in one accumulate call? You may be able to adapt something along the following lines to suit your requirements. #include <string> #include <vector> #include <numeric> #include <functional> #include <cstddef> #include <iostream> size_t foo(std::string inVal) { return inVal.size(); } struct counter : public std::binary_function<size_t, std::string, size_t> { typedef std::binary_function< size_t, std::string, size_t > bin_func; typedef bin_func::first_argument_type first_argument_type; typedef bin_func::second_argument_type second_argument_type; typedef bin_func::result_type result_type; result_type operator()(first_argument_type Arg1, second_argument_type Arg2) { return Arg1 + foo(Arg2); } }; int main() { std::vector<std::string> string_vec; string_vec.push_back("one"); string_vec.push_back("two"); string_vec.push_back("three"); size_t count = std::accumulate(string_vec.begin(), string_vec.end(), 0, counter()); std::cout << "count: " << count << '\n'; } /** * Output: * count: 11 */ Regards Paul Bibbings -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: AnonMail2005 on 2 Jun 2010 17:29 On Jun 2, 4:06 pm, ManicQin <manic...(a)gmail.com> wrote: > Hi, > > I hope the subject wasn't too obfuscated, > Let's say that I have a vector<string> and a function > > size_t foo(string inVal) > { > return someNumberRelatedToTheString; > > } > > I want to accumulate the return values of foo calls. > > currently I failed doing it in a one accumulate call so I broke it > into > transform call that inserts the returns to a vector and than > accumulate the vector > > Is there a way to do it in one accumulate call? There may be but writing your own functor is very straight forward. // accumulate functor. make it a class (with private members) if you wish struct MyFunctor { MyFunctor(size_t & total) : m_total(total) {} void operator () (const std::string & s) const { m_total += s.size(); } // reference to external variable size_t & m_total; }; Sample usage where v is your vector of strings: size_t total = 0; std::for_each(v.begin(), v.end(), MyFunctor(total)); HTH -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: mingze zhang on 2 Jun 2010 17:32
On Jun 3, 4:06 am, ManicQin <manic...(a)gmail.com> wrote: > Hi, > > I hope the subject wasn't too obfuscated, > Let's say that I have a vector<string> and a function > > size_t foo(string inVal) > { > return someNumberRelatedToTheString; > > } > > I want to accumulate the return values of foo calls. > > currently I failed doing it in a one accumulate call so I broke it > into > transform call that inserts the returns to a vector and than > accumulate the vector > > Is there a way to do it in one accumulate call? > > thanks. { quoted banner removed; please do it yourself. -mod } use for_each vector<string> a; a.push_back("abc"); a.push_back("def"); int b = 0; for_each(a.begin(), a.end(), [&b](const string &inVal){ b = b + foo(inVal); }); -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |