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

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中使用OpenCV进行OCR(光学字符识别)时,以下是一些常见的OpenCV OCR函数和类: 1. cv::text::OCRTesseract:这是OpenCV中的OCR Tesseract类,可用于识别文本和数字。它是基于Tesseract OCR引擎的封装。你可以使用该类的成员函数来进行OCR识别,例如`run()`、`run(Mat image, Rect roi)`等。 2. cv::text::OCRHMMDecoder:这是OpenCV中的OCR HMM解码器类,用于识别文本和数字。它是基于HMM(隐马尔可夫模型)的OCR解码器。你可以使用该类的成员函数来进行OCR识别,例如`run()`、`run(Mat image, Rect roi)`等。 3. cv::text::OCRHolisticWordRecognizer:这是OpenCV中的全局词语识别器类,用于识别单词或短语。它使用了基于字符级别的分类器和HMM解码器来实现识别。你可以使用该类的成员函数来进行识别,例如`run()`、`run(Mat image, Rect roi)`等。 除了这些类之外,还有一些OpenCV中的图像处理函数可以用于OCR任务,例如: - cv::resize:用于调整图像尺寸。 - cv::cvtColor:用于将图像从一种颜色空间转换为另一种颜色空间,如将彩色图像转换为灰度图像。 - cv::threshold:用于二值化图像,将图像转换为黑白二值图像。 - cv::GaussianBlur:用于对图像进行高斯模糊处理,以减少噪声。 - cv::findContours:用于检测图像中的轮廓,可以用于文本区域检测。 这只是一些常见的OpenCV OCR函数和类,你可以根据具体需求和场景进一步探索OpenCV的文档和示例代码。希望对你有所帮助!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值