Wambitz
Wambitz

Reputation: 395

How to read/write or iterate through a specific range of elements within a std::array?

I want to read/write some specific range of memory within a bigger piece of memory, basically I would like to do something like this:

std::array<int, 1024> big_array;
unsigned short int offset = 128;

template<std::size_t SIZE>
void ReadArray(std::array<int, size>&  big_array_with_address_offset)
{
  // ... some code in here 
}

ReadArray(big_array + offset); //--> This doesn't work

I was able to do something similar for another function I was working on (code below) with raw pointers as function parameter, but I'm trying to do this in a modern C++(11 on wards) way since I need to use std::array. I don't want to use raw pointers as function parameter.

int FindMinElement(int * array)
{
    // Min element from range: 128 to 384
    return *std::min_element(array, array+256);
}

FindMinElements(big_array.data()+128);

NOTE1: The only way I was able to work with a smaller std::array in the function parameter is with template<std::size_t SIZE>, otherwise I get compilation errors due to the size incompatibility.

NOTE2: I don't want to do this with std::vector since I need static memory and not dynamic.

Upvotes: 1

Views: 282

Answers (3)

Wambitz
Wambitz

Reputation: 395

I finally figure this out, this is what I was trying to achieve and what I meant about using iterators and modern C++ withouth raw pointers, P.W. answer was close to what I wanted to achieve:

short int FindMinElement(std::array<short int, 1>::iterator it);
{
    // Min element from range: 128 to 384
    return *std::min_element(it, it + 128);
}

int main()
{
    std::array<int, 1024> big_array{{0}}; 
    unsigned short int offset = 128;
    short int minval = 0;

    // Asuming big_array is populated in some other place before calling FindMin..()
    minval = FindMinElement(std::begin(big_array) + offset);
    return 0;
}

I wanted to be able to manipulate data without using a raw pointer, then the solution was to use an iterator as parameter instead of a pointer.

This also solves the compilation error mentioned in NOTE1 that I had when passing a reference of a bigger std::array to a smaller std::array function parameter

Upvotes: 0

P.W
P.W

Reputation: 26800

Pass the address of the array at the offset element to FindMinElement function. See below example on how to do it:

#include<iostream>
#include<array>
#include<algorithm>
using namespace std;

std::array<int, 1024> big_array;
unsigned short int offset = 128;

int FindMinElement(int *array)
{
    // Min element from range: 128 to 384
    return *std::min_element(array, array+256);
}

int main(void)
{  
    int counter = 0;
    for(auto & i : big_array) //populates the array from 0 to 1023
    {
        i = counter++; 
    }       
    cout << "Min element in the range 128 to 384 is: ";
    cout << FindMinElement(big_array.begin() + 128) << endl;
}

Output:

Min element in the range 128 to 384 is: 128

Upvotes: 2

Gonen I
Gonen I

Reputation: 6107

Send in an iterator to the beginning of the array instead of sending in the array.

Your FindMinElement function received a pointer, not really an array. Sending in an iterator is the closest equivalent.

Upvotes: 0

Related Questions