foreach in C++ int array

36,061

Solution 1

An array (a raw array) decays into a pointer when passed as an argument to a function, so your array has no size information.

You need to pass the length of the array explicitly into the function to know it inside the function.

Alternatively, and better, use a std::vector and then you'll have the .size() always available when needed.

Solution 2

Apart from using vectors, as Tony suggests, you can use templates and pass the array by reference so that the compiler will deduce the array's size:

template<int N>
void testFunction(int mainProd,int (&addons)[N])
{
    for(int x = 0; x < N; ++x) // ---- working 
    {
        std::cout<< addons[x];
    }
}

Solution 3

You're using concepts of C# in C++ but, even if we assume that both languages are similar, they're not equal.

The syntax for a ranged-for in C++ is the following:

for (type identifier : container) // note the ':', not ';'
{
    // do stuff
}

You can use this for flavour if you have a C++11 compiler.

Btw, it seems that you're using properties on your code:

for(int x = 0 ; addons.length;++x) // what is lenght?
{
    std::cout<< addons[x];
}

There's no such thing in C++, if you want to call an object method you need to call it as a function:

// assuming that the object 'addons' have a method
// named 'length'  that takes no parameters
addons.length();

But the addons variable isn't an object, is an array (take a look to this tutorial), so it doesn't have a method or property named length; if you need to know its length in order to iterate it you can use in some contexts the sizeof operator (see the tutorial for more information).

Let's suppose that addons were a container:

typedef std::vector<addon> Addons;
Addons addons;

If you want to iterate it using the C++11 range-for, you can write it as follows:

for (addon a : addons)
{
    // do stuff with a.
}

Hope it helps.

Solution 4

If you were to use a std::vector or std::array, you could use std::foreach,

std::vector<int> addons = {7,8,9,10};
std::array<int, 4> addons = {7,8,9,10}; // Only use one of these...
std::foreach(addons.begin(), addon.end(), [](int i) {
    std::cout << i
});

Solution 5

If you don't want to use any STL container, you just need to pass the array by reference to the function. Problem here is that you can't define such argument without exact size of the array. This restriction you can overcome making the function as a template, defining the size as the template parameter:

#include <iostream>

template<int N>
void testFunction(int mainProd,int (&addons)[N])
{
    for(int x = 0 ; x < N; ++x)
    {
        std::cout<< addons[x];
    }
}

int main()
{
    int mainProd=2;
    int Addons[]={7,8,9,10};

    testFunction(mainProd,Addons);
    return 0;
}
Share:
36,061
junni lomo
Author by

junni lomo

Updated on July 09, 2022

Comments

  • junni lomo
    junni lomo almost 2 years

    I am new to C++ and I am writing the following code. I needed to iterate over all the addons in my calling function - testFunction. I know this works in C#, but this code is not working. Can anyone please point out the right way to do it in C++?

    #include "stdafx.h"
    #include <iostream>
    #include "resource.h"
    
    int testFunction(char* tester);
    int _tmain()
    {
        int mainProd=2;
        int Addons[]={7,8,9,10};
    
        testFunction(mainProd,Addons);
    
    
    }
    void testFunction(int mainProd,int addons[])
    {
        for(int x = 0 ; addons.length;++x) ---- Not working 
        {
            std::cout<< addons[x];
        }
    }
    

    I tried to implement vector as below suggestions by you guys

    #include "stdafx.h"
    #include <iostream>
    #include "resource.h"
    #include <vector>
    
    void testFunction(std::vector<int> addons);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        std::vector<int>  Addons ;
        for(int i = 0 ;i<10;++i)
        {
            Addons.push_back(i);
        }
         testFunction(Addons);
    }
    
    void testFunction(std::vector<int> addons)
    {
        for(int i =0 ; i<addons.size();++i)
        {
            std::cout<<addons.at(i);
        }
    }
    
  • Armen Tsirunyan
    Armen Tsirunyan over 11 years
    The lambda should take int, not an iterator
  • Alex Chamberlain
    Alex Chamberlain over 11 years
    @ArmenTsirunyan My mistake, sorry.
  • Björn Pollex
    Björn Pollex over 11 years
    Or std::array if the size is fixed at compile-time.
  • Marc Glisse
    Marc Glisse over 11 years
    Might as well use the new range-for then.
  • Alex Chamberlain
    Alex Chamberlain over 11 years
    @MarcGlisse You could, but I don't like the syntax; not as readable.
  • Tony The Lion
    Tony The Lion over 11 years
    Because you have an function with a char* parameter declared before your main and only defined the correct one after. ideone.com/2H1xX8
  • TemplateRex
    TemplateRex over 11 years
    or pass an array by reference
  • Alex Chamberlain
    Alex Chamberlain over 11 years
    There are a lot of typos in this answer.
  • PaperBirdMaster
    PaperBirdMaster over 11 years
    @AlexChamberlain could you help me to fix it? i don't found any typos, maybe you're referring to my lack of english skills?
  • PaperBirdMaster
    PaperBirdMaster over 11 years
    std::foreach vs range-for could be a matter of taste?
  • Paul
    Paul over 9 years
    A raw array will also work with std::begin() and std::end(). (Not that I would recommend using raw arrays)