I modified some code I found on Wikipedia concerning maps to see if it would work before applying it to a project I'm working on that has a similar idea.
What I would want is for a user to be able to enter sentences and then be able to distinguish how many times a the person entered a word in a particular sentence. This is the code I came up with:
It compiled just fine, but I got the following error message at runtime after entering my first string:
Quote:
Debug Assertion Failed!
Program: ...s\Visual Studio 2008\Projects\mapAttempt\Debug\mapAttempt.exe
File: c:\program files\microsoft visual studio 9.0\vc\include\vector
Line: 779
Expression: vector subscript out of range
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
I just learned about vectors today; I tried using an array to do this at first but that failed miserably... someone suggested to use a vector. Needless to say, I'm a bit clueless about this.
Here's some more information on what I'm actually trying to do just in case you think there might be a better way of doing this:
I need to count how many times each word appears in each sentence in a document (capitalization does not matter, punctuation is disregarded, and each sentence occupies its own line). Let's say my document contained the following:
The dog jumped
My friend ate
My friend jumped
I should, after running my program, have a matrix that looks something like:
As you can probably tell, the first column corresponds to the first sentence, the second column to the second sentence, etc.
I was told that the best way to go about this is a hash table (or, in the case of C++, a map). My instructor told me this would be a better option than an array because our documents are rather large and the matrix will be rather sparse.
A C++ map definitely isn't a hash table. A hash table is a map, but not all maps are hash tables... Hash tables have strong advantages but plenty of pitfalls. A C++ map is a more general purpose list-based thing.
That's beside the point here, though. You're checking for the string 'end', which your document doesn't have, so your loop will never stop; so you may be trying to add strings which weren't read to your map.
Why not ask cin when it's done instead?
Also, put debug statements in your code. Output lines to cerr so you can track what your program's doing.
Last edited by Corona688; 06-18-2010 at 02:19 PM..
Well, this is basically just trying out creating a map with a vector value. This code isn't supposed to read in a file... it is supposed to read in entered sentences until a user types in "end" (sentinel controlled). I'm trying to debug with a simpler piece of code before implementing for my actual task.
For this code, a user enters a sentence. The user presses enter. This sentence should store each word as a key in the map and an integer for how many times the key was in that sentence in the vector. Then the user enters another sentence, presses enter; if a word is repeated from the first sentence, it should find that key and add the number of times it was found in that sentence into the vector.... etc etc.... until the user enters "end". Then the program should print out the key and the corresponding vector:
Quote:
(Word_1) (# of occurrences in sentence_1) (# of occurrences in sentence_2)...(# of occurrences in sentence_n)
.....
(Word_n) (# of occurrences in sentence_1) (# of occurrences in sentence_2)...(# of occurrences in sentence_n)
Well, this is basically just trying out creating a map with a vector value. This code isn't supposed to read in a file...
The difference is nil as far as cin is concerned. It will work with both.
To run with keyboard:./myprogram
To run with file:./myprogram < file
Quote:
it is supposed to read in entered sentences until a user types in "end" (sentinel controlled). I'm trying to debug with a simpler piece of code before implementing for my actual task.
You're still checking for "end", though. You should check for EOF too.
Quote:
For this code, a user enters a sentence. The user presses enter. This sentence should store each word as a key in the map and an integer for how many times the key was in that sentence in the vector.
I see.
You're trying to use a vector like a map. A map adds the elements you need whenever you use [ ], a vector does not; if you haven't told the vector to resize itself to 20 elements or more, myvector[19] won't exist and will probably cause your program to crash.
To make your scheme work you're going to have to keep all your vectors the same size as you go along. That'll mean looping through your entire map resizing every vector for the next line, even if you're just adding a zero to 99/100 vectors -- not very efficient, and wastes most space just storing zeroes. This is probably what they wanted to avoid in using a sparse container.
Try a map of maps, not a map of vectors. Then you can just go words["slartibartfast"][line]=n and it'll do what you want. map<string, map<int, int> >
And when you print out the matrix, use words["asdf"].find(0) rather than words["asdf"][0], otherwise the inner map might allocate all the elements it didn't have yet when you try to use them.
Last edited by Corona688; 06-18-2010 at 06:24 PM..
Hello,
Trying to challenge myself with C++ STL.
I want to read in data from file and do some calculation, adapted from an exercise of a book (C++ for engineers and scientist, 3rd Ed, Gary Bronson).
infile is like:
ID Name Course Credit Grade
2333021 Bokow,R. NS201 3 A
2333021 Bokow,R.... (19 Replies)
In AWK
For 3 individual vectors of the form:
-2.772 -9.341 -2.857
-5.140 -6.597 -1.823
-2.730 -5.615 1.159
I would like to write a script that parses line by line to (i) normalise, (ii) divide by the norm for *each* vector.
I.e.
sqrt(-2.772^2 + -9.341^2 + -2.857^2)=10.154
... (4 Replies)
hello guys.
i'm new to c++. i've problem using two dimensional vector.
i've a project of making conway's game of life. this is the code that i have made so far.
my problem is how can i give a two dimensional vector through main.
glider.vec1 = vec; is not correct way to give a two... (2 Replies)
Hi all, from my understanding I understand that I can use array in this manner.
struct test
{
int a;
int b;
int c;
};
test testing; //creating an array with the structer type
testing.a=1;
testing.b=2;
testing.c=3;
If I'm not wrong we can use array in this manner,... (12 Replies)
Hi all,
I have a vector,the type of the element within it is list<int>,and i wanna sort this vector.So i implemented a function as a predicate for sort(the STL algorithm).Problem came when i missed the bold part in my code,g++ generated lots of error messages.And after i added the bold... (4 Replies)
Hi! I need to make dynamic multidimensional arrays using the vector class. I found in this page How to dynamically create a two dimensional array? - Microsoft: Visual C++ FAQ - Tek-Tips the way to do it in 2D, and now i'm trying to expand it to 3D but i don't understand how is the operator working,... (1 Reply)
Hi
i have the following structure
struct S
{
char Mod_num;
char val;
char chr_nm_cd;
}
I am reading a 2GB file and inserting into the structure and writing into a vector.
I feel like only vector will be a right option. I tried with multimap but it is memory intensive and hence i... (1 Reply)
Dear colleagues,
One of my friend have a problem with c code. While compiling a c program it displays a message like
"array type has incomplete element type". Any body can provide a solution for it.
Jaganadh.G (1 Reply)