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博客