snippetcppMinor
How to write a method that takes a general iterator as input argument?
Viewed 0 times
iteratorargumentmethodwritegeneralinputthathowtakes
Problem
Now I have a bunch of these methods ( you can tell how ugly it looks like):
Is there anyway I can aggregate these two functions into one single function?
void ConvertUtility::ConvertEdgeIndexList(const list &edgeIndex)
{
list::const_iterator sit;
for(sit=edgeIndex.begin(); sit!=edgeIndex.end(); sit++)
{
}
}
void ConvertUtility::ConvertEdgeIndexList(const vector &edgeIndex)
{
vector::const_iterator sit;
for(sit=edgeIndex.begin(); sit!=edgeIndex.end(); sit++)
{
}
}Is there anyway I can aggregate these two functions into one single function?
Solution
How about:
PS. Stackoverflow may be a better site for this type of question.
template
void ConvertUtility::ConvertEdgeIndexList(C const& edgeIndex)
{
typename C::const_iterator sit;
for(sit=edgeIndex.begin(); sit!=edgeIndex.end(); ++sit)
{ // ^^ Prefer pre-increment on iterators.
}
}PS. Stackoverflow may be a better site for this type of question.
Code Snippets
template<typename C>
void ConvertUtility::ConvertEdgeIndexList(C const& edgeIndex)
{
typename C::const_iterator sit;
for(sit=edgeIndex.begin(); sit!=edgeIndex.end(); ++sit)
{ // ^^ Prefer pre-increment on iterators.
}
}Context
StackExchange Code Review Q#5129, answer score: 9
Revisions (0)
No revisions yet.