• 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

    • Hello, Christian Maas' XVI32 is a nice (and very small) hex editor. Speaking of hex editors, many years ago a colleague and I who both worked at Tribal Voice managed to edit a copy of the company's PowWow instant messaging client to make it behave better now that all of its lookup servers and other server-side tech was gone.  The program didn't support NAT (RFC-3022 was introduced in January 2001, the same time Tribal Voice was shuttered), but it still worked okay if you manually set up port-forwarding on your router.  The server at http://powwow.jazy.net/ hosts a copy (usual warnings about downloading and running untrusted code from random internet servers apply). I occasionally use some tools like Funduc Software's Search and Replace and Application Mover when I need to make mass-edits to text-based files or move programs with a hard-coded installation directories, respectively.  When I need to figure out the exact LCD panel inside of a laptop, EnTech Taiwan's Monitor Asset Manager is my go-to tool for that purpose. JD Design's website (now hosted on github.io) has a number of interesting freeware and shareware utilities.  I used to use their TouchPro utility to set the file timestamps on software I was mastering to match its version number (e.g., version 3.00 of a program had all of its files dates set to 3:00AM, and so forth). Karenware has a number of interesting freeware utilities, too. Regards, Aryeh Goretsky  
    • I still use HexChat! Not really as ancient as the 1994 AutoCAD above my post, but I have never found anything better to replace it. Yes we still operate an IRC server https://www.neowin.net/irc/ 😛 
    • At work we still have a couple of people that use a version of AutoCAD LT purchased in 1994. This predates Windows 95 and works fine on versions of Windows up to XP. Its long since run in an locked down isolated XP VM, accessible via RDP. I did install LibreCAD for them, however they said it was just too different to get to grips with. In all fairness one of them is now 75 and the other is almost 60.
    • On my music making (non internet) PC Sony Acid Pro 7.0 Adobe Audition 2015 Korg Legacy Collection Windows 7 SP1
    • Anyway to download these versions without being on the Experimental builds?
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!