判斷[_First,_Last)區間内的元素是否被分割,也就是所有符合_Pred()的元素都被置于所有不符合的元素之前
傳回[_First,_Last)區間内中的第一個元素的位置,使用_Pred(elem1,elem2),在elem1小于elem2時傳回true.
複雜度:is_partitioned():線性
partition_point():如果收到的是随機通路疊代器則為對數,否則為線性
使用方法:
template<typename T>
inline void PRINT_ELEMENTS(const T & coll, const string& optcstr = "")
{
cout << optcstr;
for (auto elem : coll)
{
cout << elem << ' ';
}
cout << endl;
}
int main()
{
vector<int> a{ 5,3,9,1,3,4,8,2,6 };
PRINT_ELEMENTS(a, "a: ");
auto isodd = [](int elem) {return elem % 2 == 1; };
if (is_partitioned(a.cbegin(), a.cend(), isodd))
{
cout << "a is partitioned" << endl;
auto pos = partition_point(a.cbegin(), a.cend(), isodd);
cout << "first even element: " << *pos << endl;
}
else
{
cout << "a is not partitioned" << endl;
}
}