我使用std::分区将向量拆分为两个子向量。我想知道第一个分区是否为空。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<uint32_t> v {0,1,2,3,4,0,0,0};
auto bound = std::stable_partition(v.begin(), v.end(), [](auto element)
{
return element > 0;
});
// print out content:
std::cout << "non zeros";
for (auto it=v.begin(); it!=bound; ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "zeros";
for (auto it=bound; it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
如何检查非零分区是否包含元素?
发布于 2020-11-12 09:39:59
bound == v.begin()
当第一个分区为empty.bound == v.end()
时,第二个分区为空。发布于 2020-11-12 09:49:10
此外,您可以知道每个分区的大小。
std::distance(v.begin(), bound)
告诉第一个分区的大小
std::distance(bound, v.end())
告诉第二个分区的大小。
https://stackoverflow.com/questions/64801353
复制相似问题