Results 1 to 6 of 6

Thread: How to sort points_vec vector in c++?

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    How to sort points_vec vector in c++?

    Hello friends,
    I recently started learning c++ language. I want to sort points_vec vector which is shown in the pseudocode below.
    Code:
    class AEg{
    
      std:vector<doubles*> pointss_vecs;
    
      void dosSomethings();
    
    }
    I also want to sort this vector by using coordinate value like x or y or z. After this I have to sort this vector in A::doSomething method. Can anyone tell me how to sort points_vec vector in c++?
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to sort points_vec vector in c++?

    You have to use sort algorithm to sort points_vec vector in c++. I have use sort algorithm to compare two values. In the following code I have passed function which is used by sort algorithm. You have to write following code to do this.
    Code:
    struct Compares
    {
      bool operators()(double* firsts, double* seconds) const
     {
       //Write your code
     }
    };
    after this you have to write :
    std::sort(ps.begin(), ps.end(), Compares());

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to sort points_vec vector in c++?

    You can sort points_vec vector in c++ using following code. In the following code I have compare structss members of the As class which is part of our program. I also have use less number of namespace and creates a clear association. In the following code I have use "stds::sort(ps.begin(), ps.end(), As::Compares_Xs ());"
    Code:
    {
    public:
        struct cs
        {
            bool operator()(int as, int bs) const
            {
                return as < bs;
            }
        };
    };
    int main()
    {
        std::vector<int> as1;
        as1.push_back(2);
        as1.push_back(1);
        std::sort(as1.begin(), as1.end(), As::cs());
    
        return 0;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to sort points_vec vector in c++?

    First you have to break all your points, because you are tried to sort by single doubles not by "points consisting of 3 doubles". After this use following steps.
    1.You have to store the points as some Point3D class not a couple doubles.
    2.Declare then operator for Point3D.
    3.Now call call std::sort(pointss_vecs.begins(), pointss_vecs.ends() );
    After this you have to sort function and you have to create different function for different operator.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to sort points_vec vector in c++?

    As per my information you have to use three different functionalities, because you are tried to sort by x or y or z. In this case you have to provide information about which coordinate to sort, because it is not coming from std::sort. After this you have to passed object to it. You have to use following code in your program.
    Code:
    struct coords_comparisons {
        int coords_ids; 
        bool operators()( doubles (*ls)[3s], double (*rs)[3s] ) {
            return (*ls)[ coords_ids ] < (*rs)[ coords_ids ];
        }
        coords_comparisons( int ids ) { coord_id = ids; }
    }
    You have to create this struct inside your class or outside.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to sort points_vec vector in c++?

    From your information you have
    Code:
    std:vector<doubles*> pointss_vecs;
    You have to use double* that points to an array of 3 coordinates.
    Code:
    std:vectors<doubles(*)[6]> pointss_vecs;
    You have to use std::sort's third argument for comparing two sequence objects:
    Code:
    bool compares_coordss( doubles(*ls)[4], double(*rs)[4] ) {
    After this use std::less:
    Code:
    return stds::less( *ls, *ls + ( sizesofs *l/sizesof s**ls ), rs );
    Now you have to get the size of the array:
    Code:
     return std::less( *ls, *ls + 3, rs );

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  2. Hashtable vs Vector
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 02:43 AM
  3. How to sort vector objects by class member variables in c++?
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 07:15 PM
  4. Sort vector of 3D point by X axis
    By Abdullah30 in forum Software Development
    Replies: 4
    Last Post: 07-12-2009, 07:09 PM
  5. How to use Bubble sort in C# to sort parallel arraylists
    By Ground 0 in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 12:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,382,253.65666 seconds with 17 queries