Search
 
SCRIPT & CODE EXAMPLE
 

CPP

function as argument in another function in c++

#include <bits/stdc++.h>
using namespace std;

bool compare(int a, int b)
{
    return a > b;
}

void ans(int x, int y, bool (&func)(int a, int b))
{
    (func(x, y)) ? cout << x << " is bigger than" << y : cout << y << " is bigger than " << x << endl;
}

int main()
{
    ans(10, 20, compare);

    return 0;
Comment

function as argument in another function in c++

/*
 * https://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c
 */

//! How to use function as argument or pass a function as parameter in in C++

#include <bits/stdc++.h>
using namespace std;

bool compare(int a, int b)
{
    return a > b;
}

//! Way 01
void Way1(int x, int y, bool (&compare)(int a, int b))
{
    (compare(x, y)) ? cout << x << " is bigger than " << y : cout << y << " is bigger than " << x << endl;
}

//! Way 02
void Way2(int a, int b, void (*Way1)(int, int, bool (&func)(int a, int b)))
{
    Way1(a, b, compare);
}

//! Way 03
/* Default argument isn't allowed to be passed */
void Way3(int p, int q, function<void(int, int, void (*Way1)(int, int, bool (&func)(int a, int b)))> Way2)
{
    Way2(p, q, Way1);
}

int main()
{
    Way3(200, 100, Way2);

    return 0;
}
Comment

passing function to another function in c++

1
2
3
int func (int)
int (*func) (int)
int (&func) (int)
Comment

passing function to another function in c++

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

void transform(std::vector<int>::iterator beginIt,
    std::vector<int>::iterator endIt,
    std::vector<int>::iterator destinationBeginIt,
    int func (int)) {
  while (beginIt != endIt) {
    *destinationBeginIt = func(*beginIt);
    beginIt++;
    destinationBeginIt++;
  }
}

int main() {
  std::vector<int> numbers{1, 2, 3}; // numbers has 3 values: 1, 2, 3
  std::vector<int> bigNumbers(3); // bigNumbers has 3 values, default
                                  // initialized: 0, 0, 0

  transform(
    numbers.begin(),
    numbers.end(),
    bigNumbers.begin(),
    [](int small) {
      return small * 10;
    }
  );

  // Print the values of bigNumbers
  for (const auto big : bigNumbers) {
    std::cout << big << std::endl;
  }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: remove element from vector on condition c++ 
Cpp :: rank() in c++ 
Cpp :: c++ check if string contains non alphanumeric 
Cpp :: string to int arduino 
Cpp :: remove at index vector c++ 
Cpp :: c++ cli convert string to string^ 
Cpp :: c++ create threads 
Cpp :: C++ mutex lock/unlock 
Cpp :: c++ rule of five 
Cpp :: c++ user input 
Cpp :: getch c++ library 
Cpp :: capitalize first letter c++ 
Cpp :: what is the short cut way to find the max and min element in an array in c++ 
Cpp :: c++ declaring and initializing strings 
Cpp :: vector erase specific element 
Cpp :: cpp macro 
Cpp :: how to read and parse a json file with rapidjson 
Cpp :: how to write something in power of a number in c++ 
Cpp :: use lower bound in pair vector 
Cpp :: matplotlib hide numbers on axis 
Cpp :: int to string c++ 
Cpp :: max heap in c++ 
Cpp :: segmented sieve of Eratosthenes in cpp 
Cpp :: c++ inline in .cpp and not in header 
Cpp :: print 2d array c++ 
Cpp :: c++ get character from string 
Cpp :: how to get size of 2d vector in c++ 
Cpp :: how to dynamically allocate an array c++ 
Cpp :: cpp get last element of vector 
Cpp :: built in function in c++ for binary to decimal 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =