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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:30:48+00:00 2026-06-17T12:30:48+00:00

How do I iterate through a list of numbers, and how many different ways

  • 0

How do I iterate through a list of numbers, and how many different ways are there to do it?

What I thought would work:

#include <cstdlib>
#include <iostream>
#include <list>


using namespace std;


int main()

{
    int numbers[] = {2, 4, 6, 8};
    int i = 0;
    for(i=0; i< numbers.size();i++)
            cout << "the current number is " << numbers[i];


    system("pause");

    return 0;

}

I get an error on the for loop line:

request for member 'size' in 'numbers', which is of non-class type 'int[4]'

  • 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-17T12:30:49+00:00Added an answer on June 17, 2026 at 12:30 pm

    Unlike a lot of modern languages plain C++ arrays don’t have a .size() function. You have a number of options to iterate through a list depending on the storage type.

    Some common options for storage include:

    // used for fixed size storage. Requires #include <array>
    std::array<type, size> collection;
    
    // used for dynamic sized storage. Requires #include <vector>
    std::vector<type> collection;
    
    // Dynamic storage. In general: slower iteration, faster insert
    // Requires #include <list>     
    std::list<type> collection;  
    
    // Old style C arrays
    int myarray[size]; 
    

    Your options for iteration will depend on the type you’re using. If you’re using a plain old C array you can either store the size somewhere else or calculate the size of the array based on the size of it’s types. Calculating the size of an array has a number of drawbacks outlined in this answer by DevSolar

    // Store the value as a constant
    int oldschool[10];
    for(int i = 0; i < 10; ++i) {
        oldschool[i]; // Get
        oldschool[i] = 5; // Set
    } 
    
    // Calculate the size of the array
    int size = sizeof(oldschool)/sizeof(int);
    for(int i = 0; i < size; ++i) {
        oldschool[i]; // Get
        oldschool[i] = 5; // Set
    }
    

    If you’re using any type that provides a .begin() and .end() function you can use those to get an iterator which is considered good style in C++ compared to index based iteration:

    // Could also be an array, list, or anything with begin()/end()
    std::vector<int> newschool;     
    
    // Regular iterator, non-C++11
    for(std::vector<int>::iterator num = newschool.begin(); num != newschool.end(); ++num) {
        int current = *num; // * gets the number out of the iterator
        *num = 5; // Sets the number.
    }
    
    // Better syntax, use auto! automatically gets the right iterator type (C++11)
    for(auto num = newschool.begin(); num != newschool.end(); ++num) {
        int current = *num; // As above
        *num = 5;
    }
    
    // std::for_each also available
    std::for_each(newschool.begin(), newschool.end(), function_taking_int);
    
    // std::for_each with lambdas (C++11)
    std::for_each(newschool.begin(), newschool.end(), [](int i) {
        // Just use i, can't modify though.
    });
    

    Vectors are also special because they are designed to be drop-in replacements for arrays. You can iterate over a vector exactly how you would over an array with a .size() function. However this is considered bad practice in C++ and you should prefer to use iterators where possible:

    std::vector<int> badpractice;
    for(int i = 0; i < badpractice.size(); ++i) {
        badpractice[i]; // Get
        badpractice[i] = 5; // Set
    }
    

    C++11 (the new standard) also brings the new and fancy range based for that should work on any type that provides a .begin() and .end(). However: Compiler support can vary for this feature. You can also use begin(type) and end(type) as an alternative.

    std::array<int, 10> fancy;
    for(int i : fancy) {
        // Just use i, can't modify though.
    }
    
    // begin/end requires #include <iterator> also included in most container headers.
    for(auto num = std::begin(fancy); num != std::end(fancy); ++num) {
        int current = *num; // Get
        *num = 131; // Set
    }
    

    std::begin also has another interesting property: it works on raw arrays. This means you can use the same iteration semantics between arrays and non-arrays (you should still prefer standard types over raw arrays):

    int raw[10];
    for(auto num = std::begin(raw); num != std::end(raw); ++num) {
        int current = *num; // Get
        *num = 131; // Set
    }
    

    You also need to be careful if you want to delete items from a collection while in a loop because calling container.erase() makes all existing iterators invalid:

    std::vector<int> numbers;
    for(auto num = numbers.begin(); num != numbers.end(); /* Intentionally empty */) {
        ...
    
        if(someDeleteCondition) {
            num = numbers.erase(num);
        } else {
            // No deletition, no problem
            ++num;
        }
    }
    

    This list is far from comprehensive but as you can see there’s a lot of ways of iterating over a collection. In general prefer iterators unless you have a good reason to do otherwise.

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

Sidebar

Related Questions

Does map() iterate through the list like for would? Is there a value in
Each key in dictionary has list of MANY integers. I need to iterate through
I am using a for loop to iterate through a list of arrays that
In Java, is there a way to iterate through a list of variables (e.g.
Using SQLite3 with Python 2.5, I'm trying to iterate through a list and pull
I am trying to iterate through a list, and I need to perform specific
I am trying to iterate through a list checking every string in the list
Is it slower to iterate through a list in Java like this: for (int
Ok, What I need to do is iterate through a list of list and
I have a list of questions that the user will iterate through, they can

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.