- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
vector y_long_name
有 100 个成员,我打算只操作它的一个由前三个成员组成的子 vector 。 subvector
函数在我的代码中用作左值:
y_long_name.subvec(0,2)=A*x1;
但是 C++ 不将其识别为左值。如何定义要使用的 yr
而不是 y_long_name.subvec(0,2)
?
我想做的是使用短格式 yr
而不是 y_long_name.subvec(0,2)
但不使用宏。
#include <iostream>
#include <armadillo>
int main()
{
const arma::vec::fixed<3> x1={1.2,3.5,-0.27};
arma::mat::fixed<3,3> A={{5,3,6},{8,2,11},{7,5,9}};
arma::vec::fixed<3> y1=A*x1; // ok
arma::vec::fixed<100> y_long_name;
y_long_name.subvec(0,2)=A*x1; // ok
arma::vec::fixed<3>& yr=y_long_name.subvec(0,2);
yr=A*x1;
y_long_name.print();
return 0;
}
错误:
test.cpp: In function ‘int main()’:
test.cpp:14:48: error: invalid initialization of non-const reference of type ‘arma::Col<double>::fixed<3ull>&’ from an rvalue of type ‘arma::subview_col<double>’
arma::vec::fixed<3>& yr=y_long_name.subvec(0,2)
最佳答案
看起来您正在尝试避免使用 subviews同时仍在访问 subview 。通常不建议这样做。但是,有一种不安全的方法可以实现这一点,即使用带有指向 external memory 指针的 vector 构造函数。 .例如:
vec a(10, fill::zeros);
double* a_data = a.memptr();
vec b( &a_data[5], 2, false, true ); // create 'b from part of vector 'a'
b(0) = 123.0; // changing elements in vector 'b' will also change 'a'
a.print("a:");
请注意,上述操作不安全,因为分配给“a”使用的内存可以通过应用于“a”的任何操作来释放和重新分配。然后, vector “b”将使用指向无效内存的指针。换句话说,仅当您真正知道自己在做什么时才使用上述技巧。
关于c++ - 从 arma::subvec 类型的右值初始化类型的非常量引用无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574338/
我正在尝试从 2 元素列表中创建 map Subvec s。 这在 Clojure 中运行良好: (into {} (list (subvec [1 2 3] 1))) >> {2 3} 但在 Clo
vector y_long_name 有 100 个成员,我打算只操作它的一个由前三个成员组成的子 vector 。 subvector 函数在我的代码中用作左值: y_long_name.subve
所以我有 (def a (vec (range 1000000))) (def b (vec (range 100000))) 并取两个向量的其余部分 (time (dorun (subvec b 1
如果我有一个 vec,我如何搜索它以查找它是否包含另一个 vec - 并返回此 subvec 开始的索引? let mut haystack = vec!(0, 0, 0, 1, 48, 120, 4
我是一名优秀的程序员,十分优秀!