欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > C++精解【10】

C++精解【10】

2025/5/3 19:35:29 来源:https://blog.csdn.net/sakura_sea/article/details/140063587  浏览:    关键词:C++精解【10】

文章目录

  • constexpr函数
  • GMP大整数
    • codeblock环境配置
    • 数据类型
    • 函数类
  • Eigen
    • minCoeff 和maxCoeff
    • Array类

constexpr函数

函数可能在编译时求值,则声明它为constexpr,以提高效率。需要使用constexpr告诉编译器允许编译时计算。

constexpr int min(int x, int y) { return x < y ? x : y; }
void test(int v)
{int m1 = min(-1, 2);            // 可能在编译期计算constexpr int m2 = min(-1, 2);  // 编译时计算int m3 = min(-1, v);            // 运行时计算constexpr int m4 = min(-1, v);  // 错误,不能在编译期计算
}
int dcount = 0;
constexpr int double(int v)
{++dcount;   // constexpr 函数无副作用,因为这一行错误return v + v;
}

constexpr函数被隐式地指定为内联函数,此外,constexpr允许递归。

#include <iostream>
constexpr int fac(int n)
{return n > 0 ? n * fac( n - 1 ) : 1;
}
inline int myadd(int x,int y){return x+y;};
int main()
{int n;std::cout<<"请输入阶乘参数:";std::cin>>n;std::cout<<std::endl<<fac(n)<<std::endl;std::cout<<myadd(12,55)<<std::endl;return 0;
}

GMP大整数

codeblock环境配置

在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

数据类型

  • 整型
mpz_t sum;struct foo { mpz_t x, y; };mpz_t vec[20];
  • 有理数
mpq_t quotient;

也是高倍精度分数。

  • 浮点数
mpf_t fp;

浮点函数接受并返回C类型mp_exp_t中的指数。目前,这通常是很长的,但在某些系统上,这是效率的一个指标。

  • 指针
Mpz_ptr用于指向mpz_t中的元素类型的指针
Mpz_srcptr for const指针指向mpz_t中的元素类型
Mpq_ptr用于指向mpq_t中的元素类型的指针
Mpq_srcptr for const指针指向mpq_t中的元素类型
Mpf_ptr用于指向mpf_t中元素类型的指针
Mpf_srcptr for const指针指向mpf_t中的元素类型
指向gmp_randstate_t中元素类型的指针
Gmp_randstate_srcptr for const指针指向gmp_randstate_t中的元素类型

函数类

用于有符号整数算术的函数,其名称以mpz_开头。关联类型为mpz_t。这门课大约有150个函数
用于有理数算术的函数,其名称以mpq_开头。关联类型为mpq_t。这门课大约有35个函数,但整数函数可以分别对分子和分母进行算术运算。
用于浮点运算的函数,其名称以mpf_开头。关联类型为mpf_t。这门课大约有70个函数。
对自然数进行操作的快速低级函数。这些由前面组中的函数使用,您也可以从时间要求非常严格的用户程序中直接调用它们。这些函数的名称以mpn_开头。关联类型为mp_limb_t数组。这个类中大约有60个(难以使用的)函数。
各种各样的功能。设置自定义分配的函数和生成随机数的函数。

Eigen

minCoeff 和maxCoeff

不带参数时,返回最小元素和最大元素,带参数时,返回元素所在坐标

#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
using namespace Eigen;
int main(){Matrix2d m {{1,2},{3,4}};std::ptrdiff_t i, j;int minOfM = m.minCoeff(&i,&j);cout << "Here is the matrix m:\n" << m << endl;cout << "Its minimum coefficient (" << minOfM<< ") is at position (" << i << "," << j << ")\n\n";int maxOfM= m.maxCoeff(&i,&j);cout << "Its maximum coefficient (" << maxOfM<< ") is at position (" << i << "," << j << ")\n\n";RowVector4i v = RowVector4i::Random();int maxOfV = v.maxCoeff(&i);cout << "Here is the vector v: " << v << endl;cout << "Its maximum coefficient (" << maxOfV<< ") is at position " << i << endl;int minOfV = v.minCoeff(&j);cout << "Its minimum coefficient (" << minOfV<< ") is at position " << j << endl;}
Here is the matrix m:
1 2
3 4
Its minimum coefficient (1) is at position (0,0)Its maximum coefficient (4) is at position (1,1)Here is the vector v:  730547559 -226810938  607950953  640895091
Its maximum coefficient (730547559) is at position 0
Its minimum coefficient (-226810938) is at position 1Process returned 0 (0x0)   execution time : 0.305 s
Press any key to continue.

Array类

Array类提供了通用数组,而Matrix类则用于线性代数。此外,Array类提供了一种简单的方法来执行系数操作,这种操作可能没有线性代数意义,比如向数组中的每个系数添加一个常数,或者对两个数组进行系数乘。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词