小编Pai*_*guy的帖子

类析构函数的实际应用

我目前正在尝试学习类和构造函数/析构函数.我明白这两者是做什么的,但是我在使用析构函数时遇到了困难,因为我无法想到它的实际应用.

任何人都可以提供一个解释的例子吗?

c++ constructor destructor class uses

4
推荐指数
2
解决办法
2452
查看次数

将数组复制到动态分配的内存

我的代码工作正常,但我觉得有一种更快的方法可以做到这一点,特别是在我的函数副本中.这是我的代码.这会更快吗?这是在顺便说一句.此外,当我从函数返回cpy时,它会删除动态内存,因为它超出了范围吗?我不希望有内存泄漏:P

#include <stdio.h>
#include <stdlib.h>
double *copy(double a[], unsigned ele);
int main(){
    double arr[8], *ptr;
    unsigned i=0;
    for(;i<7;i++){
        scanf_s("%lf", &arr[i]);
    }
    ptr=copy(arr, 8);
    for(i=0;i<7; i++)
        printf("%f", ptr[i]);

}

double *copy(double a[], unsigned ele){
    double *cpy= malloc(sizeof(double)*ele);
    int i=0;
    for(;i<ele; i++)
        cpy[i]=a[i];
    return cpy;
}
Run Code Online (Sandbox Code Playgroud)

c memory arrays pointers dynamic

4
推荐指数
2
解决办法
9964
查看次数

C中的素数

int prime(unsigned long long n){
    unsigned val=1, divisor=7;
    if(n==2 || n==3) return 1; //n=2, n=3 (special cases).
    if(n<2 || !(n%2 && n%3)) return 0; //if(n<2 || n%2==0 || n%3==0) return 0;
    for(; divisor<=n/divisor; val++, divisor=6*val+1) //all primes take the form 6*k(+ or -)1, k[1, n).
        if(!(n%divisor && n%(divisor-2))) return 0; //if(n%divisor==0 || n%(divisor-2)==0) return 0;
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码是朋友为获得素数而写的东西.它似乎正在使用某种筛分,但我不确定它是如何起作用的.下面的代码是我不那么棒的版本.我会用sqrt我的循环,但我看到他做了其他事情(可能是筛选相关),所以我没有打扰.

int prime( unsigned long long n ){
    unsigned i=5;
    if(n < 4 && n > 0)
        return 1;
    if(n<=0 …
Run Code Online (Sandbox Code Playgroud)

c numbers division sieve trial

3
推荐指数
1
解决办法
1502
查看次数

检查文件是否存在Linux bash

所以我正在尝试检查文件是否存在,然后脚本应该执行某些操作.我遇到的问题实际上是让它认识到某些东西确实存在.

if [ -e /temp/file.txt ]; then
        echo "file found!"
        sudo cp -r temp/* extra
else
        echo "file not found! Creating new one..."
        ./create.sh
fi
Run Code Online (Sandbox Code Playgroud)

下面是我正在测试的目录中的文件示例.它们显然存在,但由于某种原因,我无法让脚本看到它.我究竟做错了什么?

nima@mkt:/docs/text$ ls -a temp
.  ..  more  file.txt  file2.txt
Run Code Online (Sandbox Code Playgroud)

linux bash file

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

模板不包括一种类型

我想创建一个排除字符串或字符类型的解析函数

template <typename T>
bool parse(T & value, const string & token){
    istringstream sin(token);
    T t;
    if( !(sin >>t) )  return false;
    char junk;
    if( sin >>junk )  return false;
    value = t;
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c++ string templates types char

3
推荐指数
1
解决办法
5123
查看次数

中缀计算器表达解析器

如何在中缀计算器语法中解析和计算表达式?我想到了两种方式.

第一个涉及使用两个堆栈.一个是数字,另一个是运算符,我会评估运算符的优先级和关联,以便弄清楚如何计算表达式.

第二种方法涉及将中缀表达式转换为后缀,我不知道我该怎么做.这只是一个想法.目前我设置我的程序是为了使用第一种方法.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

bool die(const string &msg);

//stack class
class Stack{
public:
    Stack();
    void push(const double &val);
    void push(const string &oper);
    double popnum();
    string popop();
    double getopele();
    double getnumele();
private:
    static const unsigned MAX=30;
    string opstack[MAX];
    double numstack[MAX];
    unsigned opele;
    unsigned numele;
};

//operator type
struct OP{
    string name;
    void * func;
    unsigned arity;
    unsigned prec;
    bool lass;
    string descrip;
};
//operator table
OP op[]={{"+", add, 2, 4, true, "2+3 is …
Run Code Online (Sandbox Code Playgroud)

c++ stack parsing expression calculator

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

要键入的指针数组

由于我的车坏了,我今天错过了我的课.类型的指针数组是否类似于以下内容?

void *ary[10];
ary[0] = new int();
ary[1] = new float();
Run Code Online (Sandbox Code Playgroud)

我仍然有点模糊它是什么.

c c++ arrays types pointers

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