473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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 1788
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
Victor Bazarov <v.********@comacast.net> 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 <iostream>
#include <valarray>

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

for (int i = 0; i < N; ++i) {
arr[i] = 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';
}

--
Marcus Kwok
Dec 19 '05 #3
On 2005-12-19, Marcus Kwok <ri******@gehennom.net> wrote:
Victor Bazarov <v.********@comacast.net> 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
Marcus Kwok wrote:
Victor Bazarov <v.********@comacast.net> 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?).
[...]


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

V
Dec 19 '05 #5
Neil Cerutti <le*******@email.com> wrote:
On 2005-12-19, Marcus Kwok <ri******@gehennom.net> 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
On 2005-12-19, Victor Bazarov <v.********@comAcast.net> wrote:
Marcus Kwok wrote:
Victor Bazarov <v.********@comacast.net> 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?).
[...]


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
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
Neil Cerutti <le*******@email.com> 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 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();
}

--
Marcus Kwok
Dec 19 '05 #9
"Tony Young" <jd*******@yahoo.com> 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
Robbie Hatley wrote:
"Tony Young" <jd*******@yahoo.com> 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)


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[i] > MaxBuffer) MaxBuffer = Array[i] ^^^
A semicolon might be required there...
}
return MaxBuffer;
}

[..]


V
Dec 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SunMan | last post by:
Hello! I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be...
9
by: Rafal Konopka | last post by:
How can you caount the number of occurrences of elements in an array? For example var arr1 = ; how can I count how many 1s, 2s, 3s, etc.? Despite the example, I never actually know what...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
1
by: john sutor | last post by:
If you are working with a 2 dimensional array with 3 elements per record (0,0) (0,1) (0,2) How do you get the correct count of records in the Array? The count method give you back a count of...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
4
by: fhmm | last post by:
I am an intermediate programmer and i tried to solve the following programme but with no success the programme is a simple text analyzer that read an input file and cumpute the occurence and...
5
by: aszia787 | last post by:
Hi guys, I need to count the unique elements in an array. For example, if i have an array, array. The number of unique elements in the array is 3. I am sorry if this message has been posted...
4
by: mk834tt | last post by:
When you use an object as an assc array as in: var aa = new Array(); aa = 2.50; aa = 3.50; .... Is there a way to determine the number of entries? So far I count this way: for (var x in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.