Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach in C++ int array

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);
    }
}
like image 362
junni lomo Avatar asked Jan 15 '13 13:01

junni lomo


5 Answers

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.

like image 198
Tony The Lion Avatar answered Oct 19 '22 02:10

Tony The Lion


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;
}
like image 45
Sergey Goravsky Avatar answered Sep 21 '22 19:09

Sergey Goravsky


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];
    }
}
like image 7
Armen Tsirunyan Avatar answered Oct 19 '22 03:10

Armen Tsirunyan


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.

like image 3
PaperBirdMaster Avatar answered Oct 19 '22 01:10

PaperBirdMaster


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
});
like image 2
Alex Chamberlain Avatar answered Oct 19 '22 02:10

Alex Chamberlain