0
点赞
收藏
分享

微信扫一扫

C++高频面试题——内存管理、堆栈、指针

辰鑫chenxin 2024-06-28 阅读 4

文章目录

运算

+,- 加减法

  • 逐元加减法
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;

int main()
{
  Eigen::Matrix2d a;
  a << 1, 2,
       3, 4;
  Eigen::MatrixXd b(2,2);
  b << 10, 20,
       30, 40;
  cout << "a + b =\n" << a + b << endl;
  cout << "a - b =\n" << a - b << endl;
  cout << "Doing a += b;" << endl;
  a += b;
  cout << "Now a =\n" << a << endl;
  Eigen::Vector3d v(1,2,3);
  Eigen::Vector3d w(1,0,0);
  cout << "-v + w - v =\n" << -v + w - v << endl;
}

a + b =
11 22
33 44
a - b =
 -9 -18
-27 -36
Doing a += b;
Now a =
11 22
33 44
-v + w - v =
-1
-4
-6

Process returned 0 (0x0)   execution time : 0.573 s
Press any key to continue.

* / 乘除法

逐元 乘法
1、matrix*scalar
2、scalar*matrix
3、matrix*=scalar
#include <iostream>
#include <Eigen/Dense>

int main()
{
  Eigen::Matrix2d a;
  a << 10, 20,
       30, 40;
  Eigen::Vector3d v(1,2,3);
  std::cout << "a * 0.1 =\n" << a * 0.1 << std::endl;
  std::cout << "0.1 * v =\n" << 10 * v << std::endl;
  std::cout << "Doing v *= 2;" << std::endl;
  v *= 2;
  std::cout << "Now v =\n" << v << std::endl;
}

a * 2.5 =
1 2
3 4
0.1 * v =
10
20
30
Doing v *= 2;
Now v =
2
4
6

Process returned 0 (0x0)   execution time : 0.534 s
Press any key to continue.
逐元 除法
    1、matrix/scalar
   2、matrix/=scalar
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{
  Eigen::Matrix2d a;
  a << 10, 20,
       30, 40;
  Eigen::Vector3f v(1,2,3);
  cout << "a / 5 =\n" << a / 5 << endl;
  cout << "v / 5 =\n" << v /5 << endl;
  cout << "Doing a /= 10;" << endl;
  a /= 10;
  cout << "Now a =\n" << a << endl;
}

a / 5 =
2 4
6 8
v / 5 =
0.2
0.4
0.6
Doing a /= 10;
Now a =
1 2
3 4

Process returned 0 (0x0)   execution time : 0.477 s
Press any key to continue.

逐元综合运算
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{
  Eigen::Matrix2d a;
  a << 10, 20,
       30, 40;
  Eigen::Matrix2d b;
  b << 1, 2,
       3, 4;
  cout << "a *10+ b =\n" << a*10+b << endl;
}

a *10+ b =
101 202
303 404

Process returned 0 (0x0)   execution time : 0.394 s
Press any key to continue.


矩阵乘法与加减法
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{
  Eigen::Matrix2d a;
  a << 10, 20,
       30, 40;
  Eigen::Matrix2d b;
  b << 2, 4,
       8, 16;
  Eigen::Vector2d v(1,2);
  cout << "a * b =\n" << a *b << endl;
  cout << "a * v =\n" << a *v << endl;
  cout << "a + b =\n" << a +b << endl;
  cout << "a - b =\n" << a -b << endl;
}

a * b =
180 360
380 760
a * v =
 50
110
a + b =
12 24
38 56
a - b =
 8 16
22 24

Process returned 0 (0x0)   execution time : 0.429 s
Press any key to continue.

转置、共轭、伴随矩阵

  • 共轭矩阵相关知识

  • 转置矩阵
    将矩阵的行列互换得到的新矩阵称为转置矩阵,转置矩阵的行列式不变。
    在这里插入图片描述

  • 复数:

template <class Type>
class complex
  • 复数矩阵
    typedef Matrix< std::complex< float >, Dynamic, Dynamic > Eigen::MatrixXcf
  • example
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{
  Eigen::Matrix2d a;
  a << 10, 20,
       30, 40;
  Eigen::MatrixXcf b= Eigen::MatrixXcf::Random(2,2);
  cout << "转置:a^T =\n" << a.transpose() << endl;
  cout << "共轭:a conjugate() =\n" << b.conjugate() << endl;
  cout << "伴随矩阵:a adjoint() =\n"<< a.adjoint() << endl;
}

转置:a^T =
10 30
20 40
共轭:a conjugate() =
   (0.127171,0.997497) (-0.0402539,-0.170019)
   (0.617481,0.613392)    (0.791925,0.299417)
伴随矩阵:a adjoint() =
10 30
20 40

Process returned 0 (0x0)   execution time : 0.540 s
Press any key to continue.
  • a = a.transpose()转置并替换使用a.transposeInPlace()
  • a = a.adjoint() 共轭并替换a.adjointInPlace()

点乘法,叉积

  • dot product
  • cross product
  • example
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{
  Eigen::Vector3d v(1,2,3);
  Eigen::Vector3d w(0,1,2);
  cout << "v dot product w =\n" << v.dot(w) << endl;
  cout << "v cross product w =\n" << v.cross(w) << endl;
}

v dot product w =
8
v cross product w =
 1
-2
 1

Process returned 0 (0x0)   execution time : 0.327 s
Press any key to continue.

举报

相关推荐

内存管理面试题

c++面试题

Spark高频面试题

Dubbo高频面试题

高频CSS面试题

【TCP】高频面试题

【vue】高频面试题

0 条评论