小编Sas*_*hra的帖子

如何在C ++中实现静态类?

我想实现一个简单的静态类,该类可以在练习时计算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)

请帮助我解决我的问题。这是Microsoft课程的正式文档,据我了解,C ++中有静态类

c++ static-methods static-classes

2
推荐指数
1
解决办法
71
查看次数

std::sort 函数不是对向量进行排序,我不知道为什么?

我已经编写了一个代码来在 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)

c++ sorting algorithm stl c++11

0
推荐指数
1
解决办法
112
查看次数

标签 统计

c++ ×2

algorithm ×1

c++11 ×1

sorting ×1

static-classes ×1

static-methods ×1

stl ×1