[問題] vector在find函示中設定找尋範圍
開發平台(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
請問要如何修改呢?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.161.96.2
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1457537222.A.D04.html
推
03/09 23:28, , 1F
03/09 23:28, 1F
→
03/09 23:28, , 2F
03/09 23:28, 2F
→
03/09 23:28, , 3F
03/09 23:28, 3F
→
03/09 23:29, , 4F
03/09 23:29, 4F
→
03/09 23:35, , 5F
03/09 23:35, 5F
→
03/09 23:42, , 6F
03/09 23:42, 6F
→
03/09 23:43, , 7F
03/09 23:43, 7F
→
03/09 23:43, , 8F
03/09 23:43, 8F
→
03/09 23:44, , 9F
03/09 23:44, 9F
推
03/10 00:08, , 10F
03/10 00:08, 10F
推
03/10 00:10, , 11F
03/10 00:10, 11F
→
03/10 00:27, , 12F
03/10 00:27, 12F
→
03/10 00:28, , 13F
03/10 00:28, 13F
→
03/10 00:29, , 14F
03/10 00:29, 14F