How to Aggregate Vector Functions

In the C++ programming language, vector functions are pointers to functions that return vectors. Vectors are a data containment device used to store data serially, but allow random access. Function pointers are often used as arguments to functions themselves, such as passing a sorting function pointer into a sorting function. As such, you may need to collect, or aggregate, all of the vector functions you wish to use. This can be done with a simple container.

Things You'll Need

  • C++ IDE such as Eclipse CDT
  • C++ Compiler such as GCC
Show More

Instructions

    • 1

      Load the C++ Integrated Development Environment by clicking on its program icon. When it opens, select "File," "New," "Project" and choose "C++ Project" to create a new C++ project. A blank source code file appears in the text editor portion of the IDE.

    • 2

      Write the following text at the top of the source code file in order to import the following libraries:

      #include <iostream>

      #include <vector>

      using namespace std;

    • 3

      Write the following to declare a vector function:

      vector<int> vectorFunction(){ vector<int> returnVecotr; return returnVector;}

    • 4

      Declare a main function. The main function is where your program begins execution. You can place all your program code in between the curly brackets that follow the main function declaration:

      int main()

      {}

    • 5

      Write a statement in between the curly brackets of the main function that defines an aggregation of vectors:

      vector<vector<int> > aggregation;

    • 6

      Write the logic for a "for" loop, a construct that repeats its nested code block a set number of times:

      for(int i = 0; i < 10; i++)

      {}

    • 7

      Write the following vector declaration in between the curly brackets of the "for" loop. This declares a function pointer named "foo" that points to a function that returns vectors of integer data types.

      vector<int> (*foo)();

    • 8

      Write the following to initialize the pointer function. This is done by setting it equal to the address of the function "vectorFunction."

      foo = & vectorFunction

    • 9

      Write the following code to push the vector into the aggregation vector. The aggregation collects all the vector pointer functions in a nice collection.

      aggregation.push_back(foo);

Related Searches:

References

Comments

Related Ads

Featured