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 8348397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:40:35+00:00 2026-06-09T07:40:35+00:00

I have the following array: int* myArray = new int[45]; If I wanted to

  • 0

I have the following array:

int* myArray = new int[45];

If I wanted to iterate each element without knowing the actual size of the array, I would need to use a for_each?

If so, then how would you write the for_each? I was looking over the following site and reading up on for_each but can’t figure out how to put this together.

http://www.cplusplus.com/reference/algorithm/for_each/


Update: A for_each is not a good choice in this case, due to the fact that the size of the array has to be known. vectors are the proper way to accomplish such task. My reason for using arrays, in this case, was for learning purposes. if this was a serious project I would move to something such as Lists/Vectors.

  • 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-06-09T07:40:37+00:00Added an answer on June 9, 2026 at 7:40 am

    If you use MSVC (Microsoft Visual C++), you can use "for each."

    for each(int i in arr) {
        cout << i << ' ' << endl;
    }
    

    NOTE: This only works in the block of code the array is declared in.

    If not, you can also use the new range-based for loop in the C++11 standard.

    for(int i : arr) {
        cout << i << ' ' << endl;
    }
    

    If you’re intent upon the std::for_each:

    for_each(arr,arr + 10,[] (int i) {
            cout << i << ' ' << endl;
    });
    

    NOTE: This requires knowledge of the size of the array (in this example, 10).

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

Sidebar

Related Questions

I have the following array of integers: int[] array = new int[7] { 1,
If i have the following: class Array{ public: Array (int n=2) : _n(n), buf(new
I have the following array: private int[,] testSamples = new testSamples[101,101]; It's supposed to
I have the following struct: typedef struct _chess { int **array; int size; struct
Lets say I have an array numbers that contains the following values: int numbers
For example, we have the following array: char data[]=new char[]{'A','S','O','R','T','I','N','G','E','X','A','M','P','L','E'}; and an index array:
The following code produces dangling references: int main() { int *myArray = new int[2]{
I have a UIView subclass that has the following in drawRect: for(int j=0; j<[myArray
Say I have the following array of integers: int[] numbers = { 1, 6,
I have the following function: public static T TryGetArrayValue<T>(object[] array_, int index_) { ...

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.