comparing elements of one vector with another

So, as per my self-study of vectors, I was testing to see if I could manage to blacklist a set of words, then build a vector from user input and then 'bleep' out any words from the blacklist while outputting the contents of the vector. You may recognize this from 4.7 of Stroustrup's 'Programming: Principles and Practice Using C++'.

So, I want to modify this to allow the user to, prior to entering words into the vector 'words' to build the vector 'disliked' through the console input. Now, the code below works, but I can't seem to figure out (after looking extensively) how to compare 'words[i]' with each element of the 'disliked' vector without defining each of them. While each element of 'disliked' is predefined, this isn't a problem, but if I want to add the code to allow the user to push_back new strings into 'disliked', then it will become a problem.

I tried adding an additional: for(int x = 0; x < disliked.size(); ++x) after the initial for statement, but that merely led to repeating the output of most of the words (including some that should have been 'bleeped' out). Is there a simple way to replace the complex if statements containing each element of 'disliked' with something simple which compares words[i] against each element of 'disliked' from 0 up to disliked.size() and doesn't multiply my output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include "stdafx.h"
#include "../../std_lib_facilities.h"

int main()
{
	cout << "Please enter a series of words.\n";

	vector<string> words;
	for (string temp; cin >> temp; )
		words.push_back(temp);

	vector<string> disliked =
	{ "dog", "cat","monkey" };

	for (int i = 0; i < words.size(); ++i)
		if ((i == 0 || words[i - 1] != words[i]) &&
 ((words[i] == disliked[0]) || (words[i] == disliked[1]) || (words[i] == disliked[2])))
			cout << "BLEEP\n";
		else
			cout << words[i] << '\n';
	return 0;
}
Last edited on
It would be much easier to use a function for this search.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool badWord (const string& word, const vector<string> &dislikedWords)
{
  for (string s : dislikedWords)
  {
    if (word == s)
      return true;
  }
  return false;

   //another option
   //return find (dislikedWords.begin (), dislikedWords.end (), word) != dislikedWords.end ();
}

int main ()
{
  cout << "Please enter a series of words.\n";

  vector<string> words;
  for (string temp; cin >> temp;)
    words.push_back (temp);

  vector<string> disliked = { "dog", "cat", "monkey" };

  for (string& s : words)
    if (badWord (s, disliked))
      cout << "BLEEP\n";
    else
      cout << s << '\n';

  system ("pause");
  return 0;
}
Instead of your if() logic you could use std::find() instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main()
{
    vector<string> user_words{"this", "dog", "horse", "bear", "column", "word", "cat", "car", "bus", "truck"};

    vector<string> disliked{ "dog", "cat","monkey" };

    for(size_t i = 0; i < user_words.size(); ++i)
    {
        vector<string>::iterator it = find (disliked.begin(), disliked.end(), user_words[i]);
        if (it != disliked.end())
            cout << "Bleep\n";
        else
            cout << user_words[i] << '\n';
    }
    return 0;
}


Okay, then it is as I had presumed. There are simpler ways to accomplish this task, but they rely on operators and other things I haven't gotten to yet. Where I am up to hasn't dealt with iterators yet, nor have I seen anything like string& yet.

That being said, I'm seeing some helpful logical tidbits among both of your code that I will likely refer back to. Thank you both for your comments.
You can you use a simple for loop instead.
1
2
3
4
5
6
7
8
9
bool badWord (const string& word, const vector<string> &dislikedWords)
{
  for (size_t i = 0; i < dislikedWords.size(); ++i)
  {
    if (word == dislikedWords[i])
      return true;
  }
  return false;
}
Topic archived. No new replies allowed.