determine type of container elements from container iterator? (template problem)

This is a discussion on determine type of container elements from container iterator? (template problem) within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to make a very basic selection sort alogirithm into a template function: Code: template<class _RI> void selectionSort(_RI ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    82

    determine type of container elements from container iterator? (template problem)

    Hi, I'm trying to make a very basic selection sort alogirithm into a template function:
    Code:
    template<class _RI>
    void selectionSort(_RI begin, _RI end) {
    
        T lowest; // stores lowest value that has been compared
    
        for(_RI sortedElems = begin; sortedElems != end - 1; sortedElems++)
        {
            lowest = *sortedElems; // lowest equals first unsorted element
            for(_RI iter = sortedElems + 1; iter != end; iter++)
            {
                if(lowest > *iter) // 
                {
                    swap(lowest, *iter);
                }
            }//end for iter
            *sortedElems = lowest;
        }//end for sortedElems
    
        return;
    }//end selectionSort()
    The obvious link time error is that the template has no way of determining what type "T lowest" should be. I know it should be the same type as (*begin), but is there any way to use that information to make a variable of the proper type? One quick workaround I'd though of was to have the caller pass the value of the first element, but if possible I'd like to follow the format of the algorithm library.

    Thank you

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    82
    just noticed that all STL containers define C::value_type. Seems like it might be important, but I can't figure out how it could be used.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >just noticed that all STL containers define C::value_type
    You could use value_type, but then you wouldn't be able to sort arrays because they don't have such a member. You're thinking about it the wrong way, _RI is the right type:
    Code:
    template<class _RI>
    void selectionSort(_RI begin, _RI end) {
        _RI lowest; // stores lowest value that has been compared
    
        for(_RI sortedElems = begin; sortedElems != end - 1; sortedElems++)
        {
            lowest = sortedElems; // lowest equals first unsorted element
            for(_RI iter = sortedElems + 1; iter != end; iter++)
            {
                if(*lowest > *iter) // 
                {
                    swap(*lowest, *iter);
                }
            }//end for iter
            *sortedElems = *lowest;
        }//end for sortedElems
    
        return;
    }//end selectionSort()
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    82
    Thank you prelude. My pointer logic isn't very good, so I had to run through that a few times with paper and pencil, but it makes perfect sense now.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so I had to run through that a few times with paper and pencil
    A pencil and paper is to programmers what duct tape is to everyone else.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 02:03 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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