Q:

Write a program that stores the excluded words in a vector instead of in a set. What are the advantages to using a set

0

Write a program that stores the excluded words in a vector instead of in a set. What are the advantages to using a set?

All Answers

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

// By using a `set`, we can avoid removing duplicates by ourself.
#include <vector>
#include <string>
#include <iterator>
#include <iostream>
#include <fstream>
#include <algorithm>

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

int main() {
  std::string filename;
  std::cin >> filename;
  std::ifstream in(filename);
  if (!in.is_open()) {
    std::cerr << "Cannot open file: " << filename << std::endl;
    return -1;
  }
  std::istream_iterator<std::string> i_iter(in), eof;
  std::vector<std::string> words(i_iter, eof);
  for (const auto &w : words)
    std::cout << w << std::endl;
  std::cout << "\nAfter remove duplicates:\n";
  elimDups(words);
  for (const auto &w : words)
    std::cout << w << 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