小编qed*_*qed的帖子

无法在C中分配指向基元的指针?

我想知道为什么这可以编译:

#include <stdio.h>
int main(int argc, char const* argv[])
{
    char *str[3];
    char x = 'a';
    char *px;
    px = &x;
    str[0] = px;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

虽然这不能:

#include <stdio.h>
int main(int argc, char const* argv[])
{
    char *str[3];
    str[1] = &'a';
    return 0;
}

decla.c: In function ‘main’:
decla.c:9:14: error: lvalue required as unary ‘&’ operand
Run Code Online (Sandbox Code Playgroud)

c pointers

6
推荐指数
3
解决办法
741
查看次数

如何在Rcpp中向对象添加多个类?

我试过这个:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector difflag(NumericVector x, int lag) {
    int n = x.size();
    NumericVector out(n-lag);

    for(int i=0; i<(n-lag); i++) {
        out[i] = x[i+lag] - x[i];
    }

    out.attr("class") += "myclass";
    return out;
}
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误:

all.cpp: In function ‘Rcpp::NumericVector difflag(Rcpp::NumericVector, int)’:
all.cpp:64:26: error: no match for ‘operator+=’ in ‘Rcpp::RObject::attr(const string&) const((* & std::basic_string<char>(((const char*)"class"), (*(const std::allocator<char>*)(& std::allocator<char>()))))) += "myclass"’
make: *** [all.o] Error 1
Run Code Online (Sandbox Code Playgroud)

r rcpp

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

为什么断言不起作用?

这是代码:

#include <Rcpp.h>
#include <iostream>
#include <assert.h>
#include <stdio.h>

using namespace Rcpp;


// [[Rcpp::export]]
double eudist(NumericVector x, NumericVector y) {
    int nx = x.size();
    int ny = y.size();
    std::cout << nx << '\n' << ny << std::endl;
    assert(nx == ny);

    double dist=0;
    for(int i = 0; i < nx; i++) {
        dist += pow(x[i] - y[i], 2);
    }

    return sqrt(dist);
}
Run Code Online (Sandbox Code Playgroud)

在将其获取到R之后,我得到以下结果,显然在出现错误时它不会中止:

#////////////////////////////////////////////////////
sourceCpp('x.cpp')
#////////////////////////////////////////////////////
eudist(c(0, 0), c(1, 1))
2
2
[1] 1.4142
#////////////////////////////////////////////////////
eudist(c(0, 0), c(1, 1, 1))
2 …
Run Code Online (Sandbox Code Playgroud)

c++ r rcpp

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

R.1代表..1和..2代表什么?

从R语言定义引用:

请注意,ls函数默认不列出以句点开头的标识符,并且'.'和'..1','..2'等是特殊的.

以下标识符具有特殊含义,不能用于对象名称,否则重复,而函数用于下一个中断TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_ ... ..1 ..2等.

但是它没有给出任何进一步的细节.有人可以详细说明吗?

r reserved-words

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

在R中使用宏是否安全?

我使用包中的defmacro函数在R中编写了一些宏gtools:

#' IfLen macro
#' 
#' Check whether a object has non-zero length, and 
#' eval expression accordingly.
#' 
#' @param df An object which can be passed to \code{length}
#' @param body1 If \code{length(df)} is not zero, then this clause is evaluated, otherwise, body2 is evaluated.
#' @param body2 See above.
#' @importFrom gtools defmacro
#' 
#' @examples 
#' ifLen(c(1, 2), { print('yes!') }, {print("no!")})
#' 
#' @author kaiyin
#' @export
ifLen = gtools::defmacro(df, body1, …
Run Code Online (Sandbox Code Playgroud)

macros r

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

确保在HashSet中均匀分布散列,它是如何工作的?

以下是从简介到Java编程(Liang)的示例:

import java.util.LinkedList;

public class MyHashSet<E> implements MySet<E> {
  // Define the default hash table size. Must be a power of 2
  private static int DEFAULT_INITIAL_CAPACITY = 16;

  // Define the maximum hash table size. 1 << 30 is same as 2^30
  private static int MAXIMUM_CAPACITY = 1 << 30; 

  // Current hash table capacity. Capacity is a power of 2
  private int capacity;

  // Define default load factor
  private static float DEFAULT_MAX_LOAD_FACTOR = 0.75f; 

  // Specify a load factor …
Run Code Online (Sandbox Code Playgroud)

java hash

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

在PostgreSQL中UNION之后是否保留了顺序?

这是代码:

CREATE TABLE audit_trail (
      old_email TEXT NOT NULL,
      new_email TEXT NOT NULL
);

INSERT INTO audit_trail(old_email, new_email)
  VALUES ('harold_gim@yahoo.com', 'hgimenez@hotmail.com'),
         ('hgimenez@hotmail.com', 'harold.gimenez@gmail.com'),
         ('harold.gimenez@gmail.com', 'harold@heroku.com'),
         ('foo@bar.com', 'bar@baz.com'),
         ('bar@baz.com', 'barbaz@gmail.com');


WITH RECURSIVE all_emails AS (
  SELECT  old_email, new_email
    FROM audit_trail
    WHERE old_email = 'harold_gim@yahoo.com'
  UNION
  SELECT at.old_email, at.new_email
    FROM audit_trail at
    JOIN all_emails a
      ON (at.old_email = a.new_email)
)
SELECT * FROM all_emails;

        old_email         |        new_email
--------------------------+--------------------------
 harold_gim@yahoo.com     | hgimenez@hotmail.com
 hgimenez@hotmail.com     | harold.gimenez@gmail.com
 harold.gimenez@gmail.com | harold@heroku.com
(3 rows)

select old_email, new_email …
Run Code Online (Sandbox Code Playgroud)

sql postgresql union sql-order-by

6
推荐指数
2
解决办法
1306
查看次数

如何在打字稿中使用yargs解析命令行参数

这是我尝试过的(代码改编自yargs github自述文件中示例的代码):

// main.ts

import {Argv} from "yargs";


console.info(`CLI starter.`);

function serve(port: string) {
    console.info(`Serve on port ${port}.`);
}

require('yargs')
    .command('serve', "Start the server.", (yargs: Argv) => {
        yargs.option('port', {
            describe: "Port to bind on",
            default: "5000",
        }).option('verbose', {
            alias: 'v',
            default: false,
        })
    }, (args: any) => {
        if (args.verbose) {
            console.info("Starting the server...");
        }
        serve(args.port);
    }).argv;
Run Code Online (Sandbox Code Playgroud)

结果:

npm run-script build; node build/main.js --port=432 --verbose

> typescript-cli-starter@0.0.1 build /Users/kaiyin/WebstormProjects/typescript-cli-starter
> tsc -p .

CLI starter.
Run Code Online (Sandbox Code Playgroud)

看起来yargs在这里没有任何作用。

任何想法如何使它工作?

node.js typescript yargs

6
推荐指数
2
解决办法
6279
查看次数

为什么不能对表达式中的R调用对象进行求值?(子集化与从调用对象中提取)

我试图了解R expression对象,但遇到了一些困难.

代码段:

a = 1
b = 2
x = expression(sin(a+b+1))
print(class(x[[1]][2]))
eval(x[[1]][2])
Run Code Online (Sandbox Code Playgroud)

结果:

#////////////////////////////////////////////////////
x = expression(sin(a+b+1))
#////////////////////////////////////////////////////
print(class(x[[1]][2]))
[1] "call"
#////////////////////////////////////////////////////
x = expression(sin(a+b+1))
#////////////////////////////////////////////////////
print(class(x[[1]][2]))
[1] "call"
#////////////////////////////////////////////////////
eval(x[[1]][2])
Error in eval(expr, envir, enclos) : attempt to apply non-function
2: eval(expr, envir, enclos)
1: eval(x[[1]][2])
Run Code Online (Sandbox Code Playgroud)

x[[1]][2]是一个call对象,但为什么不能评估它?

expression r object

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

如何在 emacs 24 中启用电对模式

我已根据此页面上的说明启用了电对模式:http : //ergoemacs.org/emacs/emacs_insert_brackets_by_pair.html,即将这些行添加到 ~/.emacs:

;; autocomplete paired brackets
(electric-pair-mode 1)
Run Code Online (Sandbox Code Playgroud)

但这没有任何效果,我仍然需要M-x electric-pair-mode激活它。为什么会这样?

Emacs 版本是 24.3,在 Mac OSX 上。

这是我的整个 ~/.emacs

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; set up package-archives
(require 'package)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-archives (quote (("gnu" . "http://elpa.gnu.org/packages/") ("marmalade" …
Run Code Online (Sandbox Code Playgroud)

emacs autocomplete

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