我正在尝试将一个点存储到一个数组中。数组必须是10的大小,并且点必须是0到100之间的随机数。我将使用此数组,然后通过快速排序组织它,并找出哪些点最接近。做一些研究我发现Utility类有一些我认为会起作用的东西所以我试图找出如何使用随机点生成数组。有一件事是我需要通过引用传递数组,或者只是为了确保我可以在main中使用这个数组。
#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;
}
}
答案 0 :(得分:0)
所有人都很难做到你想要的事情:
<强>的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;
}
<强> Point.h 强>
#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;
}
您可以找到进行伪随机生成器和放大器所需的所有信息。来自this网页的分发。
现在我们已经启动了随机生成器,下一步是我们声明std::vector<Point<int>>
然后我们使用它的reserve
函数并使用我们的const NUM_POINTS
设置它。然后我们对NUM_POINTS
次迭代进行for循环,并使用随机(x,y)
值集合填充容器。
然后我们使用范围基数for循环显示结果。
我重复上面的过程,以显示它是用浮动完成的。我这样做是为了展示模板的有用性。
之后我终于通过使用向量的迭代器调用std::sort( begin, end )
来简单地对容器进行排序。然后我返回并使用一个远程基数for循环来显示两个排序的向量。
使用std :: sort非常容易,因为我们为类定义了一个重载的operator<()
,我们使用std :: tie来轻松比较它们。这通过汇集像Legos这样的一系列部件,向您展示了标准库的强大功能!