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

c++常见函数处理

1、clamp

clamp:区间限定函数

int64_t a = Clamp(a, MIN_VALUE, MAX_VALUE);
#include <iomanip>
#include <iostream>
#include <sstream>int main()
{std::cout << "no setw: [" << 42 << "]\n"<< "setw(6): [" << std::setw(6) << 42 << "]\n"<< "no setw, several elements: [" << 89 << 12 << 34 << "]\n"<< "setw(6), several elements: [" << 89 << std::setw(6) << 12 << 34 << "]\n";std::istringstream is("hello, world");char arr[10];is >> std::setw(6) >> arr;std::cout << "Input from \"" << is.str() << "\" with setw(6) gave \""<< arr << "\"\n";
}

2、difftime

计算秒维度的两个时间的差距

#include <stdio.h>
#include <time.h>int main(void)
{time_t now = time(0);struct tm beg = *localtime(&now);// set beg to the beginning of the monthbeg.tm_hour = 0,beg.tm_min = 0,beg.tm_sec = 0,beg.tm_mday = 1;double seconds = difftime(now, mktime(&beg));printf("%.f seconds have passed since the beginning of the month.\n", seconds);return 0;
}

3、std::stable_sort

按非降序对 [first, last] 范围内的元素进行排序。保证保留相等元素的顺序。

如果对于任何迭代器,它指向序列和任何非负整数 n,使得它 + n 是指向序列元素的有效迭代器,则序列相对于比较器 comp 进行排序,comp(*(it + n)、*it)(或 *(it + n) < *it) 的计算结果为 false。

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>struct Employee
{int age;std::string name; // Does not participate in comparisons
};bool operator<(const Employee& lhs, const Employee& rhs)
{return lhs.age < rhs.age;
}int main()
{std::vector<Employee> v{{108, "Zaphod"}, {32, "Arthur"}, {108, "Ford"}};std::stable_sort(v.begin(), v.end());for (const Employee& e : v)std::cout << e.age << ", " << e.name << '\n';
}
 std::stable_sort(match_docs.begin(), match_docs.end(),[&ref_struct](const MatchDoc &lhs, const MatchDoc &rhs) {int64_t l_level = ref_struct.level_ref->get(lhs);int64_t r_level = ref_struct.level_ref->get(rhs);float l_score = ref_struct.score_ref->get(lhs);float r_score = ref_struct.score_ref->get(rhs);if (l_level == r_level) {return l_score >= r_score;} else {return l_level > r_level;}});

4、resize

重新指定vector的大小

#include <vector>
#include <iostream>void print(auto rem, const std::vector<int>& c)
{for (std::cout << rem; const int el : c)std::cout << el << ' ';std::cout << '\n';    
}int main()
{std::vector<int> c = {1, 2, 3};print("The vector holds: ", c);c.resize(5);print("After resize up to 5: ", c);c.resize(2);print("After resize down to 2: ", c);c.resize(6, 4);print("After resize up to 6 (initializer = 4): ", c);
}

5、std::vector<T,Allocator>::insert

vector的插入函数

#include <iostream>
#include <iterator>
#include <vector>void print(int id, const std::vector<int>& container)
{std::cout << id << ". ";for (const int x : container)std::cout << x << ' ';std::cout << '\n';
}int main ()
{std::vector<int> c1(3, 100);print(1, c1);auto it = c1.begin();it = c1.insert(it, 200);print(2, c1);c1.insert(it, 2, 300);print(3, c1);// `it` no longer valid, get a new one:it = c1.begin();std::vector<int> c2(2, 400);c1.insert(std::next(it, 2), c2.begin(), c2.end());print(4, c1);int arr[] = {501, 502, 503};c1.insert(c1.begin(), arr, arr + std::size(arr));print(5, c1);c1.insert(c1.end(), {601, 602, 603});print(6, c1);
}

6、 std::stringstream

用来进行流的输入、输出和输入输出操作

std::stringstream sstream;
sstream.precision(4);
sstream << basic_score << ","<< basic_score<< ","<< qd_ctr_24h << "," << qd_ctr_7d << ","<< log_qd_clk_24h << "," << log_qd_clk_7d;
std::string features = sstream.str();

7、参考文献

理解 C++ 中的 Vector insert()

std::vector<T,Allocator>::resize - cppreference.com

difftime() function in C++ - GeeksforGeeks

std::time_t - cppreference.com

std::clamp - cppreference.com

std::stable_sort - cppreference.com

std::vector<T,Allocator>::insert - cppreference.com

clamp函数:区间限定函数-CSDN博客

C++编程语言中stringstream类介绍-CSDN博客

http://www.lryc.cn/news/260168.html

相关文章:

  • MYsql第二次作业
  • SQLAlchemy 第三篇
  • 交互过程中影响信息质量好坏的因素
  • 服务器上配置jupyter,提示Invalid credentials如何解决
  • Axure中动态面板使用及轮播图多种登录方式左侧导航栏之案列
  • 大数据之旅-问题反思
  • 系统级基础信号知识【Linux】
  • Excel单元格隐藏如何取消?
  • Visual Studio(VS)常用快捷键(最详细)
  • UDP特性之组播(多播)
  • ElasticSearch之cat shards API
  • Thread-Per-Message设计模式
  • 运筹学经典问题(一):指派问题
  • 产品经理之如何编写竞品分析(医疗HIS系统管理详细案例模板)
  • 虚拟内存管理
  • ssh时怎么同时指定其端口号,以及scp文件到远程的指定端口
  • Redis过期淘汰策略
  • 微信小程序---自定义组件
  • CGAL的最优传输曲线重构
  • 使用Docker本地安装部署Draw.io绘图工具并实现远程访问协作办公
  • 流程图、泳道图的介绍和示例分享,以及自定义元件库的介绍
  • RabbitMq的详细使用
  • 软件设计师——软件工程(二)
  • 阿里云RDS MySQL 数据如何快速同步到 ClickHouse
  • HINet技术要点
  • IntelliJ IDEA2023学习教程
  • MATLAB基础应用精讲-【数模应用】神经网络(补充篇)
  • 洛谷题单【算法1-7】搜索
  • WordPress主题Lolimeow v8.0.1二次元风格支持erphpdown付费下载
  • WTN6xxx系列OTP语音芯片:智能语音解决方案的可靠之选