5

I have a vector of strings and an empty vector of int:

vector<string> ints_as_strings;
vector<int>    ints_as_ints;

Goal is to transform the former into the latter.

std::transform with a lambda works:

std::transform(ints_as_strings.begin(),
               ints_as_strings.end(),
               back_inserter(ints_as_ints),
               [](const string& s) {return std::stoi(s);});    

However I just want to use std::stoi.

Passing the address of my mystoi function works:

int mystoi(string s) {return stoi(s);}

std::transform(ints_as_strings.begin(),
               ints_as_strings.end(),
               back_inserter(ints_as_ints),
               &mystoi);

However, directly passing the address of std::stoi doesn't work. Is this on account of the extra parameters (for which, however, default values are provided)?

Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
  • Why don't you want to use lambdas? What problems they create? – Sam Varshavchik Sep 20 '16 at 22:19
  • I am trying to understand why passing the address of my own function works whereas passing the address of `std::stoi` doesn't. – Marcus Junius Brutus Sep 21 '16 at 08:03
  • `std::stoi` has two parameters with default values: `int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );` --- `std::transform` wants UnaryOperation (one parameter) `template< class InputIt, class OutputIt, class UnaryOperation > OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op );` – ttldtor Sep 21 '16 at 11:50
  • 1
    Please, read [Function Pointer with default values](http://stackoverflow.com/questions/9760672/howto-c-function-pointer-with-default-values) – ttldtor Sep 21 '16 at 12:03

0 Answers0