How to minupulate the elements (counting by 3) of an array usingSTL?

T

Tony Young

Hi,

I have an array of integer. I need to find the max of the 0th, 3th, 6th
, 9th, ... elements in the array. I wonder if STL (standard template
library) provides a way to manipulate only part of an array (rather than
whole), e.g. 0th, 3th, 6th , 9th, ... in this case. Please advise. Thanks.

Tony
 
V

Victor Bazarov

Tony said:
I have an array of integer. I need to find the max of the 0th, 3th,
6th , 9th, ... elements in the array. I wonder if STL (standard
template library) provides a way to manipulate only part of an array
(rather than whole), e.g. 0th, 3th, 6th , 9th, ... in this case.

No, it doesn't. Roll your own.

V
 
M

Marcus Kwok

Victor Bazarov said:
No, it doesn't. Roll your own.

What about std::valarray and std::slice? In fact, there may be a better
way, but I do not use valarrays very often (does anybody?).


#include <iostream>
#include <valarray>

int main()
{
const int N = 300;
int arr[N];

for (int i = 0; i < N; ++i) {
arr = i;
}

std::valarray<int> varr(arr, N);
std::valarray<int> arr3 = varr[std::slice(0, varr.size() / 3, 3)];

std::cout << "max = " << arr3.max() << '\n';
}
 
V

Victor Bazarov

Marcus said:
Victor Bazarov said:
No, it doesn't. Roll your own.


What about std::valarray and std::slice? In fact, there may be a better
way, but I do not use valarrays very often (does anybody?).
[...]

Neither do I, that's why it didn't occur to me to suggest it.

V
 
N

Neil Cerutti

What about std::valarray and std::slice? In fact, there may be
a better way, but I do not use valarrays very often (does
anybody?).

It's one of the things included in the standard library that's
hard to understand. B.Stroustrup puts it something like this in
_TCPL_, but it was unenlightening to me: A vector optimized for
computations. Huh?

My decision was that if I needed it I would know what he meant,
so I ignored valarrays.
 
M

Marcus Kwok

Neil Cerutti said:
It's one of the things included in the standard library that's
hard to understand. B.Stroustrup puts it something like this in
_TCPL_, but it was unenlightening to me: A vector optimized for
computations. Huh?

My decision was that if I needed it I would know what he meant,
so I ignored valarrays.

Yeah, I found this (TC++PL:SE section 22.4):

In particular, the primary design criterion [of valarray] wasn't ease
of use, but rather effective use of high-performance computers when
relying on aggressive optimization techniques. If your aim is
flexibility and generality rather than efficiency, you are probably
better off building on the standard containers ... than trying to fit
into the simple, efficient, and deliberately traditional framework of
valarray.

...

A valarray is a vector optimized for numeric computation, a vector is
a flexible container designed for holding and manipulating objects of
a wide variety of types, and an array is a low-level, built-in type.

I do agree that they seem pretty esoteric though.
 
N

Neil Cerutti

Marcus said:
Victor Bazarov said:
Tony Young wrote:

I have an array of integer. I need to find the max of the 0th, 3th,
6th , 9th, ... elements in the array. I wonder if STL (standard
template library) provides a way to manipulate only part of an array
(rather than whole), e.g. 0th, 3th, 6th , 9th, ... in this case.

No, it doesn't. Roll your own.


What about std::valarray and std::slice? In fact, there may
be a better way, but I do not use valarrays very often (does
anybody?).
[...]

Neither do I, that's why it didn't occur to me to suggest it.

Looking at the SGI library docs, slice_array does not provide
iterators, so valarray doesn't provide what he needs. He'll need
to roll his own, unless he doesn't mind making copies of the
items over which he iterates.
 
P

pookiebearbottom

Just curious, if 0th, 3rd, 6th, ... are significant, are you sure that
array[int] is the correct data structure?
 
M

Marcus Kwok

Neil Cerutti said:
Looking at the SGI library docs, slice_array does not provide
iterators, so valarray doesn't provide what he needs. He'll need
to roll his own, unless he doesn't mind making copies of the
items over which he iterates.

Also, Stroustrup provides an implementation for a Slice_iter class on
p.670 of _TC++PL:SE_ :


template <class T>
class Slice_iter {
valarray<T>* v;
slice s;
size_t curr; // index of current element

T& ref(size_t i) const { return (*v)[s.start() + i*s.stride()]; }
public:
Slice_iter(valarray<T>* vv, slice ss) : v(vv), s(ss), curr(0) { }

Slice_iter end() const
{
Slice_iter t = *this;
t.curr = s.size(); // index of last-plus-one element
return t;
}

Slice_iter& operator++() { curr++; return *this; }
Slice_iter operator++(int) { Slice_iter t = *this; curr++; return t; }

T& operator[](size_t i) { return ref(i); } // C style subscript
T& operator()(size_t i) { return ref(i); } // Fortran-style subscript
T& operator*() { return ref(curr); } // current element

friend bool operator==<>(const Slice_iter& p, const Slice_iter& q);
friend bool operator!=<>(const Slice_iter& p, const Slice_iter& q);
friend bool operator< <>(const Slice_iter& p, const Slice_iter& q);
};


template <class T>
bool operator==(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return p.curr == q.curr &&
p.s.stride() == q.s.stride() &&
p.s.start() == q.s.start();
}

template <class T>
bool operator!=(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return !(p == q);
}

template <class T>
bool operator<(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return p.curr < q.curr &&
p.s.stride() == q.s.stride() &&
p.s.start() == q.s.start();
}
 
R

Robbie Hatley

Tony Young said:
I have an array of integer. I need to find the max of the 0th, 3th, 6th
, 9th, ... elements in the array. I wonder if STL (standard template
library) provides a way to manipulate only part of an array (rather than
whole), e.g. 0th, 3th, 6th , 9th, ... in this case. Please advise.
Thanks.

This is one of those rare cases where I'd advice you
to use built-in arrays and for loops intstead of the
STL. If you want to process just every third element,
simply increment through the array by threes instead
of by ones.

Something like this sould do the trick (untested;
debug it yourself):

int DivThreeMax(int Array[], int Size)
{
int MaxBuffer = Array[0];
for (int i = 0; i < Size; ++++++i) // note triple increment!
{
if (Array > MaxBuffer) MaxBuffer = Array
}
return MaxBuffer;
}

If you really need to apply STL functionality to this
kind of problem, use the for-loop technique above to
syphon every third element into an STL container.
Then you can use std. containers, iterators, algorithms,
etc. on the problem.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lone wolf intj at pac bell dot net
home dot pac bell dot net slant earnur slant
 
V

Victor Bazarov

Robbie said:
I have an array of integer. I need to find the max of the 0th, 3th, 6th
, 9th, ... elements in the array. I wonder if STL (standard template
library) provides a way to manipulate only part of an array (rather than
whole), e.g. 0th, 3th, 6th , 9th, ... in this case. Please advise.

Thanks.

This is one of those rare cases where I'd advice you
to use built-in arrays and for loops intstead of the
STL. If you want to process just every third element,
simply increment through the array by threes instead
of by ones.

Something like this sould do the trick (untested;
debug it yourself):

int DivThreeMax(int Array[], int Size)

I'd probably generalize it to be either

template<int howmany> int DivXXXMax(int Array[], int Size) // var 1

or

int DivXXXMax(int Array[], int Size, int howmany) // var 2

(and see below), or even further

template<class T... (T Array[], ...
{
int MaxBuffer = Array[0];
for (int i = 0; i < Size; ++++++i) // note triple increment!

Instead write

for (int i = howmany; i < Size; i += howmany) // var 1 or 2
{
if (Array > MaxBuffer) MaxBuffer = Array

^^^
A semicolon might be required there...
}
return MaxBuffer;
}

[..]

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top