Shadowssong
[H]ard|Gawd
- Joined
- Sep 17, 2009
- Messages
- 1,969
So my assignment is to take from standard input (from shell "$proj1.x < test0") a stream of letters and numbers. I am supposed to sort these and keep track of the numbers, characters and strings. My professor suggested we use a map (I've never used it before, been doing as much research as possible) as it would be the most effecient.
So from what I've learned this is how I am going to read it into the map, the problem is that I do not know how to increment the counter for the map pair (my understanding is that the char/num/string is to be unique so I would put it as the left value, and use the right value as the count for that specific char/num/string). This is just a snippet from my code. Can someone tell me how I would go about incrementing the right hand value? I am pretty confused on this assignment.
Then in a while loop I peek for the next char, then onto this code:
The input file is:
AAA001aaA1AAa01aaa01bb
Any ideas? Am I implementing this wrong? I want to go talk to my professor but I don't think I will be able to go see him before it is due (classes and I have to go out of town). I am pretty much teaching myself the implementation of the map class, and my book is very little help (and I can't find any relevant examples online oddly enough)
Thanks so much !
-Greg
So from what I've learned this is how I am going to read it into the map, the problem is that I do not know how to increment the counter for the map pair (my understanding is that the char/num/string is to be unique so I would put it as the left value, and use the right value as the count for that specific char/num/string). This is just a snippet from my code. Can someone tell me how I would go about incrementing the right hand value? I am pretty confused on this assignment.
Code:
//Declarations
map<char, int> char_map;
map<char,int>::iterator char_iter;
char c;
Then in a while loop I peek for the next char, then onto this code:
Code:
if(isalpha(c)) //initial check for alpha
{
cin.get(c); //grab the first char
while(isalpha(c))
{
char_iter=char_map.find(c); //Assign value from search
if(iter != char_map.end()) //If the element exists
{
char_map[iter] += 1; //Modify the count of the element HOW???
}
else
{
char_map[c] = 1; //If it doesn't exist,create element with val 1
}
cin.get(c);
}
The input file is:
AAA001aaA1AAa01aaa01bb
Any ideas? Am I implementing this wrong? I want to go talk to my professor but I don't think I will be able to go see him before it is due (classes and I have to go out of town). I am pretty much teaching myself the implementation of the map class, and my book is very little help (and I can't find any relevant examples online oddly enough)
Thanks so much !
-Greg