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 month
    beg.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博客

相关推荐

  1. c++常见函数处理

    2023-12-15 23:04:06       66 阅读
  2. C/C++ 01」C标准库中常见的字符串处理函数

    2023-12-15 23:04:06       38 阅读
  3. c语言复习:常见函数与错误

    2023-12-15 23:04:06       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 23:04:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 23:04:06       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 23:04:06       82 阅读
  4. Python语言-面向对象

    2023-12-15 23:04:06       91 阅读

热门阅读

  1. toRaw()、reactive()以及ref和reactive的区别

    2023-12-15 23:04:06       68 阅读
  2. 【Apache Pinot】Data upload jobtype 粗略分析

    2023-12-15 23:04:06       49 阅读
  3. hook钩子的功能在深度学习里有什么用

    2023-12-15 23:04:06       58 阅读
  4. 图像与视频压缩算法

    2023-12-15 23:04:06       63 阅读
  5. 算法----K 和数对的最大数目

    2023-12-15 23:04:06       54 阅读
  6. yarn link使用(npm link)

    2023-12-15 23:04:06       58 阅读