| | |
returning template container from function!!! how?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 7
Reputation:
Solved Threads: 0
hi all,
if you look at the code below you will see that i am trying to get a template function to return an iterator based on the type type of one of its parameters,
i cant seem to get it to work
i dont want to have to pass an iterator to the function i want to get it from the container that is passed
i am using dev-c++ to compile
can anyone please tell me if this is possible?
if you look at the code below you will see that i am trying to get a template function to return an iterator based on the type type of one of its parameters,
i cant seem to get it to work
i dont want to have to pass an iterator to the function i want to get it from the container that is passed
i am using dev-c++ to compile
can anyone please tell me if this is possible?
C++ Syntax (Toggle Plain Text)
template <class container, class iter, class valtype> iter findInContainer(container c, valType vt) { typedef typename container::iterator c_iter; c_iter iter = c.begin(); while (iter != c.end()) { if (*iter == vt) { return iter; } else { ++iter; } } return c.end(); } int main() { vector<int> intVec; intVec.push_back(1); intVec.push_back(2); intVec.push_back(3); findInContainer(intVec, 4); //should return intVec.end() list<int> intlist; intlist.push_back(1); intlist.push_back(2); intlist.push_back(3); findInContainer(intlist, 2); //should return iterator for intList[1] return 1; }
I don't like this approach, but...
C++ Syntax (Toggle Plain Text)
template <typename C> typename C::iterator SlowFind(C& c, const typename C::value_type& v) { typename C::iterator i; for (i = c.begin(); i != c.end() && *i != v; ++i) ; return i; } typedef std::vector<int> Vector; typedef std::list<int> List; using std::cout; using std::endl; int main() { Vector iv; iv.push_back(1); iv.push_back(2); iv.push_back(3); Vector::iterator vi; vi = SlowFind<Vector>(iv,2); if (vi != iv.end()) cout << *vi << endl; List il; il.push_back(1); il.push_back(2); il.push_back(3); List::iterator li; li = SlowFind<List>(il,3); if (li != il.end()) cout << *li << endl; return 0; }
Last edited by ArkM; Nov 3rd, 2008 at 2:39 pm.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
C++ Syntax (Toggle Plain Text)
template <typename C> typename C::iterator SlowFind(C& c, const typename C::value_type& v) { typename C::iterator i; for (i = c.begin(); i != c.end() && *i != v; ++i) ; return i; }
c.begin() and c.end() returns objects of type C::const_iterator.
in C++98 you would have to do something like
C++ Syntax (Toggle Plain Text)
template < typename CNTR > struct container_traits { typedef typename CNTR::iterator iterator ; } ; template < typename CNTR > struct container_traits< const CNTR > { typedef typename CNTR::const_iterator iterator ; } ; template < typename CNTR > typename container_traits<CNTR>::iterator SlowFind( CNTR& cntr, const typename CNTR::value_type& value ) { return std::find( cntr.begin(), cntr.end(), value ) ; }
C++09 makes it a lot easier:
C++ Syntax (Toggle Plain Text)
template < typename CNTR > auto SlowFind( CNTR& cntr, const typename CNTR::value_type& value ) -> decltype( cntr.begin() ) { return std::find( cntr.begin(), cntr.end(), value ) ; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Factors of a Polynomial
- Next Thread: graphic problem in C++
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






