检查给定整数数组中给定位置的元素是否大于其两个邻居的函数

时间:2016-10-11 17:02:44

标签: c++ function higher-order-functions

大家下午好,

希望你做得好!

我是论坛的新人,也是C ++编程的新人 我刚开始学习c ++,并希望深入研究这些函数。 一直在寻找答案本身,但没有找到任何东西。 首先,我想知道我的语法是否正确。 在下面的问题中,我提供了我的问题,以便你能看出我是否朝着正确的方向前进。

intervals= {1:[1,2], 2:[2,3], 3:[4,5], 4:[6,8], 5:[6.5,7]}
overlapping = [ [i,j,x,y] for i,x in intervals.iteritems() for j,y in intervals.iteritems() if x is not y and x[1]>y[0] and x[0]<y[0] ]
for x in overlapping:
    print '{0} overlaps with {1} at {2} and {3}'.format(x[2],x[3],x[0],x[1])

另一件事是,我想在这里找到 - 无论如何,这个函数中的3个参数如何&#34;结果&#34;之后可以用于其他代码吗? 例如,我收到的那3个(&#34;当前&#34; Pos&#34;,/* Write a Function that checks if the element at given position in given array of integers is bigger than its two neighbors (when such exist). */ #include "stdafx.h" #include <iostream> using namespace std; void outcome(int givenIndex) { const int maxn = 10; int arr[maxn] = { 10, 9, 7, 41, 14, 20, 22, 55, 43, 10 }; int comparer = 0; int lowerPosition = 0; int lowerIndex = 0; bool check = true; int higherIndex = 0; int higherPosition = 0; int currentPos = 0; for (int i = 0; i < maxn; i++) { if (givenIndex == i) { comparer = arr[givenIndex - 1]; currentPos = i; lowerIndex = arr[givenIndex - 2]; higherIndex = arr[givenIndex]; lowerPosition = i - 1; higherPosition = i + 1; cout << "position: " << lowerPosition << " ,corresponds to element: " << lowerIndex << endl; cout << "position: " << currentPos << " ,corresponds to element: " << comparer << endl; cout << "position: " << higherPosition << " ,corresponds to element: " << higherIndex << endl; cout << endl; if (comparer>lowerIndex) { cout << "element at position " << lowerPosition << " is lower than element at position " << currentPos << endl; } else { cout << "element at position " << lowerPosition << " is bigger than element at position " << currentPos << endl; } if (comparer>higherIndex) { cout << "element at position " << higherPosition << " is lower than element at position " << currentPos << endl; } else { cout << "element at position " << higherPosition << " is higher than element at position " << currentPos << endl; } } } } int _tmain(int argc, _TCHAR* argv[]) { int givenIndex = 0; cout << "enter the elemet you would like to compare to its neighbours: " << endl; cin >> givenIndex; outcome(givenIndex); return 0; } lower Position)参数是否包含在另一个函数中?

非常感谢每一个答案! 感谢您的帮助和您的努力!

0 个答案:

没有答案