Need feedback on an idea for a program

I decided to create a simple HTML analyzer. By this I mean that it will check that all tags are correctly opened and closed in order e.g. <i><b>hello</i></b> would come back as an error.

So obviously the program will read in from a file into a string object, and then I will search through that string.

Now at first I thought search through and place any tags I found using a stack of strings. Then I realised that might not be the best idea. But what about a vector of strings? I could have an intial element to act as a center point, then add every opening and closing tag either side of the center respectively and if 'n' places away from the center either side do not match, then the tags are not correctly alligned.

What does anyone think? Or otherwise propose?
Last edited on
I would do it like you originally planned, using a stack to store which tags have been opened, and if you find a closing tag that isn't wanted, you just throw and exception or return an error or whatever.
stack or linked list. Don't use a vector.

vector is only good because it assures data will be contiguous which makes for speedy random access. You don't need random access for this because you're only ever interested in the topmost element of the stack. Therefore list is much better suited.
closed account (1yR4jE8b)
Well, considering the vector class has push and pop member functions...I would argue that a vector could be used just fine, and be much quicker to implement than writing your own stack or list.
vector could be used, but would have worse performance.

The thing with resizing vectors is that if you resize enough, the entire array has to be copied to a new location in order to keep it contiguous.

list and stack (both part of STL, so you don't have to write your own) also have push and pop member functions.

Last edited on
closed account (1yR4jE8b)
Oh, my STL is extremely rusty because I've always been forced to write my own data structures...I had no idea there were lists and stacks in the stl...definitely go with either of those. and thanks for teaching me something new, I'll definitely look into learning more of the STL.
The general answer seems to be to use a stack so I'll give that a go. Thank you Firedraco, Disch, and darkestfright.
Topic archived. No new replies allowed.