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、数值算法 (1)运算后产生结果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安装
书到用时方恨少、觉知此时要躬行;拥有技术,成就未来,抖音视频教学地址: 1、介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle旗下产品。MySQL是最…...

Android camera的metadata
一、实现 先看一下metadata内部是什么样子: 可以看出,metadata 内部是一块连续的内存空间。 其内存分布大致可概括为: 区域一 :存 camera_metadata_t 结构体定义,占用内存 96 Byte 区域二 :保留区&#x…...
ElasticSearch面试题
1.介绍下es的架构? es采用的是分布式的架构,es集群中会有多个结点,而结点的角色主要有下面几种。 协调结点: 请求路由能力,将请求内容将请求转发给对应的结点进行处理。 master结点: 结点管理ÿ…...
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-组件-子组件给父组件发送事件
情景描述 【子组件】中有一个按钮,点击按钮,触发一个事件, 我们希望这个事件的处理逻辑是,给【父组件】发送一条消息过去, 从而实现 【子组件】给【父组件】通信的效果。这个问题的解决就是 “发送事件” 这个操作。 …...

[密码学]AES
advanced encryption standard,又名rijndael密码,为两位比利时数学家的名字组合。 分组为128bit,密钥为128/192/256bit可选,对应加密轮数10/12/14轮。 基本操作为四种: 字节代换(subBytes transformatio…...
CentOS 7 部署pure-ftp
文章目录 (1)简介(2)准备工作(3)更新系统(4)安装依赖环境(5)下载和解压pure-ftp源码包(6)编译和安装pure-ftp(7࿰…...

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

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

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

光栅化渲染:可见性问题和深度缓冲区算法
在前面第二章中,我们了解到,在投影点(屏幕空间中的点)的第三个坐标中,我们存储原始顶点 z 坐标(相机空间中点的 z 坐标): 当一个像素与多个三角形重叠时,查找三角形表面上…...

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

LLM Agent发展演进历史(观看metagpt视频笔记)
LLM相关的6篇重要的论文,其中4篇来自谷歌,2篇来自openai。技术路径演进大致是:SSL (Self-Supervised Learning) -> SFT (Supervised FineTune) IT (Instruction Tuning) -> RLHF。 word embedding的问题:新词如何处理&…...
Linux(操作系统)面经——part2
1、请你说说进程和线程的区别 1.进程是操作系统资源分配和调度的最小单位,实现操作系统内部的并发;线程是进程的子任务,cpu可以识别、执行的最小单位,实现程序内部的并发。 2.一个进程最少有一个线程或有多个,一个线程…...
Flink系列之:WITH clause
Flink系列之:WITH clause 适用流、批提供了一种编写辅助语句以在较大查询中使用的方法。这些语句通常称为公共表表达式 (CTE),可以被视为定义仅针对一个查询而存在的临时视图。 WITH 语句的语法为: WITH <with_item_definition> [ , …...

《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》
引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...
ssc377d修改flash分区大小
1、flash的分区默认分配16M、 / # df -h Filesystem Size Used Available Use% Mounted on /dev/root 1.9M 1.9M 0 100% / /dev/mtdblock4 3.0M...
【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】
1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件(System Property Definition File),用于声明和管理 Bluetooth 模块相…...
爬虫基础学习day2
# 爬虫设计领域 工商:企查查、天眼查短视频:抖音、快手、西瓜 ---> 飞瓜电商:京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空:抓取所有航空公司价格 ---> 去哪儿自媒体:采集自媒体数据进…...
2023赣州旅游投资集团
单选题 1.“不登高山,不知天之高也;不临深溪,不知地之厚也。”这句话说明_____。 A、人的意识具有创造性 B、人的认识是独立于实践之外的 C、实践在认识过程中具有决定作用 D、人的一切知识都是从直接经验中获得的 参考答案: C 本题解…...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)
船舶制造装配管理现状:装配工作依赖人工经验,装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书,但在实际执行中,工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...

【笔记】WSL 中 Rust 安装与测试完整记录
#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统:Ubuntu 24.04 LTS (WSL2)架构:x86_64 (GNU/Linux)Rust 版本:rustc 1.87.0 (2025-05-09)Cargo 版本:cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...

【JVM面试篇】高频八股汇总——类加载和类加载器
目录 1. 讲一下类加载过程? 2. Java创建对象的过程? 3. 对象的生命周期? 4. 类加载器有哪些? 5. 双亲委派模型的作用(好处)? 6. 讲一下类的加载和双亲委派原则? 7. 双亲委派模…...

uniapp 开发ios, xcode 提交app store connect 和 testflight内测
uniapp 中配置 配置manifest 文档:manifest.json 应用配置 | uni-app官网 hbuilderx中本地打包 下载IOS最新SDK 开发环境 | uni小程序SDK hbulderx 版本号:4.66 对应的sdk版本 4.66 两者必须一致 本地打包的资源导入到SDK 导入资源 | uni小程序SDK …...
Linux系统部署KES
1、安装准备 1.版本说明V008R006C009B0014 V008:是version产品的大版本。 R006:是release产品特性版本。 C009:是通用版 B0014:是build开发过程中的构建版本2.硬件要求 #安全版和企业版 内存:1GB 以上 硬盘…...