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

news2024/9/26 5:21:06

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*6
    cout << "innser product: " << inner_product(coll.cbegin(), coll.cend(),
                                                coll.cbegin(),
                                                0) << endl;
    // 0 + 1*6 + 2*5 + 3 * 4 + 4*3+5*2+6*1
    cout << "inner_product: " << inner_product(coll.cbegin(), coll.cend(),
                                               coll.crbegin(),
                                               0) << endl; 
    // 1 * 1+1 * 2+2 * 3+3 * 4+4 * 5+5 * 6+6
    cout << "product of sums: " 
         << inner_product(coll.cbegin(), coll.cend(),    // first range
                          coll.cbegin(),                 // second range
                          1,                             // initial value
                          multiplies<int>(),             // outer operation
                          plus<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 stack
    st.push(1);
    st.push(2);
    st.push(3);

    // pop and print two elements from the stack
    cout << st.top() << ' ';
    st.pop();
    cout << st.top() << ' ';
    st.pop();

    // modify top element
    st.top() = 77;

    // push two new elements
    st.push(4);
    st.push(5);

    // pop one element without processing it
    st.pop();

    // pop and print remaining elements
    while (!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 queue
      q.push("These ");
      q.push("are ");
      q.push("more than ");

      // pop two elements from the queue and print their value
      cout << q.pop();
      cout << q.pop();

      // push two new elements
      q.push("four ");
      q.push("words!");

      // skip one element
      q.pop();

      // pop two elements from the queue and print their value
      cout << q.pop();
      cout << q.pop() << endl;

      // print number of remaining elements
      cout << "number of elements in the queue: " << q.size()
           << endl;

      // read and print one element
      cout << 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 color
    enum Color { red, yellow, green, blue, white, black, //...,
                 numColors };

    // create bitset for all bits/colors
    bitset<numColors> usedColors;

    // set bits for two colors
    usedColors.set(red);
    usedColors.set(blue);

    // print some bitset data
    cout << "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 used
    if (usedColors.any()) {
        // loop over all colors
        for (int c = 0; c < numColors; ++c) {
            // if the actual color is used
            if (usedColors[(Color)c]) {
                //...
            }
        }
    }
}
#include <bitset>
#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main()
{
    // print some numbers in binary representation
    cout << "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 string
    string s = bitset<42>(12345678).to_string();
    cout << "12,345,678 with 42 bits: " << s << endl;

    // transform binary representation into integral number
    cout << "\"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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1314875.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C# WPF上位机开发(crc校验)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 为了验证数据传输的过程中有没有发生翻转&#xff0c;我们在传输报文的同时一般还会添加一个crc校验。对于modbus协议也是一样&#xff0c;它在数据…

帆软FCRP模拟题

制作步骤可见此博主&#xff1a;https://blog.csdn.net/Ipkiss_Yongheng/article/details/125594366 完成文件下载&#xff1a;【免费】帆软FCRP官网模拟题代码资源-CSDN文库

css的元素显示模式(有单行文字垂直居中,侧边栏等案例)

目录 1. 什么是元素的显示模式 2. 元素显示模式的类型 块元素 行内元素 行内块元素 3. 元素显示模式的转换 4.文字垂直居中 5.具体实现案例 1. 什么是元素的显示模式 定义&#xff1a;元素显示模式就是元素&#xff08;标签&#xff09;以什么方式进行显示&#xff0c;…

基于ssm居家养老系统论文

居家养老系统 摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了居家养老系统的开发全过程。通过分析高校学生综合素质评价管理方面的不足&#xff0c;创建了一个计算机管理居家养老系统的方案。文章介绍了居家…

springboot升级到3.2导致mybatis-plus启动报错

在springboot升级到3.2时&#xff0c;服务启动报错 java.lang.IllegalArgumentException: Invalid value type for attribute ‘factoryBeanObjectType’: java.lang.String&#xff1a; java.lang.IllegalArgumentException: Invalid value type for attribute factoryBeanOb…

系统安全-WAF入侵防御系统测评指标

WAF&#xff08;Web Application Firewall&#xff09;是一种部署在web应用程序前面的安全系统&#xff0c;其作用是在用户请求到达web服务器之前对用户请求进行扫描和过滤&#xff0c;分析并校验每个用户请求的网络包&#xff0c;确保每个用户请求有效且安全&#xff0c;对无效…

maven工程中读取resources中的资源文件

maven工程的代码布局如下&#xff1a;在resources下面有一个资源文件test.properties&#xff0c;现在的目标要在Java代码中读取该资源文件中的内容。 test.properties资源文件的内容如下&#xff1a; Java代码如下&#xff1a; package com.thb;import java.io.BufferedR…

(c语言)输出一个十进制整数的二进制序列

#include<stdio.h> int main() {int x;scanf("%d",&x);for (int i 31; i >0; i--)//从前向后输出{if (((x >> i) ^ 0) 0) //当移位后全都是0时&#xff0c;进入下一个循环{continue; //需要输出完整二进制数时该判断语句删去}if (…

【SpringBoot】之Mybatis=Plus集成及使用(入门级)

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是君易--鑨&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的博客专栏《SpringBoot开发之Mybatis-Plus系列》。&#x1…

​【EI会议征稿中】#先投稿,先送审#第三届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2024)​

#先投稿&#xff0c;先送审# 第三届网络安全、人工智能与数字经济国际学术会议&#xff08;CSAIDE 2024&#xff09; 2024 3rd International Conference on Cyber Security, Artificial Intelligence and Digital Economy 2024年3月1日-3日 | 中国南京 会议官网&#xff1a…

师兄啊师兄第二季开播:男主成海神?玄机是懂联动的!

《师兄啊师兄》动画第二季在12月14日终于正式开播&#xff0c;首播两集&#xff0c;还是很有诚意的。 这部动画改编自言归正传的小说《我师兄实在太稳健了》&#xff0c;原著的知名度不算很高&#xff0c;但玄机制作的动画让这个IP火出了圈。 动画第一季就凭借高颜值的人物建模…

【unity小技巧】unity最完美的CharacterController 3d角色控制器,实现移动、跳跃、下蹲、奔跑、上下坡,复制粘贴即可

最终效果 文章目录 最终效果前言为什么使用CharacterControllerSimpleMove和Move如何选择&#xff1f;1. SimpleMove2. Move 配置CharacterController参数控制相机移动跳跃下蹲处理下坡抖动问题实现奔跑和不同移速控制完整代码完结 前言 其实一开始我是不打算写的&#xff0c;…

新能源汽车的“一池春水”,或许不再由价格战掀起波澜?

2005年12月15日&#xff0c;丰田普锐斯混合动力车进入中国&#xff0c;拉开了国内新能源汽车发展的序幕。18年后的今天&#xff0c;国产及进口的新能源汽车已经渗透我国超三分之一的乘用车市场&#xff0c;与油车二分天下的愿景渐趋实现。 今年11月&#xff0c;随着购车需求进…

VLAN间的通讯---三层交换

一.三层交换 1.概念 使用三层交换技术实现VLAN间通信 三层交换二层交换 三层转发 2.基于CEF的MLS CEF是一种基于拓补转发的模型 转发信息库&#xff08;FIB&#xff09;临接关系表 转发信息库&#xff08;FIB&#xff09;可以理解为路由表 邻接关系表可以理解为MAC地址表…

从数据应用案例出发,探索2024年及未来的数据科学转型

如今&#xff0c;数据科学已经取得了长足的进步&#xff01;回顾数据科学的发展史&#xff0c;19世纪&#xff0c;人们使用基本统计模型收集、存储和处理数据。后来&#xff0c;当计算机进入万千家庭&#xff0c;数字时代正式到来&#xff0c;并由此产生了大量数据。互联网上数…

java实现局域网内视频投屏播放(二)爬虫

代码链接 视频播放原理 大多视频网站使用的是m3u8&#xff0c;m3u8其实不是一个真正的视频文件&#xff0c;而是一个视频播放列表&#xff08;playlist&#xff09;。它是一种文本文件&#xff0c;里面记录了一系列的视频片段&#xff08;segment&#xff09;的网络地址。这些…

ubuntu pycharm 死机,如何重启

1. 找出pycharm 进程的id 进入命令行&#xff1a; ps -ef 是查看当前运行的进程 值输入 ps -ef 会返回所有当前执行的进程&#xff0c;太多了&#xff0c;过滤一下&#xff0c;找到 pycharm : ps -ef | grep pycharm 2. 使用 kill -s 9 来杀死进程 如图所是&#xff0c;…

Visual Studio编辑器中C4996 ‘scanf‘: This function or variable may be unsafe.问题解决方案

目录 ​编辑 题目&#xff1a;简单的ab 1. 题目描述 2. 输入格式 3. 输出格式 4. 样例输入 5. 样例输出 6. 解题思路 7. 代码示例 8. 报错解决 方案一 方案二 方案三 方案四 总结 题目&#xff1a;简单的ab 1. 题目描述 输入两个整数a和b&#xff0c;…

产品Axure的元组件以及案例

前言 产品&#xff1c;Axure的安装以及组件介绍-CSDN博客经过上文我们可以知道我们Axure是一款适用于网站、移动应用和企业软件的交互式原型设计工具。它可以帮助用户创建高保真的交互式原型&#xff0c;包括线框图、流程图、模型、注释和规格等&#xff0c;以便与客户、开发人…

深度学习 Day16——P5运动鞋识别

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 | 接辅导、项目定制 文章目录 前言1 我的环境2 代码实现与执行结果2.1 前期准备2.1.1 引入库2.1.2 设置GPU&#xff08;如果设备上支持GPU就使用GPU,否则使用C…