我在构造函数或类中有一些有用的代码Valuable.我想确定它以前执行过submain.我怎样才能保证它没有经过优化?
int main()
{
// Dear compiler, please don't optimize ctor call out!
Valuable var;
return submain();
}
Run Code Online (Sandbox Code Playgroud)
局部变量是否足够?我需要使用static:
static Valuable *v = new Valuable();
delete v;
v = NULL;
Run Code Online (Sandbox Code Playgroud)
我可以缩短之前的一个班轮:
delete new Valuable();
Run Code Online (Sandbox Code Playgroud) 在Clojure中,我可以有一个序列a..b与(range a b).但据我所知,这是一个懒惰的序列.我可以生成一个列表和/或数字向量a..b吗?
注意:我是Clojure的新手.
Clojure允许二进制函数(每个二进制函数?),特别是,+应用于多个args:
(+ 1 2 3) ; 6
Run Code Online (Sandbox Code Playgroud)
我理解它是如何处理的(在参数列表中减少类似):
(+ (+ 1 2) 3) => (+ 3 3) => 6
Run Code Online (Sandbox Code Playgroud)
让我们考虑关系,比如,<= =等:
(< 1 2 3) ; true
Run Code Online (Sandbox Code Playgroud)
但现在我不明白Clojure是如何对待它的.它不像上面的示例,因为(<1 2)是布尔值,并且与整数的比较是没有意义的:
(< 1 2 3) => (< (< 1 2) 3) => (< true 3) ; bad!
Run Code Online (Sandbox Code Playgroud)
这是不正确的.如果是关系,应该隐藏and在内部:
(< 1 2 3 4) => (and (< 1 2) (< 2 3) (< 3 4))
Run Code Online (Sandbox Code Playgroud)
这是问题所在.他们是如何对待的?我的意思是,对我来说,就像没有统一的功能处理(arg list基本上减少了这个功能)和比较.Clojure是否区分了这些案例?
我想在调用之间缓存方法 ID。为此,我获取方法 ID:
// Put static to clarify it's saved across calls.
static jmethodID method = env->GetMethodID(class_HelloWorld, name, signature);
Run Code Online (Sandbox Code Playgroud)
我的问题是:我是否需要创建class_HelloWorld一个全局引用,NewGlobalRef或者可以从本地引用获取方法 ID 并保存(我使用class_HelloWorld)FindClass,而不需要全局引用类元数据?
假设我们有一个整数列表:1, 2, 5, 13, 6, 5, 7我想找到第一个重复的数字并返回两个索引的向量。在我的示例中,它是 5 at [2, 5]。到目前为止我所做的是loop,但我可以做得更优雅、更短吗?
(defn get-cycle
[xs]
(loop [[x & xs_rest] xs, indices {}, i 0]
(if (nil? x)
[0 i] ; Sequence is over before we found a duplicate.
(if-let [x_index (indices x)]
[x_index i]
(recur xs_rest (assoc indices x i) (inc i))))))
Run Code Online (Sandbox Code Playgroud)
不需要返回数字本身,因为我可以通过索引获取它,其次,它可能并不总是存在。
这是一个显示问题的小程序:
import Data.Fixed
main = do
print x
where
x :: Pico
x = read "12" -- error: no instance for 'Read Pico'
Run Code Online (Sandbox Code Playgroud)
我在Fixed.hs中看到了库GHC源代码 - 有一个Read实例(复制一些代码):
type Pico = Fixed E12
data E12 = E12
instance HasResolution E12 where
resolution _ = 1000000000000
instance (HasResolution a) => Read (Fixed a) where
readsPrec _ = readsFixed
Run Code Online (Sandbox Code Playgroud)
我的推理有什么问题,为什么编译器没有看到Pico是Read的实例?
我想把我的html模板(实际上是JavaScript部分)null或带有电子邮件的字符串:
var email = null;
// or a string:
var email = "somebody@somewhere.com";
Run Code Online (Sandbox Code Playgroud)
但是有了模板
var email = {{.Email}};
Run Code Online (Sandbox Code Playgroud)
我每次都被引用字符串:
var email = "null";
var email = "somebody@somewhere.com";
Run Code Online (Sandbox Code Playgroud)
怎么解决?
编辑:这是我的代码:http://play.golang.org/p/8k4s8dv2PE
您可以看到带有引号的Go环绕字符串并删除注释 - 有预处理或后处理.
如何用零填充双倍数到指定长度?
printf "{:.3}".format(0.12) # Doesn't pad
Run Code Online (Sandbox Code Playgroud)
我想得到
0.120
Run Code Online (Sandbox Code Playgroud) 为什么我的awk脚本
BEGIN {
FS = "][ \t\v]+"
}
# Note space after + in the end of the regex.
NF == 2 && $1 ~ /[:alpha:][:digit:]+ / {
print $1, "<<<";
}
Run Code Online (Sandbox Code Playgroud)
不匹配文件中的任何字符串,如下所示:
I1130 15:18:42.526808 17329 thrift_bridge.cpp:126] AAA
E1130 15:18:42.527042 16076 thrift_bridge.hpp:288] BBB
Run Code Online (Sandbox Code Playgroud)
但是如果我删除空格,则两行都在输出中.