#include <iostream>
#include <sstream>
#include <unordered_set>
#include <string>
// assumes that a word is any sequence of non-white-space characters
// return set of words read from the input stream
std::unordered_set<std::string> words_read_from( std::istream& stm )
{
std::unordered_set<std::string> words ;
std::string str ;
while( stm >> str ) words.insert(str) ;
return words ;
}
// assumes that a word is any sequence of non-white-space characters
// return set of words in phrase
std::unordered_set<std::string> words_in( const std::string& phrase )
{
std::istringstream stm(phrase) ; // create an input string stream to read from
return words_read_from(stm) ;
}
// return true if 'phrase' contains any of the words in the set 'words'
// note: case-sensitive
bool contains_word_in( const std::string& phrase, const std::unordered_set<std::string>& words )
{
for( const std::string& str : words_in(phrase) ) // for each unique word in 'phrase'
{
if( words.find(str) != words.end() ) // if it is present in the set 'words'
returntrue ;
}
returnfalse ;
}
int main()
{
std::istringstream txt1( "Dad has apples\n""Mom has pears\n""Grandma has grapes\n""Grandpa has nectarines\n""I have oranges\n" ) ;
std::istringstream txt2( "oranges\n""apples\n""pears\n" ) ;
// step 1: get the set of all words in txt2
constauto words_in_txt2 = words_read_from(txt2) ;
// step 2: for each line in txt1
std::string line ;
while( std::getline( txt1, line ) )
{
// step 2a. check if the line contains any word in the set of words in txt2
if( contains_word_in( line, words_in_txt2) ) // if it does
std::cout << line << '\n' ; // print the line
}
}
int main()
{
ifstream txt1;
ifstream txt2;
ofstream output;
txt1.open("marius.txt");
txt2.open("marius1.txt");
output.open("marius2.txt");
// step 1: get the set of all words in txt2
constauto words_in_txt2 = words_read_from(txt2);
// step 2: for each line in txt1
string line;
while (getline(txt1, line))
{
// step 2a. check if the line contains any word in the set of words in txt2
if (contains_word_in(line, words_in_txt2)) // if it does
output << line << endl;// print the line
}
output.close;
txt1.close;
txt2.close;
system("pause");
return 0;
}
1. Make a genuine attempt to understand the earlier version of the program.
2. Then, make a genuine attempt to modify it to get the results that you want.
3. After you have completed steps 1. and 2., you may want to have a look at this code.
You do learn a lot by studying code written by others (though teachers who believe that the primary goal of formal education is evaluation rather than acquisition of knowledge tend to react violently to this premise). But you will internalise that knowledge only when you apply it in your own code.
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <string>
// assumes that a word is any sequence of non-white-space characters
// return set of words read from the input stream
std::unordered_set<std::string> words_read_from(std::istream& stm)
{
std::unordered_set<std::string> words;
std::string str;
while (stm >> str) words.insert(str);
return words;
}
// return true if 'phrase' contains any substring in the set 'substrings'
// note: case-sensitive
bool contains_substring_in(const std::string& phrase, const std::unordered_set<std::string>& substrings)
{
for (const std::string& str : substrings ) // for each substring 'str' in 'substrings'
{
if ( phrase.find(str) != std::string::npos ) // if 'phrase' contains the substring
returntrue;
}
returnfalse;
}
int main()
{
std::istringstream txt1("Dadhasapples\n""Momhaspears\n""Grandmahasgrapes\n""Grandpahasnectarines\n""Ihaveoranges\n");
std::istringstream txt2("pear\ngrap\n" ) ;
// step 1: get the set of all words in txt2
constauto words_in_txt2 = words_read_from(txt2);
// step 2: for each line in txt1
std::string line;
while (std::getline(txt1, line))
{
// step 2a. check if the line contains any substring from the set of words in txt2
if ( contains_substring_in( line, words_in_txt2 ) ) // if it does
std::cout << line << '\n'; // print the line
}
}