得到最大的“绝对”向量中的整数 [英] get the max "absolute" integer in a vector

查看:48
本文介绍了得到最大的“绝对”向量中的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有人能为我提供一个使用std :: max_element()

(可能是谓词版本)返回最大值的示例;绝对"整数
向量中的
?非常感谢您的帮助。


Tony


ps。


//返回最大值向量中的整数

std :: vector< intm;

....

std :: vector< int> :: iterator p = std :: max_element(m.begin(),m.end());

解决方案

JDT写道:





有人能为我提供一个使用std :: max_element()

的例子(可能是谓词版本) )返回最大绝对值整数
向量中的
?非常感谢您的帮助。


Tony


ps。


//返回最大值向量中的整数

std :: vector< intm;

...

std :: vector< int> :: iterator p = std :: max_element(m.begin(),m.end());



这是传统方式。如果您愿意,您还可以尝试将ComparisonOp

制作成模板。


---------------- --------------------------------

#include< vector>

#include< algorithm>

#include< functional>

class ComparisonOp:public std :: binary_function< bool,int,int> ;

{

public:

result_type operator()(const first_argument_type& a,

const second_argument_type& ; b)const

{

返回(a< b);

}

};


int

main()

{

std :: vector< intm;

//用元素填充m

std :: vector< int> :: iterator p = std :: max_element(m.begin(),m.end(),

ComparisonOp());

}

----------------------- -----------------------------------


这使用了boost lambda。

---------------------------------------- ------------------

#include< vector>

#include< algorithm>

#include< boost / lambda / lambda.hpp>


//如果运算符<是为您的类型定义的(在您的情况下为int)


使用命名空间boost :: lambda;


int

main()

{

std :: vector< intm;

//用元素填充m

std :: vector< int> :: iterator p =

std :: max_element(m.begin(),m.end(),(_ 1< _2));

}


---------------------------------- ---------------------------


Piyo写道:
< blockquote class =post_quotes>
JDT写道:


>

有人能为我提供一个使用的示例std :: max_element()
(可能是谓词版本)返回最大绝对值整数
在向量中?非常感谢你的帮助。

Tony

ps。

//返回一个向量中的最大整数
std :: vector< ; intm;
...
std :: vector< int> :: iterator p = std :: max_element(m.begin(),m.end());



这是传统方式。如果您愿意,您还可以尝试将ComparisonOp

制作成模板。


---------------- --------------------------------

#include< vector>

#include< algorithm>

#include< functional>

class ComparisonOp:public std :: binary_function< bool,int,int> ;

{

public:

result_type operator()(const first_argument_type& a,

const second_argument_type& ; b)const

{

返回(a< b);

}

};


int

main()

{

std :: vector< intm;

//用元素填充m

std :: vector< int> :: iterator p = std :: max_element(m.begin(),m.end(),

ComparisonOp());

}

----------------------- ----------------------------------- <无线电通信/>

这使用boost lambda。

--------------------------- -------------------------------

#include< vector>

#include< algorithm>

#include< boost / lambda / lambda.hpp>


//如果运算符<是为您的类型定义的(在您的情况下为int)


使用命名空间boost :: lambda;


int

main()

{

std :: vector< intm;

//用元素填充m

std :: vector< int> :: iterator p =

std :: max_element(m.begin(),m.end(),(_ 1< _2));

}


---------------------------------- ---------------------------



哎呀我冒险把吸收功能/工厂如果浮动/双重

在那里但是你得到了,想法:)


HTH


2007年3月14日星期三00:56:16 GMT in comp.lang.c ++,JDT

< jd ******* @ yahoo.comwrote,


>

有人能为我提供一个使用std :: max_element()
(可能是谓词版本)返回的示例最大的绝对整数
在向量中?非常感谢你的帮助。

Tony

ps。

//返回一个向量中的最大整数
std :: vector< ; intm;
...
std :: vector< int> :: iterator p = std :: max_element(m.begin(),m.end());



我不认为我理解你的问题。你是否在询问如何从迭代器p到vector元素值获得




Hi,

Can someone provide me an example that uses std::max_element()
(probablly the predicate version) to return the max "absolute" integer
in a vector? Your help is much appreciated.

Tony

ps.

// return the max integer in a vector
std::vector<intm;
....
std::vector<int>::iterator p = std::max_element(m.begin(), m.end());

解决方案

JDT wrote:

Hi,

Can someone provide me an example that uses std::max_element()
(probablly the predicate version) to return the max "absolute" integer
in a vector? Your help is much appreciated.

Tony

ps.

// return the max integer in a vector
std::vector<intm;
...
std::vector<int>::iterator p = std::max_element(m.begin(), m.end());

This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_function<bool, int, int>
{
public:
result_type operator()( const first_argument_type &a,
const second_argument_type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(m.begin(), m.end(),
ComparisonOp());
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<intm;
// fill m with elements
std::vector<int>::iterator p =
std::max_element( m.begin(), m.end(), (_1 < _2) );
}

-------------------------------------------------------------


Piyo wrote:

JDT wrote:

>Hi,

Can someone provide me an example that uses std::max_element()
(probablly the predicate version) to return the max "absolute" integer
in a vector? Your help is much appreciated.

Tony

ps.

// return the max integer in a vector
std::vector<intm;
...
std::vector<int>::iterator p = std::max_element(m.begin(), m.end());


This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_function<bool, int, int>
{
public:
result_type operator()( const first_argument_type &a,
const second_argument_type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(m.begin(), m.end(),
ComparisonOp());
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<intm;
// fill m with elements
std::vector<int>::iterator p =
std::max_element( m.begin(), m.end(), (_1 < _2) );
}

-------------------------------------------------------------

Oops I fogot to put the abs function/fabs if float/double
in there but you get, the idea :)

HTH


On Wed, 14 Mar 2007 00:56:16 GMT in comp.lang.c++, JDT
<jd*******@yahoo.comwrote,

>Hi,

Can someone provide me an example that uses std::max_element()
(probablly the predicate version) to return the max "absolute" integer
in a vector? Your help is much appreciated.

Tony

ps.

// return the max integer in a vector
std::vector<intm;
...
std::vector<int>::iterator p = std::max_element(m.begin(), m.end());

I don''t think I understand your question. Are you asking how to get
from the iterator p to the vector element value?


这篇关于得到最大的“绝对”向量中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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