Q:

The library defines an algorithm named partition that takes predicate is true appear in the first part and those for which the predicate is false appear in the second part

0

The library defines an algorithm named partition that takes a predicate and partitions the container so that values for which the predicate is true appear in the first part and those for which the predicate is false appear in the second part. The algorithm returns an iterator just past the last element for which the predicate returned true. Write a function that takes a string and returns a bool indicating whether the string has five characters or more. Use that function to partition words. Print the elements that have five or more characters.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

bool longStr(const std::string &s) {
  return s.size() >= 5;
}

int main() {
  std::vector<std::string> words;
  for (std::string s; std::cin >> s; words.push_back(s)) {}
  auto long_bg = std::partition(words.begin(), words.end(), longStr);
  for (auto it = words.begin(); it != long_bg; ++it)
    std::cout << *it << " ";
  std::cout << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now