c++ program to read html code.

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
1
2
3
4
5
auto it = Elements.rbegin();
for(unsigned i = 0; i < 10; ++i) {
    cout << "Element: " << it->second << " : " << it->first << endl; 
    ++it;
}
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
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
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!
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
Yes it worked !! no I dont hv c++11 support !! thats why it was giving me error
thanks a lot you saved me!
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
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();
so if i change i to somthing else like j it will work fine ?
no it doesnt work !!
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 ?
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.
!!!
Last edited on
???
Last edited on
Pls help me this time i really need to get this thing done !!
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.
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.