I have a class that iterates through an STL container, begin() to end(). Very straight forward stuff. I was using a list originally, but the situation has changed to where now the type of STL container is up in the air.
The kind of iterating I'm doing is common to every STL container, which makes me think there has to be a way to write this class so that it doesn't care what STL container is used.
While I could make the class in question a template, allowing the user code to define whatever STL container they wish, this is not an appropriate solution. The class has a lot of responsibilities and it wouldn't be good to make it a template because of one small aspect of what it does.
So I'm struggling to come up with a different way. Any advice is appreciated. Thanks!
The user of a class shouldn't know or care about how the class functions under the hood. If you are exposing the STL container to the user, you're doing it wrong.
I would decide on one container and use it. If later it turns out it's the wrong decision, you can change the class to use a different container without changing the way the class is used (as long as you properly encapsulate)
Ok, you got me thinking, and I think I see what I need to do.
The data in the container is actually owned by the user. The class I'm writing currently takes nodes through a myclass.add(), does a push_back() onto a private stl list.
However as I progressed I realized my class does not have nearly enough information to properly process these nodes. I thought that instead of receiving individual nodes I should take a user defined container, so the user can implement whatever custom logic they need.
What I think I'll do is provide flags/parameters the user code can set that will tell my class what it needs to know to process the data. I can keep the container hidden and keep using myclass.add(). A drawback I'm worried about is that now I'll be troubled with trying to anticipate what the user is going to need. I think this will be fine though.