454,385 Members | 1,737 Online
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 Dec 18 '05 #1
10 Replies

 P: n/a 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. V Dec 18 '05 #2

 P: n/a Victor Bazarov wrote: 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?). #include #include int main() { const int N = 300; int arr[N]; for (int i = 0; i < N; ++i) { arr[i] = i; } std::valarray varr(arr, N); std::valarray arr3 = varr[std::slice(0, varr.size() / 3, 3)]; std::cout << "max = " << arr3.max() << '\n'; } -- Marcus Kwok Dec 19 '05 #3

 P: n/a On 2005-12-19, Marcus Kwok wrote: Victor Bazarov wrote: 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?). 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. -- Neil Cerutti Dec 19 '05 #4

 P: n/a Marcus Kwok wrote: Victor Bazarov wrote: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 (standardtemplate 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. V Dec 19 '05 #5

 P: n/a Neil Cerutti wrote: On 2005-12-19, Marcus Kwok wrote: 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. 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. -- Marcus Kwok Dec 19 '05 #6

 P: n/a On 2005-12-19, Victor Bazarov wrote: Marcus Kwok wrote: Victor Bazarov wrote: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 (standardtemplate 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. -- Neil Cerutti Dec 19 '05 #7

 P: n/a Just curious, if 0th, 3rd, 6th, ... are significant, are you sure that array[int] is the correct data structure? Tony Young wrote: 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 Dec 19 '05 #8

 P: n/a Neil Cerutti wrote: 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 Slice_iter { valarray* 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* 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 bool operator==(const Slice_iter& p, const Slice_iter& q) { return p.curr == q.curr && p.s.stride() == q.s.stride() && p.s.start() == q.s.start(); } template bool operator!=(const Slice_iter& p, const Slice_iter& q) { return !(p == q); } template bool operator<(const Slice_iter& p, const Slice_iter& q) { return p.curr < q.curr && p.s.stride() == q.s.stride() && p.s.start() == q.s.start(); } -- Marcus Kwok Dec 19 '05 #9

 P: n/a "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. 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[i] > MaxBuffer) MaxBuffer = Array[i] } 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 Dec 19 '05 #10

 P: n/a Robbie Hatley wrote: "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 templatelibrary) provides a way to manipulate only part of an array (rather thanwhole), 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 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 MaxBuffer) MaxBuffer = Array[i] ^^^ A semicolon might be required there... } return MaxBuffer; } [..] V Dec 19 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.