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

  • Home
  • SEARCH
  • 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 8598783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:14:12+00:00 2026-06-12T01:14:12+00:00

I am writing my own vector class, Vector, with the data members: T* array,

  • 0

I am writing my own vector class, Vector, with the data members: T* array, size_t vector_size and size_t capacity. I am trying to create a sort() method:

template <class T>                                                                                                 
void Vector<T>::sort(bool ascending)                                                                                 
{                                                                                                                   
    std::sort(array,array+vector_size);                                                                              
    if(ascending==false)                                                                                             
        std::reverse(array,array+vector_size);                                                                      
}   

It works fine when the elements in the array are of type int, char etc. But when I try to sort a vector consisting of Vector elements it won’t compile.
From what I have read I need to define the <operator in some way, but I really don’t know how to do that…

I have tried:

template <class T>
bool Vector<T>::operator<(Vector<T> & source) const
{
    return (vector_size < source.vector_size);
}

My main look like this:

int main() {
    Vector<int> v1(5,1);
    Vector<int> v2(7,2);
    Vector<int> v3(3,3);
    Vector<Vector<int>> v4;
    v4 = {v1,v2,v3};
    v4.sort(1);
return 0;
}

This is one of the errors I get:

/usr/include/c++/4.6/bits/stl_algo.h:2212:4: error: no match for ‘operator<’ in ‘* __first < __pivot’

  • 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-06-12T01:14:14+00:00Added an answer on June 12, 2026 at 1:14 am

    You provided a comparison method with the wrong signature. You need to accept a const reference or a value, but not a (modifiable) reference to your type, while the former should be preferred unless it’s a primitive type. So the signature of your comparison method should look like this:

    template <class T>
    bool Vector<T>::operator<(const Vector<T> & source) const
    {
        return (vector_size < source.vector_size);
    }
    

    This is because std::sort (and a lot of other methods) are designed to not modify the contents. This is guaranteed if they take a value (but this will be slow for large types) or a const reference.

    Note that you defined the comparison method to compare the size of the vectors, not their contents. All your vectors are of equal length. So they are treated to be equal by std::sort. So std::sort wouldn’t change v4… If you intend to compare the contents in a way similar to string comparison (the first entry counts first, if equal then take the next and so on…), use this:

    template <class T>
    bool Vector<T>::operator<(const Vector<T> & source) const
    {
        for(int i = 0; i < size && i < source.size; ++i) {
            if(*this[i] < source[i])
                return true;
            else if(source[i] < *this[i])
                return false;
        }
        // You have to decide what to do if the length isn't equal.
        // But if the vectors are really equal than return false:
        if(size == source.size)
            return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing my own comparator class called PercentComparator and called the sort as
I am writing my own Vector class by inheriting from my own Point class.
I'm writing an own container class and have run into a problem I can't
I've written a (array) container class template (lets call it smart array ) for
I made my own class template to maintain pair of elements of any type
I used boost::interprocess::managed_(windows_)shared_memory::construct to construct an interprocess vector holding an own class, which has
I'm writing my own UserControl which displays data in a ListBox. I want to
I'm writing own switch class. I'd like to add a delegate to it -
I'm writing my own class to manage mongodb queries within express framework. This how
I am writing my own 'Rubik's cube' application. The main class Cube has 18

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.