排序算法不适用于使用指针操作的模板
Sort algorithm is not working with my template manipulated with pointers
我在下面的代码狙击中编写了三个模板。存在输入和显示功能正常工作但排序功能不完全工作的问题。代码中存在一些逻辑问题,但我找不到它。请帮助我找到并解决它。该程序执行得很好,但是当我输入数据时,我认为它会以某种方式进入无限循环,并且其中的所有内容似乎都停止了。
#include<iostream>
using namespace std;
template<typename T>
void input(T* &array, int limit){
array = new T[limit];
T* start = array;
T* beyond = &start[limit];
while(start != beyond){
cout<<"nEnter: ";
cin>>*start;
start++;
}
}
template<typename T>
void sort(T* array, int limit){
T hold;
T* start = array, *beyond = &start[limit];
T* Next = &start[1];
for(int j=0; j<limit-1; j++){
while((start != beyond) || (Next != beyond)){
if(start>Next){
hold=*start;
start=Next;
*Next=hold;
start++;
Next++;
}
}
}
}
template<typename T>
void display(T* start, int limit){
T* beyond = &start[limit];
cout<<"nAfter Sorting: "<<endl;
while(start != beyond){
cout<<*start<<"t";
start++;
}
}
int main(){
int* x=NULL;
float* y=NULL;
char* z=NULL;
int size;
cout<<"Enter the number of elements: ";
cin>>size;
cout<<"nEnter integer values:";
input<int>(x, size);
sort<int>(x, size);
display<int>(x, size);
cout<<"nEnter floating values:";
input<float>(y, size);
sort<float>(y, size);
display<float>(y, size);
cout<<"nEnter character values:";
input<char>(z, size);
sort<char>(z, size);
display<char>(z, size);
cout<<endl;
system("pause");
}
我不确定你为什么要尝试使用模板来做到这一点。但这里有一些不好的事情。
array = new T[limit];
在这里,您通过new分配内存,但从不删除它 - 这是一个严重的错误。阅读新建和删除 - 您可能需要考虑使用类似std::shared_ptr<T> array (new T[limit], std::default_delete<T[]>())
来创建共享指针(include <memory>
在任何 c++11 编译器上
while ((start != beyond) || (Next != beyond)) {
if (start>Next) {
这将永远循环(我相信这是您问题的原因(,您仅在开始大于下一个时增加启动,但是由于开始低于下一个,这永远不会发生!
例如:start = 0x0000A 和 next = 0x0000C,Start 低于 next,因此您永远不会输入 if 语句,start++
永远不会发生。
建议:
正如其他人所说,使用标准库是一个非常好的主意。
尝试以这种方式对数据进行排序,使用指针和数组是一个非常糟糕的主意,在现代C++中使用new和delete几乎是一个禁忌(至少在我工作的地方;-(。
永远不要使用namespace std;
。
考虑使用标准库。
大多数问题在过去已经解决,其中很多问题都是如此基本,以至于您可以找到它的标准实现。
在重新实现解决方案时,您可以期望的最好的事情就是不要编写错误的代码。这些标准不仅经过充分测试,在阅读代码时,只需查看它,您就可以了解很多功能。如果你阅读了一个自定义实现,你必须检查它的作用。如果您阅读正在使用的标准实现,您已经确切地知道它的作用。
下面是一个代码片段,它将以与上述类似的方式对代码进行排序,但使用标准实现。
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
using std::cout;
using std::endl;
using std::cin;
using std::vector;
using std::string;
template<typename T>
void input(string msg, T &element)
{
cout << msg;
cin >> element;
}
template<typename T>
void display(string label, const vector<T> & v)
{
cout << label;
for (const T & t : v)
{
cout << " ";
cout << t;
}
cout << endl;
}
int main()
{
vector<int> in;
int size;
cout << "Enter the number of elements: ";
cin >> size;
in.reserve(size);
for (int i = 0; i < size; ++i)
{
int temp;
input("Input Number: ", temp);
in.emplace_back(temp);
}
//http://en.cppreference.com/w/cpp/algorithm/sort
display("before sorting", in);
std::sort(in.begin(), in.end(), std::greater<int>());
display("after sort greater", in);
std::sort(in.begin(), in.end(), std::less<int>());
display("after sort less", in);
//in Visual Studio hit (CTRL + F5) to start with a pause after execution ends
}
如果您必须以某种方式实现循环访问指针的排序函数。这是一个低效的版本:
template<typename T>
void sort(T* array, int limit)
{
T* start = array;
T* end = start + limit;
for (T* outer = start; outer < end; ++outer)
{
for (T* inner = start; inner < end; ++inner)
{
if (*outer > *inner)
{
T hold = *inner;
*inner = *outer;
*outer = hold;
}
}
}
}
- 迭代器的指针操作问题
- 我试图了解在异或操作后指针变量正在更改
- 通过浮点指针操作字节向量
- 使用 XOR 操作仅使用 2 个指针反转链表
- 为什么指针在由后增量比较器操作时行为不同?
- 指针 OOP 操作
- C++数组指针上的删除操作
- 如何有效地使用 std::async 对指针数组执行操作
- 为什么在执行增量操作之前分配指针值
- 是否可以使用C++函数指针,如 C# 的操作?
- 共享指针,C 版本的原子操作
- 普通指针,连同通常的操作 & 和 *,是 monad 吗?
- 双指针 **src 和操作 (*(*src)++ = reg_offset)
- 调用向量内的函数指针不执行任何操作
- 调用 vector.erase() 函数时指针操作无效错误
- 即使在单线程程序中,共享指针是否在引用计数中使用原子操作
- C 弦乐操作用指针,中间为空
- 尝试仅通过操作指针对链表进行排序
- 在类内部操作指针
- 仅通过操作指针交换链表中的相邻节点