Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:42:44+00:00 2026-05-31T20:42:44+00:00

I have the template class and array of pointers to objects and overloaded logic

  • 0

I have the template class and array of pointers to objects and overloaded logic operators for my objects. My bubble sort is working. So it’s know how to compare my objects
I want to replace it with standard sort
Declaration

Implementation List.tem

#include <stdio.h>
#include <stdlib.h>
#include <vector>
///////////////
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

template <class Type>
List<Type>::List()
{
    itemPtr = NULL;
    used = 0;
    size = 0;
}


template <class Type>
List<Type>::List(const List<Type>& source)
{
    itemPtr = NULL;
    used = 0;
    size = source.size;
    itemPtr = new Type[size];
    for(int i = 0; i < source.used; i++)
        addItem(source.itemPtr[i]);
}

template <class Type>
List<Type>& List<Type>::operator = (const List<Type>& source)
{
    used = 0;
    if(this == &source)
        return(*this);  
    free();

    size = source.size;
    itemPtr = new Type[size];

    for(int i = 0; i < source.used; i++)
        addItem(source.itemPtr[i]);
    return(*this);
}    

template <class Type>
List<Type>::~List()
{
    free();
}

template <class Type>
void List<Type>::free() 
{
    if(itemPtr != NULL)
    {
        delete [] itemPtr;
        itemPtr = NULL;
    }
}

template <class Type>
void List<Type>::alloc(int sizeIcrease) 
{
    Type* tmpPtr = NULL;
    size += sizeIcrease;
    tmpPtr = new Type[size];
    copy(itemPtr, itemPtr+used, tmpPtr);
    free();
    itemPtr = tmpPtr;
}

template <class Type>
Type List<Type>::getItem(int index) const
{
    Type item;
    if(index >= 0 && index < used)
        item = itemPtr[index];
    return (item);
}    

template <class Type>
int List<Type>::findItem(Type itemIn)
{
    int i = 0;
    for(i = 0; i < used; i++)
        if(itemPtr[i] == itemIn)
            break;
    if (i == used)  i = -1;
    return (i);
}

template <class Type>
void List<Type>::addItem(Type itemIn)
{
    if(used == size) alloc(10);
    itemPtr[used++] = itemIn;
    bubbles();
}

template <class Type>
void List<Type>::removeItem(Type itemIn)
{
    int index = findItem(itemIn);
    removeItem(index);
}

template <class Type>
void List<Type>::removeItem(int index)
{
    if(index >= 0 && index < used)
        itemPtr[index] = itemPtr[--used];
    bubbles();
}

template <class Type>
void List<Type>::readFile(Field fileName)
{
    Type itemTmp;   
    ifstream inFile(fileName.c_str());
    if(!inFile)
        cout << "Error opening file\n";
    do
    {
        inFile >> itemTmp;
        if(!inFile.fail())
            addItem(itemTmp);

    } while (!inFile.fail());
    //bubbles();
    vector<Type*> myvector; //(itemPtr, itemPtr+used);

    vector<Type>::iterator it;
    sort (myvector.begin(), myvector.end(), sort_by_pointee<Type>());
    inFile.close();
}


template <class Type>
void List<Type>::writeFile(Field fileName)
{
    ofstream outFile;
    outFile.open(fileName.c_str());
    if(!outFile)
        cout << "Error opening file\n";

    for(int i = 0; i < used; i++)
    {
        outFile << itemPtr[i] << "\n";
    }

    outFile.close();
}
    template <class Type>
void List<Type>::print()
{
//////Coded
}

template <class Type>
void List<Type>::bubbles()
{
////// Coded
}

template <class Type>
ostream& operator<<(ostream& os, const List<Type>& ad)
{
    for(int i = 0; i < ab.used; i++)    
        os << ab.getItem(i) << endl;
    return os;
}
template <class Type>
ofstream& operator<<(ofstream& ofs, const List<Type>& ad)
{
    for(int i = 0; i < ab.used; i++)            
        ofs << ab.getItem(i) << ",";
    return ofs;
}

template<class Type>
struct sort_by_pointee 
{
    bool operator() (const Type* lhs, const Type* rhs) const
    {
        return (*lhs < *rhs);
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:42:45+00:00Added an answer on May 31, 2026 at 8:42 pm

    If the third argument to std::sort() is not provided, objects are sorted using operator< like:

    if (a < b) {
       // ...
    }
    

    So all you need to sort objects of type Foo is to have either:

    bool Foo::operator< (const Foo& rhs) const;
    

    or

    bool operator< (const Foo& lhs, const Foo& rhs);
    

    That being said, if you have an array of pointers, then you will need to provide a custom predicate unless you want to sort objects by their memory address (I highly doubt this is what you want). You can do something like:

    template<class T>
    struct sort_by_pointee {
        bool operator() (const T* lhs, const T* rhs) const
        {
            return (*lhs < *rhs);
        }
    };
    

    And use it like:

    std::vector<Foo*> foos;
    // ...
    std::sort(foos.begin(), foos.end(), sort_by_pointee<Foo>());
    

    Edit: the sample you posted will work fine and sort the data, but the vector does not act as a proxy for the data stored in the itemPtr array. Read it again with my annotations:

    { 
        vector<Type> myvector (itemPtr, itemPtr+8);
        // 'myvector' holds a copy of the first 8 elements in the 'itemPtr' array.
    
        sort (myvector.begin(), myvector.end());
        // contents of 'myvector' are sorted, but this is a copy of 'itemPtr''s
        // contents, so items in 'itemPtr' are still in their original order.
    }
    

    If you want to sort the contents of [itemPtr,itemPtr+8) in-place, you can just do:

    std::sort(itemPtr, itemPtr+8); // use custom predicate if required.
    

    Edit: OK, following the code you posted, I would fix the readFile() method from its original definition to:

    template <class Type>
    void List<Type>::readFile(Field path)
    {
        ifstream file(path.c_str());
        if(!file.is_open()) {
            cout << "Error opening file\n";
        }
        for (Type item; file >> item;) {
            addItem(item);
        }
        sort (itemPtr, itemPtr+used);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class where I want to use objects of that class
I have a template class ArrayTemp that creates an array of pointers to type
I want to have a template class that looks something like what I have
I am writing a template class for an array of objects, call it arrayobjclass,
I have an array of pointers to other objects called Comparable* array (inside a
I have a template class that I serialize (call it C), for which I
I have a template class like below. template<int S> class A { private: char
I have a template class that is only valid for couple of template parameters:
I have a template class for thread-safe vector: template <class T> class SharedVector {
I have a template class defined like so: template <class T> class Command {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.