Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3393666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:04:41+00:00 2026-05-18T04:04:41+00:00

Suppose i want something simple like the following: I have an core-algorithm, which randomly

  • 0

Suppose i want something simple like the following:

I have an core-algorithm, which randomly selects one of the specialized algorithms (specialized at compile-time) and process this algorithm. These specialized algorithms are implemented trough functors.

The question is now: how to implement a container, which is build at compile-time, where the core-algorithm can first check the size of this container (“i got 4 algorithms -> need to randomly select algorithm 0-3”) and can then execute the functor in this container (“randomly chosen 2 -> process the third functor in container”).

How would one implement it as simple as possible? I suppose it is possible.

Is there any connection to the curiously recurring template idiom? (wiki link)
Is there a simple way with the use of Boost::Fusion? (official doc)

Edit: All the algorithms will be used in the core-algorithm. The use pattern (random-numbers) is a runtime-decision (so i don’t need compile-time-rands). The algorithm just has to know the container of functors and the size of this container for safe access.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:04:42+00:00Added an answer on May 18, 2026 at 4:04 am

    If you want your core-algorithm to execute a specialized algorithm, there should be some kind of contract between the core-algorithm and the specialized algorithm.

    If you define this contract as an interface, your container is simply a container containing pointers to these interfaces, e.g.:

    class IAlgorithm
       {
       public:
          virtual double operator()(double d) = 0;
       };
    
    typedef std::vector<IAlgorithm *> Algorithms;
    

    Calling a random algorithm is then simply taking the size of the vector, taking a random value between zero and the size of the list (0..size-1), taking the entry at that position and calling the interface.

    Alternatively, you can also use the new C++0x std::function construction, like this:

    #include <functional>
    typedef std::function<double(double)> Algorithm;
    typedef std::vector<Algorithm> Algorithms;
    

    Taking an algorithm is similar, you should be able to call an algorithm like this:

    Algorithms myAlgorithms;
    ...
    double myresult = myAlgorithms[2](mydouble);
    

    This approach has the advantage that you can also use lambda’s.

    EDIT: This is an example that uses lambda’s. It compiles and works as expected with Visual Studio 2010 (just tested this myself):

    #include <iostream>
    #include <vector>
    #include <functional> 
    typedef std::function<double(double)> Algorithm; 
    typedef std::vector<Algorithm> Algorithms; 
    
    int main()
    {
    Algorithms algorithms;
    algorithms.push_back([](double d)->double{return d+d;});
    algorithms.push_back([](double d)->double{return d*d;});
    
    std::cout << algorithms[0](5) << std::endl;
    std::cout << algorithms[1](5) << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose I have a long algorithm which I would like to be optionally verbose
Suppose I have one long long int and want to take its bits and
Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first
Of course most languages have library functions for this, but suppose I want to
Suppose that you have two huge files (several GB) that you want to concatenate
I have the following problem: Suppose I have some basic counter class Counter .
I have a simple question that keeps me up all night :( . Suppose
Suppose I want to open a file in an existing Emacs session using su
Suppose you want to make an async request in JavaScript, but you want to
Suppose I want to create a set of observers based on type. That is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.