-2
たとえば、要素が1,7,9,23,34,47,67,89,123,234,345,567の配列があります。 123の位置を知る必要があります。lower_bound(upper_bound)を使用して配列内の任意の数の位置を見つける方法は?
たとえば、要素が1,7,9,23,34,47,67,89,123,234,345,567の配列があります。 123の位置を知る必要があります。lower_bound(upper_bound)を使用して配列内の任意の数の位置を見つける方法は?
要素で配列を宣言して初期化します。次に、intのvector
を作成します。 vector
でupper_bound()
を使用してください。ここに例があります:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int arr[] = {1,7,9,23,34,47,67,89,123,234,345,567};
int len = sizeof(arr)/sizeof(arr[0]);
vector<int> v(arr,arr+len);
vector<int>::iterator upper;
upper = upper_bound(v.begin(), v.end(), 123);
cout<<(upper-v.begin())<<endl; // Output: 9
return 0;
}
希望します!
配列の 'std :: upper_bound'と' std :: lower_bound'を直接使用することができます。そのベクトルは必要ありません。 –
['std :: find'](http://en.cppreference.com/w/cpp/algorithm/find)うまく動作するはずです。もしそれが必要なら、 'std :: begin(array)'を減算してイテレータではなく(数値)インデックスを得ることができます。 – Xirema
何か試しましたか? – George