How do i loop through a c++ map<string, vector<string>> until i reach a certain string vector value?

Hello, everyone. I've been working on this project for the last 2 weeks, and i'm very close to finishing, but i can't figure out the last part. Essentially the program is a markov chain sentence generator using std::map<std::string,std::vector<std::string>>. I'm currently trying to work on the random sentence generator function, but for the life of me, I cannot get it to work.

For the random sentence generator function, we start off with the key "^", which marks the beginning of a sentence. This key has an associated string vector, from which we are supposed to randomly choose a string. That string then becomes the first word in the randomly generated sentence, and also becomes the new key. This new key has it's own associated string vector, and we again choose a random string. This random string becomes the second word in the randomly generated sentence. We keep doing this until we hit a "$" string value, which marks the end of a sentence. This is the part that i'm having trouble with, because i'm not sure whether to use a map iterator loop , or a while loop, etc.
I keep changing my code to try and see if different things work, but the basics of what i have is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 std::string BookBot:: generateSentence()
{
        std::string startString = "^";
        std::vector<std::string>tempor;
        std::string sentence;
        std::string pickedWord;
        std::map<std::string,std::vector<std::string>>::iterator iter2;

        for(iter2 = markov_chain.begin(); iter2!=markov_chain.end(); iter2++)
{
        tempor = markov_chain[startString];
        pickedWord = tempor[rand()%tempor.size()];
        sentence =pickedWord + " ";
        tempor = markov_chain[pickedWord];
        std::string pickedWord2 = tempor[rand()%tempor.size()];
        startString = pickedWord2;
        sentence = sentence + pickedWord2;
}
        return sentence;
}




Last edited on
Never ever ever do this:

 
        std::map<std::string,std::vector<std::string>>::iterator iter2;

Instead, use auto in the for like this:

 
for (auto it = markov.begin(); it != markov.end(); ++it)

However, I don't think iterating through the map is what you want. How about something like this:

1
2
3
4
5
6
7
8
9
10
11
std::string BookBot::generateSentence() {
    std::string sentence;    
    for (std::string word = "^"; ; ) {
        auto word_list = markov[word];
        word = word_list[rand() % word_list.size()];
        if (word == "$")
            break;
        sentence += word + " ";
    }
    return sentence
}

Last edited on
What is that for loop do? I've never seen that before
It's just an infinite loop, which you can write like this:

 
for (;;)

But I used the first position to declare word within the scope of the for.
If the middle position is empty, then it's considered to always be true.

(Note that I edited the code above to remove an extraneous "auto" on line 5.)
Last edited on
lemme try and see if that works
I'm beginning to think there's a mistake on the professors end. I keep getting the same different value then what they're expecting.so idk anymore
What do you mean by "the same different value"?
Did you call srand(time(0)) to seed the random number generator early in main?
They have built in tests for the programs they give us to work on. If your code is totally correct, it passes all the tests. And we're not using total randomization, only partial ( like srand(seed) ) so that everyone should get the same answers.
> only partial ( like srand(seed) ) so that everyone should get the same answers.
Given a program compiled with two different compilers, both calling srand(seed) for the same value of seed, there is no guarantee that both executables get the same sequence out of rand().

You're guaranteed to get the same sequence in your program.
But use another compiler, you might get a different, but still repeatable, sequence.
Last edited on
I think the professor is running them all on the same compiler
Topic archived. No new replies allowed.