找出最大值与集合中每个成员之间的差异 [英] finding the difference between the highest value and each member in the set

查看:77
本文介绍了找出最大值与集合中每个成员之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
谁能指导我如何找到最大值与nb_tuple集中的每个成员之间的差.

Hello
Can anyone guide me how to find the difference between the highest value and each member in the set of nb_tuple.

nb_tuple* max = NULL;
	int max_r = 0;
	for (set<int>::iterator it = rs.begin(); it != rs.end(); it++) {
		int r = *it;
		if (r > 0) {
	for (std::vector<nb_tuple*>::iterator it2 = reachability[r].begin();
		it2 != reachability[r].end();it2++) {
                nb_tuple* x = *it2;
		if (max == NULL || x->power() > max->power()) {
						max = x;
						max_r = r;
					}
                                         ...
			}
		}



非常感谢
Pare



Many thanks
Pare

推荐答案

我不确定您是否正确地理解了您,但是找到最大值以及最大值与所有数字之间的差的一种方法是...

创建变量"max"并分配零
循环确定最大值
评估实际数字是否大于max
如果是:将实际值分配给max
如果不是:请检查下一个号码
可选:如果需要,也可以保存最大值的位置


一旦获得最大值,就进行另一个循环
您只需减去两个值即可得到每个位置的差异
(如果您希望它不带符号,请使用绝对值)
差异可以保存在另一个集合或数组中,也可以保存在其他任何内容中

如果这不是您想要的...,请使用消息下方的改进问题",并对其进行更好的解释.

添加:以简单的方式进行.

I am not sure to understand you correctly but a way to find the max and the difference between max and all numbers would be...

Create a variable "max" and assign zero
do a loop to determine the highest value
evaluate if actual number is bigger than max
if yes: assign the actual value to max
if not: check the next number
Optional: you can save the position of the max as well if you want/need to


Once you have the max value you make another loop
you get the diference of each position just substracting both values
(if you want it without sign, then use the absolute value)
the difference can be saved in another set or array or whatever you need

If this is not what you are looking for... then please use the "improve question" right side below your message and explain it better.

Addition: Doing it on the easy way.

int max = 0;
int i;

for (i = 0; i < original_set_length; i++)
{
   if (original_set[i] > max)
   {
      max = original_set[i];
   }
}

for (i = 0; i < original_set_length; i++)
{
   differences_set[i] = max - original_set[i];
}


如果Nelek对问题含义的假设是正确的,那么有一种懒惰的编码器方法可以执行他的算法:-

-使用std :: max_element找出集合中的最大值是什么
-使用std :: transform找出max_value-每个元素是什么,然后将它们复制到新集合或某个地方的流中

关于减法,您唯一需要编写的代码就是函子.

这里有一个道理-每次您开始编写特定循环时,都要对标准库算法进行快速扫描,看看是否有适合您尝试执行的操作.

干杯,

Ash
If Nelek''s assumption about what the question means is true then there''s a lazy coder way of performing his algorithm:-

- use std::max_element to work out what the maximum value in the set is
- use std::transform to work out what the max_value - each element is and copy them into a new collection or to a stream somewhere

About the only code you have to write is the functor for doing the subtraction.

There''s a moral here - every time you start writing a specific loop have a quick scan over the standard library algorithms and see if there''s any that fit what you''re trying to do.

Cheers,

Ash


感谢您的所有评论.您理解我想找到的正确内容.抱歉,我的解释不太清楚.
thank you for your all comments.You understand correct what I want to find.Sorry for my explanation that not quite clear.


这篇关于找出最大值与集合中每个成员之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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