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?

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...

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;
}
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;
}

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

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:

template < typename CNTR >
auto SlowFind( CNTR& cntr, const typename CNTR::value_type& value 
             ) -> decltype( cntr.begin() ) 
{   return std::find( cntr.begin(), cntr.end(), value )  ;  }

thank you guys.....

thats great!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.