c++ program to read html code.

Mar 14, 2013 at 7:51am
I have to write a c++ program to read html code and do bunch of stuff. One thing that i have to do is to count the no of attributes and sort them in descenting out and print out only first 10 frequently used attributes.
I counted them using maps and sorted them using multimaps but now dnt knw how to print only 10 elements

for(std::map<string, int>::iterator it = Element.begin(); it != Element.end(); ++it)
Elements.insert(pair<int, string>(it->second, it->first));

for(std::multimap<int, string>::reverse_iterator it = Elements.rbegin(); it != Elements.rend(); ++it)
{
cout << "Element: " << it->second << " : " << it->first << endl;
}
This is how i did it
an any one please help me display only 10 elements and not all the elements
Mar 14, 2013 at 8:03am
1
2
3
4
5
auto it = Elements.rbegin();
for(unsigned i = 0; i < 10; ++i) {
    cout << "Element: " << it->second << " : " << it->first << endl; 
    ++it;
}
Mar 14, 2013 at 8:06am
So I just write this inside the for loop that i have ?
it gives me error on the first line
I am sorry can u please answer me :(
I have my assignment due in couple of hours and its like i am almost there but i am not
i would really really appreciate your help
Last edited on Mar 14, 2013 at 8:11am
Mar 14, 2013 at 8:23am
replace your second loop with that completely.

If it gives you an error, post it.

map already sortes its elements. On which doesn't do that is unordered_map
Last edited on Mar 14, 2013 at 8:25am
Mar 14, 2013 at 8:26am
if i do that it gives me error:

ISO C++ forbids declaration of `it' with no type
cannot convert`std::reverse_iterator<std::_Rb_tree_iterator<std::pair<const int, std::string> > >' to `int' in initialization

but i am sorting my maps in the second loop!
Mar 14, 2013 at 8:30am
1
2
3
4
for(std::multimap<int, string>::reverse_iterator it = Elements.rbegin(); it != Elements.rend(); ++it)
{
cout << "Element: " << it->second << " : " << it->first << endl;
}

That code isn't sorting anything. it just shows records from last to first.

Which compiler do you use? Does it have C++11 support? if not: replace auto with std::multimap<int, string>::reverse_iterator
Mar 14, 2013 at 8:39am
Yes it worked !! no I dont hv c++11 support !! thats why it was giving me error
thanks a lot you saved me!
Mar 14, 2013 at 8:47am
wait i also have to do the same thing for attributes and if i try the same code it gives me error again :(

It says redeclaration of `std::reverse_iterator<std::_Rb_tree_iterator<std::pair<const int, std::string> > > it'

Help needed again !!
Last edited on Mar 14, 2013 at 8:48am
Mar 14, 2013 at 8:58am
1
2
3
int i = 0;
//do somethig with i
int i = 0; //Will give you an error: i already declared. You just need to assign a value: i = 0; 

Your case is similar:
it = Elements.rbegin();
Mar 14, 2013 at 9:00am
so if i change i to somthing else like j it will work fine ?
Mar 14, 2013 at 9:03am
no it doesnt work !!
Mar 14, 2013 at 9:04am
for(std::multimap<int, string>::reverse_iterator it = Elements.rbegin(); it != Attributes.rend(); ++it)
{
i < 10; i++;
cout << "Attributes: " << it->second << " : " << it->first << endl;
}

Do u think this is right ?
Mar 14, 2013 at 9:14am
so if i change i to somthing else like j it will work fine ?
No. You just need to delete type declaration and leave only assigment.
Do u think this is right ?
No it's not.

Show full code. I assume you deleted type from first it declaration.

And you should really return to the basics. You cannot write good code if you make simple mistakes.
Mar 14, 2013 at 9:18am
!!!
Last edited on Mar 14, 2013 at 11:47am
Mar 14, 2013 at 9:18am
???
Last edited on Mar 14, 2013 at 11:47am
Mar 14, 2013 at 9:25am
Pls help me this time i really need to get this thing done !!
Mar 14, 2013 at 9:27am
1
2
3
4
5
it = Attributes.rbegin();
for(unsigned i = 0; i < 10; ++i) {
cout << "Attributes: " << it->second << " : " << it->first << endl;
++it;
}


As I said: you have problem with basic C++ understanding.
Mar 14, 2013 at 9:35am
Thank you so very much
Yes i knw this is not my strong side!!
i am doing to go back to my basics in my break

I also have to do one last thing if u can please help me with it

for any element name that did not have a matching ending name, print line containing the element name, colon and the count of the number missing ending elements names for that element.

This is my first and last time doing this please if u could help.
Topic archived. No new replies allowed.