Markov chain text generator: Difference between revisions

Content deleted Content added
→‎{{header|Rust}}: add 2018 edition requirement
Ans (talk | contribs)
→‎{{header|C++}}: unused TextPos variable and pos variable in createText() removed, using std::string::npos instead of -1 (size_t is never negative!), made keyLen, words and kl unsigned
Line 64: Line 64:
class markov {
class markov {
public:
public:
void create( std::string& file, int keyLen, int words ) {
void create( std::string& file, unsigned int keyLen, unsigned int words ) {
std::ifstream f( file.c_str(), std::ios_base::in );
std::ifstream f( file.c_str(), std::ios_base::in );
fileBuffer = std::string( ( std::istreambuf_iterator<char>( f ) ), std::istreambuf_iterator<char>() );
fileBuffer = std::string( ( std::istreambuf_iterator<char>( f ) ), std::istreambuf_iterator<char>() );
Line 93: Line 93:
std::cout << "\n";
std::cout << "\n";
}
}
void createDictionary( int kl ) {
void createDictionary( unsigned int kl ) {
std::string w1, key;
std::string w1, key;
size_t wc = 0, pos, textPos, next;
size_t wc = 0, pos, next;
next = fileBuffer.find_first_not_of( 32, 0 );
next = fileBuffer.find_first_not_of( 32, 0 );
if( next == -1 ) return;
if( next == std::string::npos ) return;
while( wc < kl ) {
while( wc < kl ) {
pos = fileBuffer.find_first_of( ' ', next );
pos = fileBuffer.find_first_of( ' ', next );
Line 146: Line 146:
tin arms and
tin arms and
</pre>
</pre>



=={{header|C#|C sharp}}==
=={{header|C#|C sharp}}==