2011-12-26 4 views
0

メリークリスマスとハッピーニューイヤー!ベクトルコンテナの所定の間隔で検索するには

ベクトルコンテナの所定の間隔(ベクトル全体ではない)で検索して、ユーザー指定の整数が存在するかどうかを調べたいとします。しかし、私はそれを動作させることができませんでした。私に助けてもらえますか?どうもありがとうございました。

#include <iostream> 
#include <algorithm> 
#include <vector> 

using namespace std; 

int main() 
{ 
int myints[] = { 10, 20, 30 ,40 }; 
vector<int> myvector (myints,myints+4); 
vector<int>::iterator it, itLower, itUpper; 
itLower = myvector.begin(); 
itUpper = myvector.begin(); 
advance(itLower, 1); 
advance(itUpper, 2); 

// iterator to vector element: 
it = find (itLower, itUpper, 50); 

if (it != myvector.end()) 
{ 
    // Found 
    cout << "Found it"; 
} 
else 
{ 
    // Not fount 
    cout << "Not found it"; 
} 

return 0; 
} 
あなたはちょうどそれがあなたのケースの端にある最後の要素を(返すアイテムを見つけることができません

if (it != itUpper) ... 

もしfindによってライン

if (it != myvector.end()) ... 

を置き換えることが

答えて

0

あなた完全なベクトルではない)。範囲はitUpperで定義されています。

+0

ありがとうございました! itUpperによって指し示される最後の項目が検索条件を満たす場合私は何をすべきか? – GoldenLee

+0

@ GoldenLee私の答えで述べたように、最後の項目は決してあなたの範囲に含まれていません。ですから、もしそれを含めるには、それに応じてitUpperを定義しなければなりません。 – Howard

+0

OK。私はそれを前進させて、それ自身を含めます。ありがとうございました! – GoldenLee

関連する問題