I am writing a program that will create a map of vectors with each key of the map referencing a vector of strings.
I am able to create the map but then when I try to access the contents - I am not able to...
The code I have written is pasted below. Within the function createMap, the map is correctly created. But outside of the while loop in the function processString, I get nothing. In fact, even if I have a generic bout statement after the while loop, it does not execute. Any advice?
Please wrap your code with code wrapper from the website and use tabs so that your code is read easily. I can't look at your code while it's a mess. Sorry!
Make sure that your map is passed correctly to the function. You say that the map is correctly created, how do you know that? simply cout the result of each loop step, and check what happens. I can't really follow in your program, because there are many details missing. So I advise you to do some real debugging, which means, either use a debugger, which shows you the data values at each breakpoint you set, or simple use a cout to see how the variables update with each step.
Then, you can post a problem of something you can't code. I'm sorry, but it's really so complicated to check such a code this way. Just follow the steps I mentioned, and I promise it's gonna work.
//
void createMap(string &s, string &st, map<string, string> &m)
{
m[s]= st;
}
void processString(string &line, map<string, string > & m, int k, Set<string> &keys) //Set is used in the wrong way
{
int i=0;
string s, st;
while (cond) //What is this cond
{
if(i > line.length()) break;
else
{
s = line.substr(i,k);
st = line.substr(i+k,k); //Not able to understand this .
createMap(s,st,m);
keys.add(s); //Set is using in the wrong way
i++;
}
}
s= " the ";
cout << "Size of vector for the string " << s << "is = " << m[s].size() << endl;
}
The comments indicate that this isn't your code... is this your code?
while(cond) means that the loop will keep running while cond equals 1 or true (cond has a boolean value).
line.substr(i+k,k): well, remember that line is a string, and if you check a reference manual for string, you'll find that the method substr takes a substring over the specified range.
about Set, it apparently is a special type of set containers, not the standard one.
I apologize for sending a code that I was trying to find workarounds for...
Yes, this is my code. The code requires the user to input a string or a file from which a string is read. It also asks the user to specify an order (integer k). Using the given string and the order, the code will identify a sub-string of length k (as the key) and record in a vector of strings (that are sub-strings of length k) that immediately follow the sub-string (or the key). Therefore, I chose to use a map of a vector of strings. The objective of the code is to ultimately generate random text based on the patterns that it observes in the user provided text. I stopped when I was having trouble with the map of vectors.
For example, the first sentence in my reply here would identify "in" as a 2-character string as the key and then store sub-strings (g space), (g space) and (d space) as 3 substrings that follow the key "in" in the string. A vector of strings with two occurrences of (g space) and one occurrence of (d space) would be added to the map.
I could not use a set because the set would only store unique substrings.
I am posting the code in its entirety, if that will help you advice me on the problem that I am facing. The problem is that when I debug the program, the create map function does correctly extract the key and the sub-strings that immediately follow it. I get the correct number of occurrences - I used size to identify the number of sub-strings in the vector for a given key. When I return to the process string function, I cannot access my map. I used a cout call to the map but the program does not seem to execute it after the while loop in the "processString" function. That is why I put in a generic cout function to show that the program does not execute that step.
I figured out the problem. I cannot go all the way to end of the string in the function processString - because when it reaches the end of the input line it does not have any substrings to map to the identified key. The map function therefore exits abnormally. That is why I was not executing anything after the while loop because of an abnormal end - even though the program does not report any messages to the screen. It just terminates.
Anyway, I have the solution or at least it looks like I have the vector of strings mapped to their key.