如何使用STL对数组的元素(按3计数)进行微分? [英] How to minupulate the elements (counting by 3) of an array usingSTL?

查看:80
本文介绍了如何使用STL对数组的元素(按3计数)进行微分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个整数数组。我需要找到数组中第0,第3,第6

,第9,...元素的最大值。我想知道STL(标准模板

库)是否提供了一种只操作数组的一部分的方法(而不是

整个),例如在这种情况下,0号,3号,6号,9号......请指教。谢谢。


Tony

解决方案

Tony Young写道:

我有一个整数数组。我需要找到数组中第0,第3,第6,第9 ......元素的最大值。我想知道STL(标准模板库)是否提供了一种只操纵数组的一部分(而不是整个)的方法,例如, 0,1,3,6,9 ......在这种情况下。




不,它不是。滚动你自己。


V


Victor Bazarov< v。******** @ comacast.net> ;写道:

Tony Young写道:

我有一个整数数组。我需要找到数组中第0,第3,第6,第9 ......元素的最大值。我想知道STL(标准模板库)是否提供了一种只操纵数组的一部分(而不是整个)的方法,例如, 0th,3th,6th,9th,......在这种情况下。



不,它不是。滚动你自己。




std :: valarray和std :: slice怎么样?事实上,可能有更好的方式,但我不经常使用valarrays(有人吗?)。

#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

< br> <> blockquote> 2005-12-19,Marcus Kwok< ri ****** @ gehennom.net>写道:

Victor Bazarov< v。******** @ comacast.net>写道:

Tony Young写道:

我有一个整数数组。我需要找到数组中第0,第3,第6,第9 ......元素的最大值。我想知道STL(标准模板库)是否提供了一种方法来操纵数组的一部分(而不是整数),例如0th,3th,6th
,9th,......在这种情况下。



不,它不是。滚动你自己。



std :: valarray和std :: slice怎么样?事实上,可能有更好的方法,但我不经常使用valarrays(是否有人?)。




它'这是标准库中包含的内容之一,其中很难理解。 B.Stroustrup在

_TCPL_中给出了类似这样的内容,但它对我来说没有启发:为
计算优化的向量。嗯?


我的决定是,如果我需要它,我会知道他的意思,

所以我忽略了valarrays。


-

Neil Cerutti


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

解决方案

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


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


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


这篇关于如何使用STL对数组的元素(按3计数)进行微分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆