Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template help needed

Template help needed

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Messer
    wrote on last edited by
    #1

    I do this over and over and I can't figure out how to make this function into a template.

    void CleanUpVector()
    {
        std::vector::iterator iter;
     
        for (iter = vMusic.begin(); iter != vMusic.end(); ++iter)
        {
    	delete *iter;		
        }
        vMusic.clear();
    }
    
    
    struct TAG 
    {
        string TrackNumber;
        string Title;
        string Artist;
        string Genre;
        string Year;
        string Comment;
        string Album;
        string PersistsAs;
    };
    
    struct Music_Item
    {
        HSTREAM Stream;
        TAG     Tag;
    };
    

    Any ideas anyone? I can't figure this out.

    A 1 Reply Last reply
    0
    • S Steve Messer

      I do this over and over and I can't figure out how to make this function into a template.

      void CleanUpVector()
      {
          std::vector::iterator iter;
       
          for (iter = vMusic.begin(); iter != vMusic.end(); ++iter)
          {
      	delete *iter;		
          }
          vMusic.clear();
      }
      
      
      struct TAG 
      {
          string TrackNumber;
          string Title;
          string Artist;
          string Genre;
          string Year;
          string Comment;
          string Album;
          string PersistsAs;
      };
      
      struct Music_Item
      {
          HSTREAM Stream;
          TAG     Tag;
      };
      

      Any ideas anyone? I can't figure this out.

      A Offline
      A Offline
      Andrew Walker
      wrote on last edited by
      #2

      You really don't want to be doing this - it's asking for trouble. You'd be much better using something like boost::shared_ptr in the vector rather than raw pointers - then, if required you can pass custom deleters into the function. Below is an example of a template function which does what you requested. Depending on usage the call to clear() may be redundant, or may not do what you expected.template<class T> void cleaner(std::vector<T>& v) { typedef typename std::vector<T>::iterator iter; iter cur(v.begin()); iter end(v.end()); for(; cur != end; ++cur) { delete (*cur); } v.clear(); }


      EDIT: fixed typo

      S 1 Reply Last reply
      0
      • A Andrew Walker

        You really don't want to be doing this - it's asking for trouble. You'd be much better using something like boost::shared_ptr in the vector rather than raw pointers - then, if required you can pass custom deleters into the function. Below is an example of a template function which does what you requested. Depending on usage the call to clear() may be redundant, or may not do what you expected.template<class T> void cleaner(std::vector<T>& v) { typedef typename std::vector<T>::iterator iter; iter cur(v.begin()); iter end(v.end()); for(; cur != end; ++cur) { delete (*cur); } v.clear(); }


        EDIT: fixed typo

        S Offline
        S Offline
        Steve Messer
        wrote on last edited by
        #3

        Thanks that worked great. This is the part I couldn't figure out. typedef typename std::vector::iterator iter; iter cur(v.begin()); iter end(v.end());

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups