每次我看一个C函数指针,我的眼睛都会茫然.我看不懂.
从这里,这里有两个函数指针TYPEDEFS的例子:
typedef int (*AddFunc)(int,int);
typedef void (*FunctionFunc)();
Run Code Online (Sandbox Code Playgroud)
现在我习惯了这样的事情:
typedef vector<int> VectorOfInts ;
Run Code Online (Sandbox Code Playgroud)
我读到的是
typedef vector<int> /* as */ VectorOfInts ;
Run Code Online (Sandbox Code Playgroud)
但我无法阅读上面的2个typedef.包围和星号放置,这是不合逻辑的.
为什么*旁边的单词AddFunc ..?
所以,当我尝试声明一个大于10000x10000的矩阵时,我发现Eigen包崩溃了.我需要声明一个这样的矩阵..可靠地约13000x13000个元素.我跑了一个测试:
for( int tortureEigen = 1 ; tortureEigen < 50000 ; tortureEigen++ )
{
printf( "Torturing Eigen with %dx%d..\n", tortureEigen, tortureEigen ) ;
Eigen::MatrixXd m( tortureEigen, tortureEigen ) ;
}
Run Code Online (Sandbox Code Playgroud)
在我的机器(6 GB RAM)上以14008个元素崩溃.
我有点失望!我认为Eigen就像MATLAB或octave,不应该使用更大的数组崩溃,即使它确实击中了磁盘或其他东西.
而且当我运行此测试并保持TaskMan打开时,创建这些矩阵的过程甚至不会使用那么多内存.TaskMan报告的使用量低于2k.
使用Eigen 2.0.15稳定释放
我正在评估一个简单的函数:
y = (2*x)/sqrt( 1 + x.^2 );
Run Code Online (Sandbox Code Playgroud)
其中x包含大约100个值的向量在哪里.但是,y在这种情况下,MATLAB 等于单个标量值.如果我做:
y = 2*x;
Run Code Online (Sandbox Code Playgroud)
我y按预期得到了一个值向量.如果我做:
y = x.^2;
Run Code Online (Sandbox Code Playgroud)
我也y按预期得到了一个值向量.
为什么上面的等式y = (2*x)/sqrt( 1 + x.^2 );给出单个值而不是值向量?
我可以在同一个文件中使用MATLAB脚本代码和功能代码吗?
%% SAVED IN FILE myfunc.m (otherwise fail)
function [out1] = myfunc( x )
out1 = sqrt( 1 + (cos(x))^2 );
end
%%
%OTHER CRAP
y = 1:10
% use myfunc
Run Code Online (Sandbox Code Playgroud)
即使使用end关键字,它似乎也不起作用.这种类型的东西是允许的还是我总是需要在自己正确命名的文件中使用EACH功能?
我确信几年前我看到了在同一个文件中使用这些函数的函数和代码.
可能重复:
未定义的行为和序列点
在机器代码级别的C++中,postincrement ++运算符何时执行?
所述优先级表表示后缀++运算符是2级:这意味着在
int x = 0 ;
int y = x++ + x++ ; // ans: y=0
Run Code Online (Sandbox Code Playgroud)
后缀++ 首先执行.
但是,这条线的逻辑运算似乎是首先添加(0 + 0),但是这是怎么回事?
我想象的是以下内容:
// Option 1:
// Perform x++ 2 times.
// Each time you do x++, you change the value of x..
// but you "return" the old value of x there?
int y = 0 + x++ ; // x becomes 1, 0 is "returned" from x++
// do it for the …Run Code Online (Sandbox Code Playgroud) 有关:
我可以这样做吗?
template <int N> union Vector
{
float e[ N ] ;
// If N is 3, define x,y,z components
#if N==3
struct { float x,y,z ; } ;
#elif N==2
struct { float x,y ; } ;
#endif
} ;
// use
int main()
{
Vector<2> xy ;
xy.e[ 0 ] = 5 ;
xy.e[ 1 ] = 2 ;
xy.x = 2 ;
Vector<3> xyz ;
xyz.z = 4 ;
}
Run Code Online (Sandbox Code Playgroud) 可能的重复:
是否可以编写 C++ 模板来检查函数是否存在?
在 JavaScript 等语言中,您可以检查属性是否存在
// javascript
if( object['property'] ) // do something
Run Code Online (Sandbox Code Playgroud)
在 C++ 中,我想根据类型T是否具有特定属性来条件编译。这可能吗?
template <typename T>
class IntFoo
{
T container ;
public:
void add( int val )
{
// This doesn't work, but it shows what I'm trying to do.
// if the container has a .push_front method/member, use it,
// otherwise, use a .push_back method.
#ifdef container.push_front
container.push_front( val ) ;
#else
container.push_back( val ) ;
#endif
}
void print()
{ …Run Code Online (Sandbox Code Playgroud) 如果一个向量等于另一个向量,并且向量不相等,我想设置一个float值1.00.0
if( v1 == v2 ) floatVal = 1.0 ;
else floatVal = 0.0 ;
Run Code Online (Sandbox Code Playgroud)
但它不是"更快"或只是设置优化
floatVal = (v1 == v2) ;
Run Code Online (Sandbox Code Playgroud)
但它不起作用.你不能隐式(或明确地)将float转换为bool?有没有办法做到这一点,还是我必须使用if语句分支?
我正在尝试使用std::set我将抛出一堆边缘的地方,并且只保留唯一的边缘.
An Edge是两个(整数索引)节点之间的一条线.边缘(1,2)==(2,1),因为这些边缘是无向的.
不过,我遇到了一个令人费解的情况.在//??下面的代码中标记的部分,行为不是我所期望的.
运行此代码的结果只保留2个边,(1,2)和(4,8).(2,1)被集合丢弃,但除非我激活注释掉的部分,否则它不应该被删除!这里发生了什么?//|| ( A==o.B && B==o.A )operator==
这个set<Edge>实现让我觉得......前卫.
#include <stdio.h>
#include <set>
using namespace std ;
struct Edge
{
int A,B ;
Edge( int iA, int iB ) : A(iA), B(iB) {}
bool operator==( const Edge & o ) const {
//??
return ( A==o.A && B==o.B ) ;//|| ( A==o.B && B==o.A ) ;
}
bool operator<( const Edge& o ) const {//MUST …Run Code Online (Sandbox Code Playgroud)