are the functions below correct? can you improve upon it? std::set<T>…
- User
are the functions below correct? can you improve upon it? std::set setIntersection(std::set& s1, std::set& s2) { typename std::set::iterator itFirst = s1.begin(); std::set toRet;
while (itFirst != s1.end()) { if (s2.find(*itFirst) != s2.end()) { toRet.insert(*itFirst); } itFirst++; } return toRet; }
template std::set setUnion(std::set& s1, std::set& s2) { std::set toRet;
typename std::set::iterator itFirst = s1.begin();
while (itFirst != s1.end()) { toRet.insert(*itFirst); ++itFirst; }
typename std::set::iterator itSec = s2.begin();
while (itSec != s2.end()) { toRet.insert(*itSec); ++itSec; }
return toRet; }
- Assistant
The
setIntersectionandsetUnionfunctions you provided are mostly correct, but there are a few aspects that can be improved upon:-
Template Declaration for
setIntersection: ThesetIntersectionfunction is missing a template declaration, which will lead to a compilation error. You should add at the beginning.
-
- User
explain how the 2 functions i gave you work
- Assistant
- User
{ set retval; stringstream ss(convToLower(rawWords)); string word;
while (ss >> word){ if (word.length() >= 2){ string subword; for (size_t i=0; i < word.size(); i++){ if (ispunct(word[i])){ if (subword.size() >= 2){ subword = convToLower(subword); retval.insert(trim(subword)); } subword.clear(); } else{ subword+= word[i]; } } if (subword.size() >= 2){ subword = convToLower(subword); retval.insert(trim(subword)); } } } return retval;}
is there anything wrong with this
- Assistant
- User
this is what i want my function to do, are they technically the same? std::setstd::string parseStringToWords(string rawWords) { std::set hitWords;
std::stringstream ss;
ss << rawWords; string tmp;
while(ss >> tmp) { int count = 0; string toRet;
for (unsigned int i = 0; i < tmp.size(); ++i) { if (ispunct(tmp[i])) { if (count >= 2) { toRet = convToLower(toRet); hitWords.insert(trim(toRet)); } count = 0; toRet.clear(); } else { ++count; toRet += tmp[i]; } } if (count >= 2) { toRet = convToLower(toRet); hitWords.insert(trim(toRet)); }}
return hitWords;
- Assistant
- User
so my functions do the same as this above?
- Assistant
Yes, both your original function and the improved function I provided aim to accomplish the same task. Here is a breakdown to highlight the similarities:
Common Objectives:
-
Tokenization of Input: Both parse the input string into smaller tokens (words) using whitespace as the delimiter, facilitated by a
std::stringstream. -
: Both functions treat punctuation as word boundaries. When encountering a punctuation mark within any given word, it ends the current subword and checks its length before processing.
-
- User
is this a correct implementation utlitizing the functions above #include "book.h" using namespace std;
Book::Book(const std::string category, const std::string name, double price, int qty, const std::string isbn, const std::string author) : Product(category, name, price, qty) , isbn_(isbn), author_(author){}
Book::~Book(){}
setstd::string Book::keywords() const { //find keywords in title and author name set author = parseStringToWords(author_); set name = parseStringToWords(name_);
//use utility func to merge 2 sets above then add isbn set<string> s_key = setUnion(author, name); s_key.insert(isbn_); return s_key;}
string Book::displayString() const { string retval; retval += name_ + " \n" + "Author: " + author_ + " " + "ISBN: " + isbn_ + "\n" + to_string(price_) + " " + to_string(qty_) + " left."; return retval; }
void Book::dump(std::ostream& os) const { os << category_ << endl; os << name_ << endl; os << price_ << endl; os << qty_ << endl; os << isbn_ << endl; os << author_ << endl; }
- Assistant