![](/img/trans.png)
[英]How to sort an array or ArrayList<Point> ASC first by x and then by y?
[英]Storing (x,y) point into array
我試圖將一個點存儲到數組中。 數組的大小必須為10,點必須是0到100之間的隨機數。我將使用此數組,然后通過快速排序對其進行整理,並找出哪些點最接近。 做一些研究后,我發現Utility類有一些我認為可以起作用的東西,因此我試圖找出如何使數組生成帶有隨機點的方法。 一件事是我需要通過引用傳遞數組,或者以確保我可以在主數組中傳遞的方式傳遞數組。
#include <iostream>
#include "qsort.h"
#include <stdlib.h>
#include <utility>
using namespace std;
const int ARRAY_SIZE = 10;
void initializePairs(pair<int,int> array);
int main()
{
//pair<int, int> shortPointArray[ARRAY_SIZE];
/*pair<int,int> temp = make_pair(5,6);
pair<int,int> shortPointArray[1];
shortPointArray[0] = temp;*/
pair<int,int> shortPointArray[1];
//qsort sorting;
initializePairs(shortPointArray);
return 1;
}
void initializePairs(pair<int,int> array)
{
int x;
int y;
pair<int,int> temp;
for(int i = 0; i < ARRAY_SIZE; i++)
{
x = rand() % 100;
y = rand() % 100;
temp = make_pair(x,y);
array[i] = temp;
}
}
要做您要尋找的並非全部:
main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include "Point.h"
int main() {
std::random_device rd; // Random Device: Used To Seed Mersenne Random Generator
std::mt19937 gen; // Mersenne Twister
gen.seed( rd() ); // Seed The Generator
std::uniform_int_distribution<> dist(0, 100); // Uniform Int Distribution between [a, max]
// Point<int>
std::vector<Point<int>> points;
points.reserve( NUM_POINTS );
for ( std::size_t i = 0; i < NUM_POINTS; i++ ) {
// Instead of creating a temporary stack copy each iteration
// I chose to use the constructor directly and instead of
// push_back, I'm using emplace_back.
// Point<int> p( dist( gen ), dist( gen ) );
// points.push_back( p );
points.emplace_back( Point<int>( dist( gen ), dist( gen ) ) );
}
std::cout << "Showing 10 points of type int with random (x,y):\n";
for ( auto& p : points ) {
std::cout << p;
}
std::cout << std::endl;
// Point<float>
std::vector<Point<float>> points2;
points.reserve( NUM_POINTS );
std::uniform_real_distribution<float> dist2( 0, 100.0f );
for ( std::size_t i = 0; i < NUM_POINTS; i++ ) {
// Instead of creating a temporary stack copy each iteration
// I chose to use the constructor directly and instead of
// push_back, I'm using emplace_back.
// Point<float> p( dist( gen ), dist( gen ) );
// points2.push_back( p );
points2.emplace_back( Point<float>( dist( gen ), dist( gen ) ) );
}
std::cout << "Showing 10 points of type float with random (x,y):\n";
for ( auto& p : points2 ) {
std::cout << p;
}
std::cout << std::endl;
// Sorting the containers:
std::sort( points.begin(), points.end() );
std::sort( points2.begin(), points2.end() );
std::cout << "Showing the sorted points with type int (x,y):\n";
for ( auto& p : points ) {
std::cout << p;
}
std::cout << std::endl;
std::cout << "Showing the sorted points with type float (x,y):\n";
for ( auto& p : points2 ) {
std::cout << p;
}
std::cout << std::endl;
std::cout << std::endl;
system( "PAUSE" );
return 0;
}
點
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <tuple> // std::tie
const std::size_t NUM_POINTS { 10 };
// Need class Point prototype for operator<< declaration
template<class> class Point;
// Need operator<< declaration for class template Point's friend declaration
template<class T>
std::ostream& operator<<( std::ostream& out, const Point<T>& );
// Class Declaration & Definition
template<class T>
class Point {
public:
T _x;
T _y;
Point() : _x( 0 ), _( 0 ) {}
Point( T x, T y ) : _x( x ), _y( y ) {}
Point( T& x, T& y ) : _x( x ), _y( y ) {}
Point( T* x, T* y ) : _x( *x ), _y( *y ) {}
// friend prototype: notice the extra <> in this declaration
// It tells the compiler that this friend function will be a specialization of this class template
friend std::ostream& operator<< <>( std::ostream& out, const Point<T>& p );
// operator< for comparison
bool operator<( Point<T>& p ) {
// std::tie makes it real easy to compare a (set) of values.
return std::tie( _x, _y ) < std::tie( p._x, p._y );
}
// operator> for comparison
bool operator<( Point<T>& p ) {
return !(*this < p );
}
// operator== for comparison
bool operator==( Point<T>& p ) {
return (this->_x == p._x && this->y == p._y );
}
};
// operator<< definition
template<class T>
std::ostream& operator<<( std::ostream& out, const Point<T>& p ) {
return out << "(" << p._x << "," << p._y << ")\n";
}
#endif // !POINT_H
至於類template Point<T>
的實現,可以引用頭文件中的注釋。
有關主要功能的細節,我將介紹其中一些細節。
為了生成您的隨機值,我強烈建議您不要使用random()
或其相關的任何已棄用或即將成為函數的函數。 我將從學習和使用偽隨機生成器開始,這些偽隨機生成器可以在標准庫中找到以及不同類型的分布:所有這些都可以在<random>
頭文件中找到。 您可以使用std::default_random_engine()
但我更喜歡使用std::random_device
我們可以使用它來對我們選擇的引擎(生成器)進行SEED
。 一種更流行使用的引擎或發電機稱為Mersenne Twister
,它的名稱為std::mt19937
,它也有65位版本。 這很簡單。
{
std::random_device rd; // create an instance of our device to seed with
std::mt19937 gen; // create an instance of our generator (engine)
gen.seed( rd() ); // This seeds the generator (engine)
// Now we need a distribution along with its data type
// there are different versions of these distributions for different types
// Some are for integral types while others are for floating point types
// Here we want a uniform distribution for int so we default the template
std::uniform_int_distribution<> dist(0, 100); //random from [0,100]
// otherwise we could of done
std::uniform_int_distribution<unsigned int> dist2( 0, 50 ); // random from [0, 50]
// There are other types of distributions
std::normal_distribution<> a;
std::poisson_distribution<> b;
// etc.
// If the distributions say "real" they are floating point types
std::uniform_real_distribution<float> f;
std::uniform_real_distribution<double> d;
// Just as there are different distributions there also other
// generators or engines beside the mersenne twister.
// There is another way besides using `random_device` to seed the generator
// you can use <chrono> header to use `std::chrono::high_resolution_clock
// to seed the generator
// You can also seed by const value
// and you can use std::seed_seq;
}
你可以找到你需要從做偽隨機發生器及分布的所有信息此網頁。
因此,現在我們有了隨機數生成器並開始下一步,我們聲明一個std::vector<Point<int>>
然后使用其reserve
功能並將其設置為const NUM_POINTS
。 然后,我們進行NUM_POINTS
循環進行NUM_POINTS
次迭代,並用一組隨機(x,y)
值填充容器。 然后,我們使用范圍為循環的底數顯示結果。
我重復以上過程以顯示它是使用浮點數完成的。 我這樣做是為了展示模板的有用性。
之后,我最終使用向量的迭代器通過調用std::sort( begin, end )
來對容器進行std::sort( begin, end )
。 然后,我回過頭來,使用循環范圍的底數來顯示兩個排序的向量。
使用std :: sort非常容易,因為我們為類定義了一個重載的operator<()
,並且我們使用了std :: tie來輕松比較它們。 通過將諸如Legos套之類的許多零件組合在一起,可以向您展示成為標准庫的力量!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.