1

I want to send a array to a function!

I'm a php programmer, so I write an example in php, and please convert it to C++:

function a($x) {
    foreach ($x as $w) print $w;
}

$test = array(1, 2, 3);
a($test);
James McNellis
  • 348,265
  • 75
  • 913
  • 977
mrdaliri
  • 7,148
  • 22
  • 73
  • 107

3 Answers3

11

The best way to do this is to have the function take a pair of iterators: one to the beginning of the range and one to the end of the range (which is really "one past the end" of the range):

template <typename ForwardIterator>
void f(ForwardIterator first, ForwardIterator last)
{
    for (ForwardIterator it(first); it != last; ++it)
        std::cout << *it;
}

then you can call this function with any range, whether that range is from an array or a string or any other type of sequence:

// You can use raw, C-style arrays:
int x[3] = { 1, 2, 3 };
f(x, x + 3);

// Or, you can use any of the sequence containers:
std::array<int, 3> v = { 1, 2, 3 };
f(v.begin(). v.end());

For more information, consider getting yourself a good introductory C++ book.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 1
    +1: Especially for last remark. C++ can become a nightmare if explored by experimentation. Are you sure the link is correct? It's pointing to boost::bind. – 6502 Jan 30 '11 at 21:20
  • @6502: Oops! Thanks for the heads-up. Copy-and-paste fail. – James McNellis Jan 30 '11 at 21:21
  • You should keep in mind that there exist standard library algorithms that let you pass both the iterators and the thing-to-do-at-each-step, and assemble the loop for you. – Karl Knechtel Jan 30 '11 at 22:06
  • @Karl: There are, but they are often a beating to use (they are far more useful with the soon-to-be-standard C++0x lambda expressions). – James McNellis Jan 30 '11 at 22:52
  • [This particular function could be implemented using `for_each` and `ostream_iterator` quite easily, but it's usually not quite so straightforward.] – James McNellis Jan 30 '11 at 23:15
2

Try this method:

int a[3];
a[0]=1;
a[1]=...

void func(int* a)
{
   for( int i=0;i<3;++i )
      printf("%d",a++);
}
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • 1
    @Paul R: That's perfectly valid C++, although it's not what one would normally consider idiomatic C++. – Greg Hewgill Jan 30 '11 at 21:31
  • @Greg: I know, but my point is that it is highly misleading to give a C++ newcomer examples which are really just C code that happens to compile as C++, rather than more appropriate and idiomatic C++. I would down-vote this answer if I had any votes left today. – Paul R Jan 30 '11 at 22:16
1
template <typename T, size_t N>
void functionWithArray(T (&array)[N])
{
    for (int i = 0; i < N; ++i)
    {
        // ...
    }
}

or

void functionWithArray(T* array, size_t size)
{
    for (int i = 0; i < size; ++i)
    {
        // ...
    }
}

The first one uses an actual array and the length of the array does not need to be specified since its known at compile time. The second one points to a block of memory so the size needs to be specified.

These functions can be used in two different ways:

int x[] = {1, 2, 3};
functionWithArray(x);

and:

int* x = new int[3];
x[0] = 1;
x[1] = 2;
x[2] = 3;
functionWithArray(x, 3);
Marlon
  • 19,924
  • 12
  • 70
  • 101