小编lim*_*imp的帖子

使用std :: accumulate,得到"太多参数"错误

std::accumulate应该能够采取三个或四个参数.在前一种情况下,只是当你想在容器中添加数字时; 在后一种情况下,当你想先应用一个函数然后添加它们时.我已经编写了生成随机双精度矢量的代码,然后对它们做了一些事情:首先它使用x-> x ^ 2变换std::transform,然后将它们相加std::accumulate,最后将这两个动作组合成一个使用四个-argument版本std::accumulate.

一切都有效,除了第3步.查看http://www.cplusplus.com/reference/numeric/accumulate/上的示例代码,我看不出为什么这不起作用的原因,但我'在编译时得到"太多的参数错误"(我正在使用XCode.由于某种原因,它没有告诉我行号,但我已将其缩小到第二次使用std::accumulate).任何见解?

#include <numeric>
#include <time.h>
#include <math.h>
using std::vector;
using std::cout;
using std::endl;

double square(double a) {
    return a*a;
}

void problem_2_1() {
    vector<double> original;

    //GENERATE RANDOM VALUES
    srand((int)time(NULL));//seed the rand function to time
    for (int i=0; i<10; ++i) {
        double rand_val = (rand() % 100)/10.0;
        original.push_back(rand_val);
        cout << rand_val << endl;
    }

    //USING TRANSFORM        
    vector<double> squared;
    squared.resize(original.size());

    std::transform(original.begin(), original.end(), squared.begin(), square);

    for (int …
Run Code Online (Sandbox Code Playgroud)

c++ accumulate

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

如何在同一行中分隔多个正则表达式匹配

我正在尝试使用正则表达式来解析以某种(key###value)格式编写的值。该值始终是一个数字。不管怎样,我认为用 python 来做是最简单的,所以这里是我正在尝试的一些代码:

import re

line = "(text 1###123)(text 2###345)";

matchObj = re.match( r'\(.*###[0-9]+\)', line)

if matchObj:
   print matchObj.group(0) # produces (text 1###123)(text 2###345)
   # print matchObj.group(1) # gives an error
else:
   print "No match!!"
Run Code Online (Sandbox Code Playgroud)

尽管有两个不同的对象与我编写的正则表达式匹配,但 python 将它们作为单个字符串返回给我——这不是我想要的。我怎样才能解决这个问题?

事实上,我真正想要的是将字符串分成类似["text 1", "123", "text 2", "345]. 因此,如果有人有一个简单的方法来实现这一点,我也将不胜感激。

python regex regex-group

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

Rust"extern mod std;" 失败

我开始了"为rubyists生锈"教程,这段代码让我感到惊讶:

extern mod std;

#[test]
fn this_tests_code() {
    println("")
}

fn main() {
}
Run Code Online (Sandbox Code Playgroud)

试图编译这个给了我:

> rustc testing.rs
testing.rs:1:0: 1:0 error: multiple matching crates for `core`
testing.rs:1 extern mod std;
             ^
note: candidates:
note: path: /usr/local/lib/rustc/x86_64-apple-darwin/lib/libcore-c3ca5d77d81b46c1-0.5.dylib
note: meta: name = "core"
note: meta: vers = "0.5"
note: meta: uuid = "c70c24a7-5551-4f73-8e37-380b11d80be8"
note: meta: url = "https://github.com/mozilla/rust/tree/master/src/libcore"
note: path: /usr/local/lib/rustc/x86_64-apple-darwin/lib/libcore-c3ca5d77d81b46c1-0.6.dylib
note: meta: name = "core"
note: meta: vers = "0.6"
note: meta: uuid = "c70c24a7-5551-4f73-8e37-380b11d80be8"
note: meta: url = "https://github.com/mozilla/rust/tree/master/src/libcore"
error: …
Run Code Online (Sandbox Code Playgroud)

rust

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

Rust"错误:移出不可变的字段"

我创建了以下Rust结构:

struct HTTPRequestHeader {
    name: ~str,
    data: ~str,
    next: Option<~HTTPRequestHeader>
}
Run Code Online (Sandbox Code Playgroud)

以下代码打印出来:

fn print_headers(hdr: &HTTPRequestHeader) {
    println(fmt!("%s: %s", (*hdr).name, (*hdr).data));
    match (*hdr).next {
        Some(next_hdr) => { print_headers(next_hdr); }
        None => { }
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试编译此代码,我收到以下错误:

> rustc http_parse.rs
http_parse.rs:37:7: 37:18 error: moving out of immutable field
http_parse.rs:37    match (*hdr).next {
                          ^~~~~~~~~~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)

这是什么意思?包含(*hdr).name, (*hdr).data)编译而没有错误的行.该match声明不应该试图变异hdr.next以任何方式,所以我不明白怎么不变性进入它.我试图编写一个不带指针的修改版本:

fn print_headers(hdr: HTTPRequestHeader) {
    println(fmt!("%s: %s", hdr.name, hdr.data));
    match hdr.next {
        Some(next_hdr) => { print_headers(*next_hdr); …
Run Code Online (Sandbox Code Playgroud)

immutability rust

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

Docx:似乎无法获得要呈现的项目符号列表

我正在使用 XML 从头开始​​构建 DocX 文档。我有一个非常简单的目标:创建一个项目符号列表,就像ulHTML 中的a 一样。阅读编号列表WordProcessingML 规范(第 2.9 节),我创建了我认为可以满足此要求的内容。这是我的numbering.xml样子:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:numbering xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
  <w:abstractNum w:abstractNumId="1">
    <w:multiLevelType w:val="singleLevel"/>
    <w:lvl w:ilvl="0">
      <w:start w:val="1"/>
      <w:numFmt w:val="bullet"/>
      <w:lvlText w:val="&#8226;"/>
      <w:lvlJc w:val="left"/>
      <w:pPr>
        <w:tabs>
          <w:tab w:pos="360" w:val="num"/>
        </w:tabs>
        <w:ind w:hanging="360" w:left="360"/>
      </w:pPr>
      <w:rPr>
        <w:rFonts w:ascii="Symbol" w:hAnsi="Symbol" w:hint="default"/>
      </w:rPr>
    </w:lvl>
  </w:abstractNum>
  <w:num w:numId="1">
    <w:abstractNumId w:val="1"/>
  </w:num>
</w:numbering>
Run Code Online (Sandbox Code Playgroud)

所以我有一个 id 为 1 的抽象编号,定义为带有一些缩进的单个级别和一个项目符号作为 lvlText(符号&#8226;呈现为 …

docx wordprocessingml

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

如何在emacs中创建一个新的,未命名的文件?

这是一个非常简单的请求:我想创建一个新的空白文件而不给它一个名字(还).我可以使用暂存缓冲区,但只有一个.我可以C-x C-f打开一个新文件,但是我必须给它一个名字和路径.如果我只是给自己写笔记或草拟想法,我不想给它起个名字.如何创建一个新的空的未命名文件?

emacs

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

为什么coffeescript会覆盖我的下划线?

我编写了以下代码,旨在将字符串连接到一个URL.

_ = require 'underscore'

exports.joinUrl = (start, rest...) ->
  for item in rest
    if _.last start is '/'
      if _.first item is '/'
        start += item[1..]
      else
        start += item
    else
      if _.first item is '/'
        start += item
      else
        start += '/' + item
  start
Run Code Online (Sandbox Code Playgroud)

当我启动coffeescript repl时,会发生一件非常奇怪的事情:

> _ = require 'underscore'
[snipped]
> {joinUrl} = require './joinurl'
{ joinUrl: [Function] }
> _
{ joinUrl: [Function] }
Run Code Online (Sandbox Code Playgroud)

咦?不知何故导入joinUrl是覆盖变量的定义_.即使(a)coffeescript将上面粘贴的模块包装到一个函数中,因此变量的任何使用_都不应该影响外部范围,并且(b)在该代码中的任何一点我都不做任何赋值_,除了require …

javascript coffeescript

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

有argparse收集,但没有响应,标志

我有一个脚本,它接受一些参数,使用其中一些参数来选择要运行的脚本,并将其余参数传递给该脚本.所以它看起来像这样:

parser = ArgumentParser()
parser.add_argument('script', choices['a', 'b'])
parser.add_argument('rest_args', nargs='*')
args = parser.parse_args()
if args.script == 'a':
    subprocess.call('python a.py %s' % ' '.join(args.rest_args))
else:
    subprocess.call('python b.py %s' % ' '.join(args.rest_args))
Run Code Online (Sandbox Code Playgroud)

这工作正常,除非我想传入以...开头的参数-.例如,如果我打电话python my_script.py a --foo,我会得到一个错误unrecognized arguments,当我真的想让它运行时python a.py --foo(即只是传递--foo给子进程).

有没有办法解决这个问题argparse

python argparse

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

如何在D中的堆上创建一个字符串?

我在D中编写一个trie,我希望每个trie对象都有一个指向某些数据的指针,如果该节点是trie中的终端节点,则该指针具有非NULL值,否则为NULL.在创建trie之前,数据的类型是不确定的(在C中,这将使用a完成void *,但我计划用模板完成),这是为什么需要指向堆对象的原因之一.

这需要我最终在堆上创建我的数据,此时trie节点可以指向它.尝试,似乎new执行此任务,就像在C++中一样.但是由于某种原因,这会因字符串而失败.以下代码有效:

import std.stdio;

void main() {
    string *a;
    string b = "hello";
    a = &b;
    writefln("b = %s, a = %s, *a = %s", b, a, *a);
}
/* OUTPUT:
b = hello, a = 7FFF5C60D8B0, *a = hello
*/
Run Code Online (Sandbox Code Playgroud)

但是,这失败了:

import std.stdio;

void main() {
    string *a;
    a = new string();
    writefln("a = %s, *a = %s", a, *a);
}
/* COMPILER FAILS WITH:
test.d(5): Error: new can only create structs, dynamic arrays …
Run Code Online (Sandbox Code Playgroud)

memory-management d

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