我想实现一个简单的静态类,该类可以在练习时计算c ++中整数的pow值。所以我的代码在这里:
#pragma once
#ifndef MATH_H
#define MATH_H
static class Math
{
public:
static int pow(int,int);
};
#endif /* MATH_H */
Run Code Online (Sandbox Code Playgroud)
和pow功能的实现:
#include "Math.h"
int Math::pow(int base, int exp){
if(exp==1)
return base;
else if(exp%2==0)
return pow(base,exp/2)*pow(base,exp/2);
else
return pow(base,exp/2)*pow(base,exp/2)*base;
}
Run Code Online (Sandbox Code Playgroud)
但是cygwin编译器抛出编译错误:
In file included from Math.cpp:16:0:
Math.h:16:1: error: a storage class can only be specified for objects and functions
static class Math
^~~~~~
Run Code Online (Sandbox Code Playgroud)
我已经编写了一个代码来在 C++ 中实现快速排序算法,它正在运行,但 std::sort() 函数根本不起作用。请向我解释原因。
#include "header.h"
using namespace std;
using namespace std::chrono;
bool myfunction (int i,int j) { return (i<j); }
int Partition(vector<int>& A, int start, int end){
int pivot_idx = (rand() % (end - start + 1)) + start;
Xswap(A[pivot_idx], A[end]);
int pivot = A[end];
int P_index = start;
for(int i=start; i < end; i++){
if(A[i] <= pivot){
Xswap(A[i], A[P_index]);
P_index++;
}
}
Xswap(A[P_index], A[end]);
return P_index;
}
void Qsort(vector<int>& A, int start, int end){
if(start < end){ …Run Code Online (Sandbox Code Playgroud)