開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
沒有
問題(Question):
一般來說要在vector找某筆資料可以使用find
vector<string> v;
v.push_back("50");
v.push_back("2991");
v.push_back("23");
v.push_back("9999");
vector<string>::iterator i = v.begin();
i = find(v.begin(), v.end(), "9999");
if (i != v.end ())
{
int nPosition = distance (v.begin(), i);
cout << "Value "<< *i;
cout << " found in the vector at position: " << nPosition << endl;
}
else
cout<<"not found"<<endl;
如果有找到就會回傳在vector所在位置,反之則說not found。
現在我有問題的地方在於要如何設定 "找尋範圍"
改成 i = find(v.begin()+2, v.end(), "2991"); 則會顯示not found
但如果是從 v.end()-1 就會變得怪怪的。
例子:
i = find(v.begin(), v.end()-2, "9999");
Output: Value 23 found in the vector at position: 2
預期結果: not found
請問要如何修改呢?
謝謝