943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 643
  • C++ RSS
Nov 3rd, 2008
0

returning template container from function!!! how?

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
monogana is offline Offline
12 posts
since Aug 2008
Nov 3rd, 2008
0

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

I don't like this approach, but...
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 4th, 2008
0

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

Quote ...
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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 ) ; }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 6th, 2008
0

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

thank you guys.....

thats great!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
monogana is offline Offline
12 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Factors of a Polynomial
Next Thread in C++ Forum Timeline: graphic problem in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC