当前位置: 首页 > news >正文

C++相关闲碎记录(14)

1、数值算法

(1)运算后产生结果accumulate()
#include "algostuff.hpp"using namespace std;int main() {vector<int> coll;INSERT_ELEMENTS(coll, 1, 9);PRINT_ELEMENTS(coll);cout << "sum: " << accumulate(coll.cbegin(), coll.cend(), 0) << endl;cout << "sum: " << accumulate(coll.cbegin(), coll.cend(), -100) << endl;cout << "product: " << accumulate(coll.cbegin(), coll.cend(), 1, multiplies<int>()) << endl;cout << "product: " << accumulate(coll.cbegin(), coll.cend(), 0, multiplies<int>()) << endl;return 0;
}
输出:
1 2 3 4 5 6 7 8 9 
sum: 45
sum: -55 
product: 362880  这里是累称的结果
product: 0
(2)计算两数列的内积inner_product()
#include "algostuff.hpp"
using namespace std;int main() {list<int> coll;INSERT_ELEMENTS(coll, 1, 6);PRINT_ELEMENTS(coll);// 0 + 1*1 + 2*2 + 3*3+4*4 + 5*5+6*6cout << "innser product: " << inner_product(coll.cbegin(), coll.cend(),coll.cbegin(),0) << endl;// 0 + 1*6 + 2*5 + 3 * 4 + 4*3+5*2+6*1cout << "inner_product: " << inner_product(coll.cbegin(), coll.cend(),coll.crbegin(),0) << endl; // 1 * 1+1 * 2+2 * 3+3 * 4+4 * 5+5 * 6+6cout << "product of sums: " << inner_product(coll.cbegin(), coll.cend(),    // first rangecoll.cbegin(),                 // second range1,                             // initial valuemultiplies<int>(),             // outer operationplus<int>())                   // inner operation<< endl;return 0;
}
输出:
1 2 3 4 5 6 
innser product: 91
inner_product: 56
product of sums: 46080
(3)相对数列和绝对数列之间的转换partial_sum()
#include "algostuff.hpp"
using namespace std;int main() {vector<int> coll;INSERT_ELEMENTS(coll, 1, 6);PRINT_ELEMENTS(coll);partial_sum(coll.cbegin(), coll.cend(), ostream_iterator<int>(cout, " "));cout << endl;partial_sum(coll.cbegin(), coll.cend(), ostream_iterator<int>(cout, " "),multiplies<int>());cout << endl;return 0;
}
1 2 3 4 5 6 
1 3 6 10 15 21
1 2 6 24 120 720
(4)将绝对值转换成相对值adjacent_difference()
#include "algostuff.hpp"
using namespace std;int main() {deque<int> coll;INSERT_ELEMENTS(coll, 1, 6);PRINT_ELEMENTS(coll);adjacent_difference(coll.cbegin(), coll.cend(),ostream_iterator<int>(cout, " "));cout << endl;adjacent_difference(coll.cbegin(), coll.cend(),ostream_iterator<int>(cout, " "),plus<int>());cout << endl;adjacent_difference(coll.cbegin(), coll.cend(), ostream_iterator<int>(cout, " "),multiplies<int>());cout << endl;return 0;
}
输出:
1 2 3 4 5 6 
1 1 1 1 1 1
1 3 5 7 9 11
1 2 6 12 20 30
#include "algostuff.hpp"
using namespace std;int main() {vector<int> coll = {17, -3, 22, 13, 13, -9};PRINT_ELEMENTS(coll, "coll: ");adjacent_difference(coll.cbegin(), coll.cend(),coll.begin());PRINT_ELEMENTS(coll, "relative: ");partial_sum(coll.cbegin(), coll.cend(),coll.begin());PRINT_ELEMENTS(coll, "absolute: ");return 0;   
}
输出:
coll: 17 -3 22 13 13 -9 
relative: 17 -20 25 -9 0 -22
absolute: 17 -3 22 13 13 -9

2、stack堆栈

#include <iostream>
#include <stack>
using namespace std;int main()
{stack<int> st;// push three elements into the stackst.push(1);st.push(2);st.push(3);// pop and print two elements from the stackcout << st.top() << ' ';st.pop();cout << st.top() << ' ';st.pop();// modify top elementst.top() = 77;// push two new elementsst.push(4);st.push(5);// pop one element without processing itst.pop();// pop and print remaining elementswhile (!st.empty()) {cout << st.top() << ' ';st.pop();}cout << endl;
}
输出:
3 2 4 77 

自定义stack 类

#ifndef STACK_HPP
#define STACK_HPP#include <deque>
#include <exception>template <typename T>
class Stack {
protected:std::deque<T> c;
public:class ReadEmptyStack : public std::exception {public:virtual const char* what() const throw() {return "read empty stack";}};typename std::deque<T>::size_type size() const {return c.size();}bool empty() const {return c.empty();}void push(const T& elem) {c.push_back(elem);}T pop() {if (c.empty()) {throw ReadEmptyStack();}T elem(c.back());c.pop_back();return elem;}T& top() {if (c.empty()) {throw ReadEmptyStack();}return c.back();}};#endif
#include <iostream>
#include <exception>
#include "Stack.hpp"using namespace std;
int main() {try {Stack<int> st;st.push(1);st.push(2);st.push(3);cout << st.pop() << " ";cout << st.pop() << " ";st.top() = 77;st.push(4);st.push(5);st.pop();cout << st.pop() << " ";cout << st.pop() << endl;cout << st.pop() << endl;} catch (const exception& e) {cerr << "EXCEPTION: " << e.what() << endl;}return 0;
}
输出:
3 2 4 77
EXCEPTION: read empty stack

3、queue队列

自定义queue

#ifndef QUEUE_HPP
#define QUEUE_HPP#include <deque>
#include <exception>template <typename T>
class Queue {
protected:std::deque<T> c;
public:class ReadEmptyQueue : public std::exception {public:virtual const char* what() const throw() {return "read empty queue";}};typename std::deque<T>::size_type size() const {return c.size();}bool empty() const {return c.empty();}void push(const T& elem) {c.push_back(elem);}T pop() {if (c.empty()) {throw ReadEmptyQueue();}T elem(c.front());c.pop_front();return elem;}T& fron() {if (c.empty()) {throw ReadEmptyQueue();}return c.front();}
};#endif
#include <iostream>
#include <string>
#include <exception>
#include "Queue.hpp"      // use special queue class
using namespace std;int main()
{try {    Queue<string> q;// insert three elements into the queueq.push("These ");q.push("are ");q.push("more than ");// pop two elements from the queue and print their valuecout << q.pop();cout << q.pop();// push two new elementsq.push("four ");q.push("words!");// skip one elementq.pop();// pop two elements from the queue and print their valuecout << q.pop();cout << q.pop() << endl;// print number of remaining elementscout << "number of elements in the queue: " << q.size()<< endl;// read and print one elementcout << q.pop() << endl;}catch (const exception& e) {cerr << "EXCEPTION: " << e.what() << endl;}
}
输出:
These are four words!
number of elements in the queue: 0
EXCEPTION: read empty queue

4、priority queue 带优先级的队列

namespace std {template <typename T, typename Container = vector<T>,typename Compare = less<typename Container::value_typy>>class priority_queue {protected:Compare comp;Container c;public:explicit priority_queue(const Compare& cmp = Compare(),const Container& cont = Container()):comp(cmp),c(cont) {make_heap(c.begin(), c.end(), comp);}void push(const value_type& x) {c.push_back(x);push_heap(c.begin(), c.end(), comp);}void pop() {pop_heap(c.begin(), c.end(), comp);c.pop_back();}bool empty() const {return c.empty();}size_type size() const {return c.size();}const value_type& top() const {return c.front();}...};
}

priority_queue()内部使用的heap相关算法。

5、bitset

#include <bitset>
#include <iostream>
using namespace std;int main()
{// enumeration type for the bits// - each bit represents a colorenum Color { red, yellow, green, blue, white, black, //...,numColors };// create bitset for all bits/colorsbitset<numColors> usedColors;// set bits for two colorsusedColors.set(red);usedColors.set(blue);// print some bitset datacout << "bitfield of used colors:   " << usedColors << endl;cout << "number   of used colors:   " << usedColors.count() << endl;cout << "bitfield of unused colors: " << ~usedColors << endl;// if any color is usedif (usedColors.any()) {// loop over all colorsfor (int c = 0; c < numColors; ++c) {// if the actual color is usedif (usedColors[(Color)c]) {//...}}}
}
#include <bitset>
#include <iostream>
#include <string>
#include <limits>
using namespace std;int main()
{// print some numbers in binary representationcout << "267 as binary short:     "<< bitset<numeric_limits<unsigned short>::digits>(267)<< endl;cout << "267 as binary long:      "<< bitset<numeric_limits<unsigned long>::digits>(267)<< endl;cout << "10,000,000 with 24 bits: "<< bitset<24>(1e7) << endl;// write binary representation into stringstring s = bitset<42>(12345678).to_string();cout << "12,345,678 with 42 bits: " << s << endl;// transform binary representation into integral numbercout << "\"1000101011\" as number:  "<< bitset<100>("1000101011").to_ullong() << endl;
}
输出:
267 as binary short:     0000000100001011
267 as binary long:      00000000000000000000000100001011
10,000,000 with 24 bits: 100110001001011010000000
12,345,678 with 42 bits: 000000000000000000101111000110000101001110
"1000101011" as number:  555

相关文章:

C++相关闲碎记录(14)

1、数值算法 &#xff08;1&#xff09;运算后产生结果accumulate() #include "algostuff.hpp"using namespace std;int main() {vector<int> coll;INSERT_ELEMENTS(coll, 1, 9);PRINT_ELEMENTS(coll);cout << "sum: " << accumulate(…...

18、vue3(十八):菜单权限,按钮权限,打包,发布nginx

目录 一、菜单权限和路由拆分 1.思路分析 2.深拷贝插件 3.代码实现 4.效果展示...

04 在Vue3中使用setup语法糖

概述 Starting from Vue 3.0, Vue introduces a new syntactic sugar setup attribute for the <script> tag. This attribute allows you to write code using Composition API (which we will discuss further in Chapter 5, The Composition API) in SFCs and shorte…...

vite+ts——user.ts——ts接口定义+axios请求的写法

import axios from axios; import qs from query-string; import {UserState} from /store/modules/user/types;export interface LoginData{username:string;password:string;grant_type?:string;scope?:string;client_id?:string;client_secret?:string;response_type?:…...

环境搭建及源码运行_java环境搭建_mysql安装

书到用时方恨少、觉知此时要躬行&#xff1b;拥有技术&#xff0c;成就未来&#xff0c;抖音视频教学地址&#xff1a;​​​​​​​​​​​​​​ 1、介绍 MySQL是一个关系型数据库管理系统&#xff0c;由瑞典MySQL AB 公司开发&#xff0c;属于 Oracle旗下产品。MySQL是最…...

Android camera的metadata

一、实现 先看一下metadata内部是什么样子&#xff1a; 可以看出&#xff0c;metadata 内部是一块连续的内存空间。 其内存分布大致可概括为&#xff1a; 区域一 &#xff1a;存 camera_metadata_t 结构体定义&#xff0c;占用内存 96 Byte 区域二 &#xff1a;保留区&#x…...

ElasticSearch面试题

1.介绍下es的架构&#xff1f; es采用的是分布式的架构&#xff0c;es集群中会有多个结点&#xff0c;而结点的角色主要有下面几种。 协调结点&#xff1a; 请求路由能力&#xff0c;将请求内容将请求转发给对应的结点进行处理。 master结点&#xff1a; 结点管理&#xff…...

C++ 数据结构知识点合集-C/C++ 数组允许定义可存储相同类型数据项的变量-供大家学习研究参考

#include <iostream> #include <cstring>using namespace std;// 声明一个结构体类型 Books struct Books {char title[50];char author[50];char subject[100];int book_id; };int main( ) {Books Book1; // 定义结构体类型 Books 的变量 Book1Books …...

【机器学习】5分钟掌握机器学习算法线上部署方法

5分钟掌握机器学习算法线上部署方法 1. 三种情况2. 如何转换PMML,并封装PMML2.1 什么是PMML2.2 PMML的使用方法范例3. 各个算法工具的工程实践4. 只用Linux的Shell来调度模型的实现方法5. 注意事项参考资料本文介绍业务模型的上线流程。首先在训练模型的工具上,一般三个模型训…...

Vue3-21-组件-子组件给父组件发送事件

情景描述 【子组件】中有一个按钮&#xff0c;点击按钮&#xff0c;触发一个事件&#xff0c; 我们希望这个事件的处理逻辑是&#xff0c;给【父组件】发送一条消息过去&#xff0c; 从而实现 【子组件】给【父组件】通信的效果。这个问题的解决就是 “发送事件” 这个操作。 …...

[密码学]AES

advanced encryption standard&#xff0c;又名rijndael密码&#xff0c;为两位比利时数学家的名字组合。 分组为128bit&#xff0c;密钥为128/192/256bit可选&#xff0c;对应加密轮数10/12/14轮。 基本操作为四种&#xff1a; 字节代换&#xff08;subBytes transformatio…...

CentOS 7 部署pure-ftp

文章目录 &#xff08;1&#xff09;简介&#xff08;2&#xff09;准备工作&#xff08;3&#xff09;更新系统&#xff08;4&#xff09;安装依赖环境&#xff08;5&#xff09;下载和解压pure-ftp源码包&#xff08;6&#xff09;编译和安装pure-ftp&#xff08;7&#xff0…...

Vue2-动态组件案例

1.component介绍 说明&#xff1a; Type: string | ComponentDefinition | ComponentConstructor Explanation: String: 如果你传递一个字符串给 is&#xff0c;它会被视为组件的名称&#xff0c;用于动态地渲染不同类型的组件。这是一个在运行时动态切换组件类型的常见用例。…...

【源码】车牌检测+QT界面+附带数据库

目录 1、基本介绍2、基本环境3、核心代码3.1、车牌识别3.2、车牌定位3.3、车牌坐标矫正 4、界面展示4.1、主界面4.2、车牌检测4.3、查询功能 5、演示6、链接 1、基本介绍 本项目采用tensorflow&#xff0c;opencv&#xff0c;pyside6和pymql编写&#xff0c;pyside6用来编写UI界…...

实战1-python爬取安全客新闻

一般步骤&#xff1a;确定网站--搭建关系--发送请求--接受响应--筛选数据--保存本地 1.拿到网站首先要查看我们要爬取的目录是否被允许 一般网站都会议/robots.txt目录&#xff0c;告诉你哪些地址可爬&#xff0c;哪些不可爬&#xff0c;以安全客为例子 2. 首先测试在不登录的…...

光栅化渲染:可见性问题和深度缓冲区算法

在前面第二章中&#xff0c;我们了解到&#xff0c;在投影点&#xff08;屏幕空间中的点&#xff09;的第三个坐标中&#xff0c;我们存储原始顶点 z 坐标&#xff08;相机空间中点的 z 坐标&#xff09;&#xff1a; 当一个像素与多个三角形重叠时&#xff0c;查找三角形表面上…...

docker入门小结

docker是什么&#xff1f;它有什么优势&#xff1f; 快速获取开箱即用的程序 docker使得所有的应用传输就像我们日常通过聊天工具文件传输一样&#xff0c;发送方将程序传输到超级码头而接收方也只需通过超级码头进行获取即可&#xff0c;就像一只鲸鱼拖着货物来回运输一样。…...

LLM Agent发展演进历史(观看metagpt视频笔记)

LLM相关的6篇重要的论文&#xff0c;其中4篇来自谷歌&#xff0c;2篇来自openai。技术路径演进大致是&#xff1a;SSL (Self-Supervised Learning) -> SFT (Supervised FineTune) IT (Instruction Tuning) -> RLHF。 word embedding的问题&#xff1a;新词如何处理&…...

Linux(操作系统)面经——part2

1、请你说说进程和线程的区别 1.进程是操作系统资源分配和调度的最小单位&#xff0c;实现操作系统内部的并发&#xff1b;线程是进程的子任务&#xff0c;cpu可以识别、执行的最小单位&#xff0c;实现程序内部的并发。 2.一个进程最少有一个线程或有多个&#xff0c;一个线程…...

Flink系列之:WITH clause

Flink系列之&#xff1a;WITH clause 适用流、批提供了一种编写辅助语句以在较大查询中使用的方法。这些语句通常称为公共表表达式 (CTE)&#xff0c;可以被视为定义仅针对一个查询而存在的临时视图。 WITH 语句的语法为&#xff1a; WITH <with_item_definition> [ , …...

KubeSphere 容器平台高可用:环境搭建与可视化操作指南

Linux_k8s篇 欢迎来到Linux的世界&#xff0c;看笔记好好学多敲多打&#xff0c;每个人都是大神&#xff01; 题目&#xff1a;KubeSphere 容器平台高可用&#xff1a;环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

label-studio的使用教程(导入本地路径)

文章目录 1. 准备环境2. 脚本启动2.1 Windows2.2 Linux 3. 安装label-studio机器学习后端3.1 pip安装(推荐)3.2 GitHub仓库安装 4. 后端配置4.1 yolo环境4.2 引入后端模型4.3 修改脚本4.4 启动后端 5. 标注工程5.1 创建工程5.2 配置图片路径5.3 配置工程类型标签5.4 配置模型5.…...

【JVM】- 内存结构

引言 JVM&#xff1a;Java Virtual Machine 定义&#xff1a;Java虚拟机&#xff0c;Java二进制字节码的运行环境好处&#xff1a; 一次编写&#xff0c;到处运行自动内存管理&#xff0c;垃圾回收的功能数组下标越界检查&#xff08;会抛异常&#xff0c;不会覆盖到其他代码…...

聊一聊接口测试的意义有哪些?

目录 一、隔离性 & 早期测试 二、保障系统集成质量 三、验证业务逻辑的核心层 四、提升测试效率与覆盖度 五、系统稳定性的守护者 六、驱动团队协作与契约管理 七、性能与扩展性的前置评估 八、持续交付的核心支撑 接口测试的意义可以从四个维度展开&#xff0c;首…...

是否存在路径(FIFOBB算法)

题目描述 一个具有 n 个顶点e条边的无向图&#xff0c;该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序&#xff0c;确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数&#xff0c;分别表示n 和 e 的值&#xff08;1…...

代理篇12|深入理解 Vite中的Proxy接口代理配置

在前端开发中,常常会遇到 跨域请求接口 的情况。为了解决这个问题,Vite 和 Webpack 都提供了 proxy 代理功能,用于将本地开发请求转发到后端服务器。 什么是代理(proxy)? 代理是在开发过程中,前端项目通过开发服务器,将指定的请求“转发”到真实的后端服务器,从而绕…...

mac 安装homebrew (nvm 及git)

mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用&#xff1a; 方法一&#xff1a;使用 Homebrew 安装 Git&#xff08;推荐&#xff09; 步骤如下&#xff1a;打开终端&#xff08;Terminal.app&#xff09; 1.安装 Homebrew…...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现企业微信功能

1. 开发环境准备 ​​安装DevEco Studio 3.1​​&#xff1a; 从华为开发者官网下载最新版DevEco Studio安装HarmonyOS 5.0 SDK ​​项目配置​​&#xff1a; // module.json5 {"module": {"requestPermissions": [{"name": "ohos.permis…...

PostgreSQL——环境搭建

一、Linux # 安装 PostgreSQL 15 仓库 sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm# 安装之前先确认是否已经存在PostgreSQL rpm -qa | grep postgres# 如果存在&#xff0…...

脑机新手指南(七):OpenBCI_GUI:从环境搭建到数据可视化(上)

一、OpenBCI_GUI 项目概述 &#xff08;一&#xff09;项目背景与目标 OpenBCI 是一个开源的脑电信号采集硬件平台&#xff0c;其配套的 OpenBCI_GUI 则是专为该硬件设计的图形化界面工具。对于研究人员、开发者和学生而言&#xff0c;首次接触 OpenBCI 设备时&#xff0c;往…...