在R我可以这样写:
l <- list(a=0, b="10");
Run Code Online (Sandbox Code Playgroud)
并获取名为的元素的值,b如下所示:
x <– l$b
Run Code Online (Sandbox Code Playgroud)
有没有办法通过使用Rcpp::List对象获得相同的结果?
我在使用浮点精度时遇到麻烦 Eigen。
我有两个Eigen::MatrixXd; 第一个矩阵A(nx1)仅包含正整数,而第二个矩阵B(nx1)仅包含一列并填充相同的实数(例如:-0.714312)。
我需要计算以下内容Eigen::MatrixXd:
const auto exponential = [](double x)
{ return std::exp(x); };
MatrixXd W = B.unaryExpr(exponential);
MatrixXd residuals = A - W;
Run Code Online (Sandbox Code Playgroud)
问题是当我打印残差之和时:
cout << residuals.sum();
// output = 6.16951e-06
Run Code Online (Sandbox Code Playgroud)
通过使用R和相同的输入矩阵执行相同的操作,我得到了一个不同的值。
通过使用R矩阵,我得到-2.950208e-09。同时的元素的总和A,B并且W是相同的两个中C++和在R。
我在理解以下代码时遇到一些麻烦:
double a = -1000;
double b = numeric_limits<double>::min();
if (a < b)
{
cout << "why?";
}
Run Code Online (Sandbox Code Playgroud)
输出是:
为什么?
怎么可能-1000低于numeric_limits<double>::min()?
我有一个Eigen::Matrix<double, Dynamic, Dynamic>,我需要检查它的任何元素是否与0不同.
我尝试了以下代码:
Matrix<double, Dynamic, Dynamic> m;
bool f = (m != 0.0).any();
Run Code Online (Sandbox Code Playgroud)
但是我遇到了编译器错误.
二进制表达式的操作数无效('const Eigen :: Matrix'和'double')
我需要使用 Direct2D 创建一个透明位图,并使用我的设备上下文在其上绘制。
ID2D1DeviceContext1* d2dContext = ...
ID2D1Bitmap* pBitmap;
d2dContext->CreateBitmap(
bitmapSize,
nullptr,
0,
D2D1::BitmapProperties1(
D2D1_BITMAP_OPTIONS_TARGET,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
dpiX, dpiY),
&pBitmap);
d2dContext->BeginDraw();
d2dContext->SetTarget(pBitmap);
d2dContext->Clear(D2D1::ColorF(0, 0));
d2dContext->DrawLine(...);
hr = d2dContext->EndDraw();
Run Code Online (Sandbox Code Playgroud)
不幸的是我无法创建任何透明位图。我尝试了几种像素格式组合,包括D2D1_ALPHA_MODE_STRAIGHT,但没有成功。
有什么解决办法吗?
我想将我的类方法作为参数传递给接受函数指针和void*. 下面是一个例子:
#include <functional>
typedef void(*pfnc) (void*);
struct Foo
{
static void static_foo(void*)
{
}
void foo(void*)
{
}
void listner(pfnc f, void* p)
{
f(p);
}
void test()
{
listner(static_foo); // works with static method
auto f = [](void*) {};
listner(f); // works with lambda
std::function<void(void*)> stdf = std::bind(&Foo::foo, this, std::placeholders::_1);
listner(stdf); // does not compile with not static method
}
};
Run Code Online (Sandbox Code Playgroud)
不幸的是我的解决方案无法编译。我必须改变什么?
我从Microsoft的GSL实现(C++指南支持库)中看到了这段代码:
#if defined(__clang__) || defined(__GNUC__)
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define GSL_LIKELY(x) (!!(x))
#define GSL_UNLIKELY(x) (!!(x))
#endif
Run Code Online (Sandbox Code Playgroud)
我读到了__builtin_expect(这里和这里),但我仍然不清楚双boolean否定运算符的目的是什么(!!(x)).为什么用它?
有问题的文件就是这个.
以下代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <utility>
using namespace std;
struct Foo
{
std::string s;
int i;
};
int main()
{
cout << boolalpha << is_nothrow_constructible<Foo>::value << endl;
cout << is_nothrow_constructible<pair<string, int>>::value << endl;
cout << is_nothrow_move_constructible<Foo>::value << endl;
cout << is_nothrow_move_constructible<pair<string, int>>::value << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时产生以下输出g++ -std=c++11:
true
false
true
true
Run Code Online (Sandbox Code Playgroud)
为什么std::pair<string, int>不是建设性的,Foo而是,为什么它不是可移动的可构造的?
我试图理解为什么以下代码无法编译(Playground):
fn inspection<I>(iter: I)
where
I: Iterator,
I::Item: Ord,
{
let inspection = iter
.filter(|x| x > 0);
}
fn main() {
let data = vec![1, 2, 3];
inspection(data.iter()); // or inspection(data.into_iter());
}
Run Code Online (Sandbox Code Playgroud)
错误是:
error[E0308]: mismatched types
--> src/main.rs:9:25
|
9 | .filter(|x| x > 0);
| ^ expected reference, found integral variable
|
= note: expected type `&<I as std::iter::Iterator>::Item`
found type `{integer}`
Run Code Online (Sandbox Code Playgroud)
我尝试按照这里解释的各种替代方法(通过取消引用元素)而没有成功.
第一次尝试:
.filter(|x| **x > 0);
Run Code Online (Sandbox Code Playgroud)
error[E0614]: type `<I as std::iter::Iterator>::Item` cannot …Run Code Online (Sandbox Code Playgroud) 我有一个Vec<Box<dyn Trait>>作为输入,我想将它的元素存储在一个Vec<Rc<RefCell<dyn Trait>>>. 最好的方法是什么?
我试过:
use std::cell::RefCell;
use std::rc::Rc;
trait Trait {}
fn main() {
let mut source: Vec<Box<dyn Trait>> = Vec::new();
let mut dest: Vec<Rc<RefCell<dyn Trait>>> = Vec::new();
for s in source {
let d = Rc::new(RefCell::new(s.as_ref()));
dest.push(d);
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到了错误:
error[E0277]: the trait bound `&dyn Trait: Trait` is not satisfied
--> src/main.rs:12:19
|
12 | dest.push(d);
| ^ the trait `Trait` is not implemented for `&dyn Trait`
|
= note: required for the cast …Run Code Online (Sandbox Code Playgroud)