'Find random word in c++ string

How would I pick a random word in a C++ string? Also is there a better way to handle importing the words? Just started learning C++ this week.

Current Code:


#include <iostream>
#include <fstream>
#include <string>

class WordList {
private:
  std::ifstream m_file;
  std::string m_wl;
public:
  WordList(const char* f) {
    m_file.open(f);

    std::string str;
    while (std::getline(m_file, str)) {
      m_wl += str;
      m_wl.push_back('\n');
    }
  }

  bool isWord(std::string s) {
    if (m_wl.find(s) != std::string::npos) {
      return true;
    } else {
      return false;
    }
  }

};

int main() {
  std::cout << "Generating" << std::endl;

  WordList list("sgb-words.txt");
  std::cout << list.isWord("hello") << std::endl;
}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source