• 0

[C++] Sorting a Vector of vectors


Question

Hello,

I have an entity in 3D, which is defined by a std::vector of points. Each point is an std::vector containing three values :, X, Y and Z.

So it's really a vector< vector< int > >.

I want to find the geometrical center of the 3D entity. To do this, I think I have to find the minimum X and maximum X, and take the value in-between, repeat for Y, repeat for Z; those three values are the center.

I could of course proceed with a bunch of for loops but this is tedious. To find maximum and minimum values, I could more easily use the sort algorithm of the STL. I'm a bit clueless at how I would implement this, though. Yes generally speaking I know how to implement a functor for an algorithm but in this case my brain refuses to cooperate.

Any help is appreciated.

Link to comment
https://www.neowin.net/forum/topic/700528-c-sorting-a-vector-of-vectors/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

If I read your post right, your vector of vectors is structured like this:

VectorList< Vector<int>[3] = {x, y, z} > (an arbitrarily-sized list of vectors, where each vector contains 3 elements)

and you want to find the geometrical center. There isn't anything in the STL that can do it this way because of your structure. Honestly, you've got the organization a bit backward for the goal you're trying to achieve. It would be a lot simpler if the inner vector was a list of (x/y/z)-coordinates, and the outer vector contained each inner vector, something like this:

VectorList< Vector<int> >[3] (a list of 3 vectors, where each vector is of arbitrary size)

You might create a function that returns a vector as described with the input being your own implementation, and then pass the result as an argument to the actual function that computes the geometric center, which might make use of things from the <algorithm> header like std::min_element() and std::max_element(). FWIW, I said a conversion function would be useful because both have their strengths and weaknesses.

I've got the code ready, but it seems like you really enjoy programming; I think I'll let you think about it. ^_^

I hope this helps!

  • 0

I'm not sure why you are using a vector to store a static number of variables.

In my opinion, a better way would be to create a Point class and a PointVector class (or however you want to name them). The Point class would simply hold the x, y, and z coordinates and have methods to access and edit those values, and the PointVector class would implement a vector to hold those points. You could then create methods in the PointVector class to sort by whichever point you want or give you min and max of whichever point. Such simple classes wouldn't take very long to write.

  • 0

I am in no way responsible for the poor design, this is an assignment. I cannot change the provided implementation AFAIK, I have to work with it.

So it seems my brain refused to cooperate because there's no evident way of doing it with STL algorithms. Sigh. I guess I'll do what rpgfan suggested.

Thanks. :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • PDF Shaper 15.6 by Razvan Serea PDF Shaper is a set of feature-rich PDF software that makes it simple to split, merge, watermark, sign, optimize, convert, encrypt and decrypt your PDF documents, also delete and move pages, extract text and images. The program is optimized for low CPU resource usage and operates in batch mode, allowing users to process multiple PDF files while doing other work on their computers. PDF Shaper is available in three editions - Free, Premium and Ultimate. Compare and pick edition which is suitable for you. Compatible with Windows 7, 8, 10, 11. Features: Convert PDFs to Word, text, or image files (and vice versa) Merge, split, and watermark documents with precision Insert, move, delete, rotate and crop pages/ranges Rename and organize PDF collections efficiently Encrypt with advanced AES password protection Apply multiple digital signatures for documents Extract text, images, or complete pages from any PDF Benefits: Easy-to-use, intuitive user interface Low CPU resource usage during any process, including conversion Free for personal use and for any non-commercial organization Supports Unicode characters Supports batch processing for any operation Small installation size PDF Shaper 15.6 changelog: Improved overall program performance. Improved image rendering in all tools. Improved XMP information extraction. Improved compatibility with ARM64 systems. Improved DOC to PDF conversion: Improved support for font files. Improved support for picture cropping. Improved support for list formatting. Improved support for text line spacing. Download: PDF Shaper 15.6 | 8.0 MB (Free for personal use only) Link: PDF Shaper Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yeah, this is absolutely nothing new and EA have done it before. Burnout Paradise, released in 2008, had dynamic advertising billboards that were updated via the internet and targeted people based on location and what EA knew about them from their profile. It was particularly notable for the fact that the Obama presidential campaign ran ads in the game, in an attempt to reach a younger audience who didn't watch broadcast TV any more. It was by no means the first though. Battlefield 2142 from 2006 had the same thing. In fact, Neowin wrote a story about it back then. https://www.neowin.net/news/ba...-in-game-ads-clarification/
    • This is obviously aimed at the education where Apple has lost so much ground to Chromebooks in the last few years, but unless they come up with a comparable management system for education why would anyone switch back?
    • Here's how we arrived at that claim: Note that this is just Play Store downloads. The app is also available on the Galaxy App Store
    • Google Play states the app had more than 50 million downloads. What other metric do you suggest should be used?
  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      531
    2. 2
      +Edouard
      206
    3. 3
      PsYcHoKiLLa
      130
    4. 4
      Steven P.
      90
    5. 5
      neufuse
      74
  • Tell a friend

    Love Neowin? Tell a friend!