问题描述
在这个程序中,他们希望我找到
a中子序列的最大值,给定序号(序列由数组表示)
使用模板和pointers.also我必须写一个主要,以便
实例化我班级的不同对象。在这个程序中我不想要
来查找总和!!!!请有人帮帮我吗?
发布你到目前为止所做的事情,我们可以提供帮助。
此致,
Peter Jansson
真的吗?哇。所以......你有一个阵列?让我们看看吧。
向我们展示阵列,根据需要填充它,然后将它们的元素打印到屏幕上。
不需要模板,不需要指针,不需要最大值。只需
输出数组。
让我们面对它,如果你不能这样做,你将如何操纵它的
内容以任何方式?
嗯?
标题< algorithmprovides一个通用函数:
模板< class RandomAccessIterator>
void nth_element(RandomAccessIterator first,
RandomAccessIterator nth,
RandomAccessIterator last);
使用std :: less,但你可以还提供了你自己的比较谓词:
模板< class RandomAccessIterator,类比较>
void nth_element(RandomAccessIterator first,
RandomAccessIterator nth,
RandomAccessIterator last,
比较comp);
所以,为了找到一个范围内的最小值[从,到],你会这样做:
nth_element(from,from,to);
minimum = * from;
你可以改变它来找到最大值。
警告:此算法重新排列序列。确保你允许这样做。
最好
Kai-Uwe Bux
in this program they want me to find the max value of a subsequence in
a given sequnce of numbers(the sequence is represented by an array) by
using templates and pointers.also i must write a main in order to
instantiate differnt objects of my class. in this program i don''t want
to find the sum!!!!. please can anyone help me?
Post what you have done so far and we may help.
Sincerely,
Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Really? Wow. So like... you have an array? Lets see it.
Show us the array, populate it as required and then print its elements
to the screen.
No templates needed, no pointers needed, no max value needed. Just
output the array.
Lets face it, if you can''t do that, how are you going to manipulate its
contents in any way?
hmm?
http://www.parashift.com/c++-faq-lite/how-to-post.html
The header <algorithmprovides a generic function:
template<class RandomAccessIterator>
void nth_element(RandomAccessIterator first,
RandomAccessIterator nth,
RandomAccessIterator last);
using std::less, but you can also supply a comparison predicate of your own:
template<class RandomAccessIterator, class Compare>
void nth_element(RandomAccessIterator first,
RandomAccessIterator nth,
RandomAccessIterator last,
Compare comp);
So, in order to find the minimum in a range [from,to), you would do:
nth_element( from, from, to );
minimum = *from;
You can vary this to find the maximum instead.
Warning: this algorithm rearranges the sequence. Make sure that you are
allowed to do that.
Best
Kai-Uwe Bux
这篇关于给定序列数中子序列的最大值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!