Passing function to a function

Ok so I was coding a template, which requires me to pass a function. Very similar to how you can pass a function to std::sort.

However I noticed that for sort I can pass any combination of const and pass by reference or pass by value, and use lambda or function pointer. How can I implement such a feature?

Try something like:

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

using namespace std;

template<typename Container, typename Comparator>
void mysort(Container& container, Comparator comparator)
{
    sort(container.begin(), container.end(), comparator);
}

int main()
{
    vector<int> blah = { 7, 3, 6, 1};
    mysort(blah, [](const auto lhs, const auto rhs)
            {
                return lhs > rhs;
            });

    for (const auto& x : blah)
    {
        cout << x << " ";
    }
    cout << endl;
}
1 Like

Thanks. Will I be able to omit template arguments even if I have other template arguments.
I should always send full code as I realise now.

kinda long, but main details are easy to see
#include <iostream>
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
using ll = long long int;
//T is type of node
//U is type of lazy node
template<typename T, typename U, typename f1, typename f2, typename f3>
struct lazy_segment_tree{
    int n;
    int H;
    T base;
    vector<T> segtree;
    vector<U> lazytree;
    vector<bool> isempty;
    f1 join;
    f2 assign;
    f3 lazyassign;
    lazy_segment_tree(vector<T> &seq, f1 join, f2 assign, f3 lazyassign, T base) : 
    join(join), assign(assign), lazyassign(lazyassign){
        n=seq.size();
        H = 8*sizof(n) - __builtin_clz(n);
        segtree.resize(2*n);
        lazytree.resize(n);
        isempty.resize(n,1);
        for(int i=0;i<n;i++){
            segtree[n+i]=seq[i];
        }
        for(int i=n-1;i>=1;i--){
            segtree[i]=join(segtree[(i<<1)], segtree[(i<<1)|1]);
        }
    }
    void calc(int pos, int h){
        segtree[pos]=join(segtree[(pos<<1)],segtree[(pos<<1)|1]);
        if(!isempty[pos]){
            segtree[pos]=assign(h, lazytree[pos],segtree[pos]);
        }
    }
    void apply(int pos, U value, int h){
        segtree[pos]=assign(h, value, segtree[pos]);
        if(pos<n){
            if(!isempty[pos]){
                lazytree[pos]=lazyassign(value, lazytree[pos]);
            }
            else{
                lazytree[pos] = value;
            }
            isempty[pos]=0;
        }
    }
    void updatenode(int pos){
        int h=1;
        pos+=n;
        while(pos>1){
            h++;
            pos>>=1;
            calc(pos, h);
        }
    }
    void push(int pos){
        int h=H;
        int k=1<<(H-1);
        for(pos+=n;h;--h, k>>=1){
            int x=(pos>>h);
            if(!isempty[x]){
                apply((x<<1), lazytree[x], h);
                apply((x<<1)|1, lazytree[x], h);
                isempty[x]=1;
            }
        }
    }
    void updaterange(int l, int r, U value){
        push(l);
        push(r-1);
        int k=1;
        int l0=l, r0=r;
        for(l+=n, r+=n;l<r;l>>=1, r>>=1, k++){
            if(l&1){
                apply(l++, value, k);
            }
            if(r&1){
                apply(--r, value,k);
            }
        }
        updatenode(l0);
        updatenode(r0-1);
    }
    T query(int l, int r){
        push(l);
        push(r-1);
        T ansl=base;
        T ansr=base;
        for(l+=n, r+=n;l<r;l>>=1, r>>=1){
            if(l&1){
                ansl=join(ansl, segtree[l++]);
            }        
            if(r&1){
                ansr=join(segtree[--r], ansr);
            }
        }
        return join(ansl,ansr);
    }
};
void solve(){
    int n;
    cin>>n;
    lazy_segment_tree<int,int> segtree(vector<int>(n),
        [&](int x,int y){return x+y;}, 
        [&](int h,int lazy,int value){return (1<<h)*lazy + value;},
        [&](int lazy1, int lazy2){return lazy1 + lazy2;}
    );
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}

edit : sorry for unnecessary code, I was cleaning up my templates

1 Like