-1
using namespace std;
vector<string> wordDiff(string s, string t)
{
    istringstream parse_s(s);
    vector<string> words_s(istream_iterator<string>(parse_s), {});

    istringstream parse_t(t);
    vector<string> words_t(istream_iterator<string>(parse_t), {});

    sort(words_s.begin(), words_s.end());
    sort(words_t.begin(), words_t.end());

    vector<string> funk;
    set_difference(words_s.begin(), words_s.end(), 
                        words_t.begin(), words_t.end(),
                        back_inserter(ret));
    return funk;
}

so far i am able to get the array of strings with words in s that are not i t with set_difference however i am unable to get the array in the order of s

2 Answers2

0

Simple solution is not to sort words_s (only words_t) and use std::remove_if:

  sort(words_t.begin(), words_t.end());

  auto it = std::remove_if( words_s.begin(), words_s.end(), [words_t]( const std::string &str ) {
      return std::find( words_t.begin(), words_t.end(), str ) != words_t.end() );
  } );
  words_s.erase( it, words_s.end() );

you may want consider to use std::unordered_set instead of sorted vector for words_t

Slava
  • 43,454
  • 1
  • 47
  • 90
0

The simplest solution is to put all excluded words into a std::unordered_set instead of a std::vector.

Then you can use the set to check each word in your word list if it has to be excluded or not. This way, you no longer need to sort. All you need is a copy_if and a lambda.

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33