returning template container from function!!! how?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 7
Reputation: monogana is an unknown quantity at this point 
Solved Threads: 0
monogana monogana is offline Offline
Newbie Poster

returning template container from function!!! how?

 
0
  #1
Nov 3rd, 2008
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?

  1. template <class container, class iter, class valtype>
  2. iter findInContainer(container c, valType vt)
  3. {
  4. typedef typename container::iterator c_iter;
  5. c_iter iter = c.begin();
  6. while (iter != c.end())
  7. {
  8. if (*iter == vt)
  9. {
  10. return iter;
  11. } else
  12. {
  13. ++iter;
  14. }
  15. }
  16. return c.end();
  17. }
  18.  
  19. int main()
  20. {
  21. vector<int> intVec;
  22.  
  23. intVec.push_back(1);
  24. intVec.push_back(2);
  25. intVec.push_back(3);
  26.  
  27. findInContainer(intVec, 4);
  28. //should return intVec.end()
  29.  
  30. list<int> intlist;
  31.  
  32. intlist.push_back(1);
  33. intlist.push_back(2);
  34. intlist.push_back(3);
  35.  
  36.  
  37. findInContainer(intlist, 2);
  38. //should return iterator for intList[1]
  39.  
  40. return 1;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: returning template container from function!!! how?

 
0
  #2
Nov 3rd, 2008
I don't like this approach, but...
  1. template <typename C>
  2. typename C::iterator SlowFind(C& c, const typename C::value_type& v)
  3. {
  4. typename C::iterator i;
  5. for (i = c.begin(); i != c.end() && *i != v; ++i)
  6. ;
  7. return i;
  8. }
  9.  
  10. typedef std::vector<int> Vector;
  11. typedef std::list<int> List;
  12. using std::cout;
  13. using std::endl;
  14.  
  15. int main()
  16. {
  17. Vector iv;
  18. iv.push_back(1);
  19. iv.push_back(2);
  20. iv.push_back(3);
  21. Vector::iterator vi;
  22. vi = SlowFind<Vector>(iv,2);
  23. if (vi != iv.end())
  24. cout << *vi << endl;
  25.  
  26. List il;
  27. il.push_back(1);
  28. il.push_back(2);
  29. il.push_back(3);
  30. List::iterator li;
  31. li = SlowFind<List>(il,3);
  32. if (li != il.end())
  33. cout << *li << endl;
  34. return 0;
  35. }
Last edited by ArkM; Nov 3rd, 2008 at 2:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: returning template container from function!!! how?

 
0
  #3
Nov 4th, 2008
  1. template <typename C>
  2. typename C::iterator SlowFind(C& c, const typename C::value_type& v)
  3. {
  4. typename C::iterator i;
  5. for (i = c.begin(); i != c.end() && *i != v; ++i)
  6. ;
  7. return i;
  8. }
this is broken when C is a const container.
c.begin() and c.end() returns objects of type C::const_iterator.

in C++98 you would have to do something like
  1. template < typename CNTR > struct container_traits
  2. { typedef typename CNTR::iterator iterator ; } ;
  3.  
  4. template < typename CNTR > struct container_traits< const CNTR >
  5. { typedef typename CNTR::const_iterator iterator ; } ;
  6.  
  7. template < typename CNTR >
  8. typename container_traits<CNTR>::iterator SlowFind( CNTR& cntr,
  9. const typename CNTR::value_type& value )
  10. { return std::find( cntr.begin(), cntr.end(), value ) ; }

C++09 makes it a lot easier:
  1. template < typename CNTR >
  2. auto SlowFind( CNTR& cntr, const typename CNTR::value_type& value
  3. ) -> decltype( cntr.begin() )
  4. { return std::find( cntr.begin(), cntr.end(), value ) ; }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 7
Reputation: monogana is an unknown quantity at this point 
Solved Threads: 0
monogana monogana is offline Offline
Newbie Poster

Re: returning template container from function!!! how?

 
0
  #4
Nov 6th, 2008
thank you guys.....

thats great!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC