Vector Looping

How can i loop through a vector of strings and check if a letter is in a word? this is what i tried so far but im getting an error at "==".

1
2
3
4
5
6
7
8
for (int v = 0; v <= word_list.size(); v++) {
			for (int j = 0; j < word_list.size(); j++) {
				if (word_list[v][j] == letter_guess) {

				}
			}
			
		}
Last edited on
Try
1
2
3
4
5
6
7
8
9
10
11
string word_list="abcd";
char  letter_guess='a';

for (int v = 0; v <= word_list.size(); v++) 
{
	if (word_list[v] == letter_guess) 
	{
	cout << word_list[v] << " Match found at " << v << endl;
	}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <vector>

int main()
{
    const std::vector<std::string> words { "All", "happy", "families", "are", "alike;",
              "each", "unhappy", "family", "is", "unhappy", "in", "its", "own", "way." } ;

    const char letter = 'y' ;

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( const std::string& str : words ) // for each string str in the vector
    {
        // http://en.cppreference.com/w/cpp/string/basic_string/find (4)
        if( str.find(letter) != std::string::npos ) // if str contains the letter
        {
            // do something. say, print out the string
            std::cout << str << '\n' ;
        }
    }
}
Topic archived. No new replies allowed.