Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a partition is empty in c++

Tags:

c++

stl

I am using the std::partition to split a vector in two sub vectors. I want to know if the first partition is empty

#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';
}

How to check if the non zeros partition contains elements or not?

like image 263
malefstro Avatar asked Jan 22 '26 05:01

malefstro


1 Answers

  • bound == v.begin() when first partition is empty.
  • bound == v.end() when second partition is empty.
like image 109
Jarod42 Avatar answered Jan 24 '26 21:01

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!