std::find
の使い方
サンプルコード
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v = { 3, 1, 4, 1, 5, 9, 2 };
std::vector<int>::iterator p = std::find(v.begin(), v.end(), 2);
if(p == v.end()){
std::cout << "2: not found" << std::endl;
}
else{
std::cout << "v[" << static_cast<int>(p - v.begin()) << "] = 2" << std::endl;
}
p = std::find(v.begin(), v.end(), 8);
if(p == v.end()){
std::cout << "8: not found" << std::endl;
}
else{
std::cout << "v[" << static_cast<int>(p - v.begin()) << "] = 8" << std::endl;
}
p = std::find_if(v.begin(), v.end(), [](int a){ return a%2==0; });
if(p == v.end()){
std::cout << "even: not found" << std::endl;
}
else{
std::cout << "v[" << static_cast<int>(p - v.begin()) << "] = " << *p << std::endl;
}
return 0;
}
実行結果
v[6] = 2
8: not found
v[2] = 4