Many Values to One Index

This is a discussion on Many Values to One Index within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to take all my SDL_Surface values and put them into one value with an index for every ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    33

    Question Many Values to One Index

    Hi,

    I want to take all my SDL_Surface values and put them into one value with an index for every SDL_Surface I have, so that I can put them through a for loop.

    This might be impossible, but I'm not too experienced with C++.


    If it matters, what I'm trying to do is put all my SDL_Surface values through:

    Code:
    SDL_FreeSurface( Surface );
    So if I'm correct in what I want to do, it'd look like this:

    Code:
    //Put all my Surfaces into tSurface[iTotalSurfaces]
    //TotalSurfaces will, obviously, be an integer representing the total number of surfaces I have
    
    for( int i=0, i<iTotalSurfaces,i++ )
    {
    SDL_FreeSurface( tSurface[i] );
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The best way would probably to put them into a map (assuming SDL_Surface is some kind of pointer type, if it's a complex structure you're better off using a pointer to it):
    Code:
    std::map<std::string, SDL_Surface> SurfaceMap;
    
    SurfaceMap.insert(std::make_pair("SomeTexture.png", Surface));
    Then to iterate all surfaces:
    Code:
    std::map<std::string, SDL_Surface>::iterator i;
    
    i = SurfaceMap.begin();
    while(i != SurfaceMap.end())
    {
      DoSomethingWithSurface(*i);
      i++;
    }
    This, of course, assumes that each surafce has a filename associated with it. If this is not a case try using an std::set as uniqueness is guaranteed (no duplicate iterations as could be possible in an std::list/std::vector etc...).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    All the surfaces get initialized somewhere, so just pop them onto the array at that time.

    You could also use the STL vector instead of an array to save you the dynamic memory trouble

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    Quote Originally Posted by MadCow257
    All the surfaces get initialized somewhere, so just pop them onto the array at that time.

    You could also use the STL vector instead of an array to save you the dynamic memory trouble
    I remember hearing about that before a while back. I'm having a problem with it though.

    I'm running gentoo linux using kdevelop to write my app, and I don't think I have the vector include file. I get errors in the compile, and theres no vector in any include directory afaik.

    Is there a place I can download this file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-22-2008, 12:18 AM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Reiventing the wheel (again)
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2007, 03:26 AM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  5. index array
    By kurz7 in forum C Programming
    Replies: 9
    Last Post: 04-24-2003, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21