Q:

Implement your own version of elimDups. Test your program by printing the vector after you read the input, after the call to unique, and after the call to erase

0

Implement your own version of elimDups. Test your program by printing the vector after you read the input, after the call to unique, and after the call to erase.

All Answers

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

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

std::ostream &print(std::ostream &os, const std::vector<std::string> &vs) {
  for (const auto &i : vs)
    os << i << " ";
  return os;
}

void elimDups(std::vector<std::string> &words) {
  print(std::cout, words) << words.size() << std::endl;
  std::sort(words.begin(), words.end());
  print(std::cout, words) << words.size() << std::endl;
  auto end_unique = std::unique(words.begin(), words.end());
  print(std::cout, words) << words.size() << std::endl;
  words.erase(end_unique, words.end());
  print(std::cout, words) << words.size() << std::endl;
}

int main() {
  std::vector<std::string> words;
  for (std::string s; std::cin >> s; words.push_back(s)) {}
  elimDups(words);

  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