Q:

Reimplement the program that eliminated duplicate words that we wrote in § 10.2.3 (p. 383) to use a list instead of a vector

0

Reimplement the program that eliminated duplicate words that we wrote in § 10.2.3 (p. 383) to use a list instead of a vector.

All Answers

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

#include <list>
#include <string>
#include <iostream>
#include <algorithm>

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

void elimDups(std::list<std::string> &words) {
  print(std::cout, words) << words.size() << std::endl;
  words.sort();
  print(std::cout, words) << words.size() << std::endl;
  words.unique();
  print(std::cout, words) << words.size() << std::endl;
}

int main() {
  std::list<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