当前位置: 首页 > 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> [ , …...

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…...

stm32G473的flash模式是单bank还是双bank?

今天突然有人stm32G473的flash模式是单bank还是双bank&#xff1f;由于时间太久&#xff0c;我真忘记了。搜搜发现&#xff0c;还真有人和我一样。见下面的链接&#xff1a;https://shequ.stmicroelectronics.cn/forum.php?modviewthread&tid644563 根据STM32G4系列参考手…...

<6>-MySQL表的增删查改

目录 一&#xff0c;create&#xff08;创建表&#xff09; 二&#xff0c;retrieve&#xff08;查询表&#xff09; 1&#xff0c;select列 2&#xff0c;where条件 三&#xff0c;update&#xff08;更新表&#xff09; 四&#xff0c;delete&#xff08;删除表&#xf…...

【解密LSTM、GRU如何解决传统RNN梯度消失问题】

解密LSTM与GRU&#xff1a;如何让RNN变得更聪明&#xff1f; 在深度学习的世界里&#xff0c;循环神经网络&#xff08;RNN&#xff09;以其卓越的序列数据处理能力广泛应用于自然语言处理、时间序列预测等领域。然而&#xff0c;传统RNN存在的一个严重问题——梯度消失&#…...

C++ 基础特性深度解析

目录 引言 一、命名空间&#xff08;namespace&#xff09; C 中的命名空间​ 与 C 语言的对比​ 二、缺省参数​ C 中的缺省参数​ 与 C 语言的对比​ 三、引用&#xff08;reference&#xff09;​ C 中的引用​ 与 C 语言的对比​ 四、inline&#xff08;内联函数…...

Spring Boot+Neo4j知识图谱实战:3步搭建智能关系网络!

一、引言 在数据驱动的背景下&#xff0c;知识图谱凭借其高效的信息组织能力&#xff0c;正逐步成为各行业应用的关键技术。本文聚焦 Spring Boot与Neo4j图数据库的技术结合&#xff0c;探讨知识图谱开发的实现细节&#xff0c;帮助读者掌握该技术栈在实际项目中的落地方法。 …...

NLP学习路线图(二十三):长短期记忆网络(LSTM)

在自然语言处理(NLP)领域,我们时刻面临着处理序列数据的核心挑战。无论是理解句子的结构、分析文本的情感,还是实现语言的翻译,都需要模型能够捕捉词语之间依时序产生的复杂依赖关系。传统的神经网络结构在处理这种序列依赖时显得力不从心,而循环神经网络(RNN) 曾被视为…...

c#开发AI模型对话

AI模型 前面已经介绍了一般AI模型本地部署&#xff0c;直接调用现成的模型数据。这里主要讲述讲接口集成到我们自己的程序中使用方式。 微软提供了ML.NET来开发和使用AI模型&#xff0c;但是目前国内可能使用不多&#xff0c;至少实践例子很少看见。开发训练模型就不介绍了&am…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...