Jump to content

Extracting partial array from array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
Is there a way, in C++, to extract part of an array?

I have some array, and I want to extract part of it to use as a parameter for a function. But I would prefer to do this without using for loops or using more variables than the array itself. Something like array[3-5].

I am pretty sure I have worked with languages where you could do this using '-' or '~', I am just not sure if it's possible in C++. I imagine it could possibly be done with pointer somehow.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you are dealing with an array that isn't specially terminated, such as null terminated char arrays, then you MUST pass the size of the array as a minimum requirement for any function. You will need three parameters: a pointer to the array, and a minimum and maximum index. If you use a vector, you will have more flexibility.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
So I would not be able to do something like func(array[3-5]), but would have put the part I want into a new array instead?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
When you pass an array to a function, you pass the entire array. You can instead do:
Func(array,3,5)
or you can copy a section of the array to array2 and call
Func(array2,3)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
At the very least you need a length parameter on top of the array. Then you can pass a slice of the array--by offsetting the pointer--and use the length to retrieve the rest of the elements:

#include <iostream>


using namespace std;


template <typename T>

void print_slice(T *a, int n)

{

  while (--n >= 0)

    cout << *a++ << '\n';

}


int main()

{

  int a[] = {1, 2, 3, 4, 5};


  print_slice(a + 2, 2);

}

But no, C++ doesn't directly support full slicing of arrays where you can say something like a[2..3] and get {3, 4} as a result.

#6
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
CPD, brackets are usually considered to represent inclusive points.

#7
lucuis

lucuis

    Newbie

  • Members
  • PipPip
  • 21 posts
As far as I know, array in C++ it's just a pointer to the first element. You can't even figure out it's size. The only thing you can do with it is to shift the pointer to the first element. So slicing can be done by shifting first element and adjusting the size.

---

Edited by WingedPanther, 01 December 2008 - 08:43 AM.
Double post


#8
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts

Quote

CPD, brackets are usually considered to represent inclusive points.
What's your point?

Quote

So slicing can be done by shifting first element and adjusting the size.
Yes, that's it exactly.

#9
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts

CPD said:


But no, C++ doesn't directly support full slicing of arrays where you can say something like a[2..3] and get {3, 4} as a result.

My point? You use brackets (in maths terms) and don't represent all inclusive points.