+ Reply to Thread
Page 2 of 2
FirstFirst 1 2
Results 11 to 13 of 13

Thread: Templates and Sorting

  1. #11
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,780
    Blog Entries
    40

    Re: Templates and Sorting

    Actually, it does succeed in sorting, and this is perfectly fine code. Array's are nothing more than thinly veiled pointers, so while it is true the function is not technically passing by reference, that would be because it is passing by address instead.

    You can just as easily use this code:
    Code:
    template<typename T>
    void selectSort(T arr[], int n) {
    As this code:
    Code:
    template<typename T>
    void selectSort(T *arr, int n) {
    - Zeke

  2. #12
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    Re: Templates and Sorting

    Thanks for the info.

    So when you added the pointer operator before the array name, you were able to remove the array operator? Why? I tried it out but I don't quite see why that still works.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  3. #13
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,780
    Blog Entries
    40

    Re: Templates and Sorting

    Arrays are pointers, that's why. When arrays are declared, they return a pointer to a block sequence of values that are right next to each other, and when you use the array operator ([]), you're essentially asking to dereference the pointer value plus whatever value is inside of the array operator brackets. An example might be better... essentially using the array operator like this:
    Code:
    int t = myArray[5];
    Is the same as using this:
    Code:
    int t = *(myArray + 5);
    However since that doesn't really LOOK that good, they opted to use the array operator.

    There's one other difference I know of with arrays, and that's when you're using dynamic allocation and the delete operator. You need to use "delete[]" when you delete an array instead of plain old "delete", like you would for data types, structs, and objects.

    Hope that helps.

    - Zeke

+ Reply to Thread
Page 2 of 2
FirstFirst 1 2

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts