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

  • SEARCH
  • Home
  • 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 6821573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:33:39+00:00 2026-05-26T21:33:39+00:00

i have a question regaring c++ and arrays. let say i have a class

  • 0

i have a question regaring c++ and arrays.

let say i have a class which is called CustomArray, and is nothing more than an generic array wich has attributes for size and capacity, to make the array dynamic. defined as:

template<typename T>
class CustomArray
{
public:
    int capacity, size;
    T* items;
    //constructor

    //destructor

    //function1

    //function2

    //etc...
};

now i’m kinda stuck, i want to implement a function like:”

void performOnAllItems(/*function?*/)
{
    for(int i = 0; i < size; i++)
    {
        //perform function on element
    }
}

that takes another function as a parameter (if that is possible?) and performs it on all elements. is that possible? and if so… how?

thanks in advance.

  • 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-26T21:33:39+00:00Added an answer on May 26, 2026 at 9:33 pm

    Add members begin and end like so:

    T *begin() { return items; }
    
    T *end() { return items + size; }
    

    Create a functor that derives from std::unary_function.

    E.g.

    template <typename T>
    class MyFunc : std::unary_function<T, void> {
    public:
        void operator()(T& t) {
        // ...
        }
    };
    

    Then call std::foreach(foo.begin(), foo.end(), MyFunc);

    Update

    In C++11, you can use a lambda for the foreach:

    std::foreach(foo.begin(), foo.end(),
                  [/* (1) */](T& t) { /* ... */ }
                ); 
    

    If (1) isn’t empty, then the lambda is a closure; this is known as a capturing lambda, and Visual C++ 10 Lambda Expressions provides a nice example of this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP array question regarding searching which I'm hoping some kind person
quick question regarding Array's in xcode. I have ht efollowing code, which is supposed
I have a quick question regarding the scope of dynamic arrays, which I assume
I have the following class which I'm fighting over how to implement. The question
I have question regarding the SQLAlchemy. How can I add into my mapped class
i have a question regarding the AsyncTask class in android, and why it is
I have a question regarding iOS (or perhaps more accurately Objective-C) and properties. I
I am writing rspec test for my Cars class, and have a question regarding
I have two questions regarding the multidimensional arrays. I declared a 3D array using
I have a question regarding using KVO-compliant methods to insert/remove objects from an array.

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.