Map of Vectors of strings

Hi,

I am new to this forum and to c++.

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?

Thanks,

========
Code
========

void createMap(string &s, string &st, Map<Vector<string> > & m)
{
m[s].add(st);

}

void processString(string &line, Map<Vector<string> > & m, int k)

int i=0;
string s, st;

while (cond)
{
if(i > line.length()) break;
else
{
s = line.substr(i,k);
st = line.substr(i+k,k);
createMap(s,st,m);
i++;
}
}
s= " the ";
cout << "Size of vector for the string " << s << "is = " << m[s].size() << endl;

}
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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

void createMap(string &s, string &st, Map<Vector<string> > & m)
{
	m[s].add(st);

}

void processString(string &line, Map<Vector<string> > & m, int k, Set<string> &keys)
{
	int i=0;
	string s, st;
	
	while (cond)
	{
		if(i > line.length()) break;
		else 
		{
			s = line.substr(i,k);
			st = line.substr(i+k,k);
			createMap(s,st,m);
			keys.add(s);
			i++;
		}
	}
	
	s= " the ";
	cout << "Size of vector for the string " << s << "is = " << m[s].size() << endl;
	
}
Hi TheDestroyer,

Were you able to review the code that I inserted with code wrapper and indents.

Please let me know what could potentially be going on - that I am not able to access the map that I am creating in createMap.

Thanks,
Hello,

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.

Regards.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
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;
	
}


See the comments and the modification
Last edited on
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.

Refer to the reference of those functions:

http://www.cplusplus.com/reference/stl/set/

http://www.cplusplus.com/reference/stl/map/

http://www.cplusplus.com/reference/string/string/

and read about the methods used and understand how they work.

Regards.
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.

Thank you,

======
Entire code
======

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "genlib.h"
#include <iostream>
#include <fstream>
#include "simpio.h"
#include "vector.h"
#include "map.h"


void createMap(string &s, string &st, Map<Vector<string> > & m)
{
	m[s].add(st);
	cout << s << " = " << st << " and size = " << m[s].size() << endl;

}

void processString(string &line, Map<Vector<string> > & m, int k)
{
	int i=0;
	string s, st;
	
	while (true)
	{
		if(i > line.length()) break;
		else 
		{
			s = line.substr(i,k);
			st = line.substr(i+k,k);
			createMap(s,st,m);
			i++;
		}
	}
	
	s= " the ";
	cout << "Size of vector for the string " << s << "is = " << m[s].size() << endl;
	
}

void ReadFile(ifstream &in, Map<Vector<string> > & m, int k)
{
	while (true) 
	{
		string line;
		getline(in, line);
		if(in.fail()) break;
		else 
		{
			processString(line, m, k);
		}
	}
	

}


int main() 
{
	Map<Vector<string> > m;
	ifstream in("sample1.txt");

	int k;
	cout << "Input the order for the random writing" << endl;
	
	k= GetInteger();
	
	ReadFile(in,m,k);
		
	return 0;
}
Hi,

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.

Thank you for all your inputs.
why are you not using to get the string.rigth() to get the right side of the string .
Topic archived. No new replies allowed.