C++现代教程四

html 复制代码
float转string不带多余0
float a = 1.2;
std::tostring(a); 		 // 1.200000
std::ostringstream strStream;
strStream << a; // 1.2
if (!strStream.view().empty())	// 判定流有数据

// 边框融合
float measureText(std::u8string text, FontTypes::Rectangle &rectangle)
{
    float width   = 0;
    auto  results = shape(text, {});
    if (results.empty())
        return 0;
    using FLimit = std::numeric_limits<float>;
    float minX   = FLimit::max();
    float minY   = FLimit::max();
    float maxX   = FLimit::min();
    float maxY   = FLimit::min();
    for (auto &result : results) {
        FontTypes::Rectangle inkBound, outBound;
        getGlyphBound(result.codepoint, inkBound, outBound);
        inkBound.x += width;

        minX = std::min(minX, inkBound.x);
        minY = std::min(minY, inkBound.y);
        maxX = std::max(maxX, inkBound.x + inkBound.width);
        maxY = std::max(maxY, inkBound.y + inkBound.height);

        width += result.advance;
    }
    rectangle.x      = minX;
    rectangle.y      = minY;
    rectangle.width  = maxX - minX;
    rectangle.height = maxY - minY;
}

// #line使用
#line 143 "emoji_presentation_scanner.c" 这行代码告诉预处理器,从这行开始,任何编译错误或警告都应该指向 "emoji_presentation_scanner.c" 文件的第 143 行。
如果你使用脚本或工具自动生成代码,#line 指令可以用来设置生成代码的行号和文件名,确保错误信息指向生成代码的源文件和行号。
#line 100 "generated_code.c"		// Auto-generated code goes here.. 
#line 指令可以在宏体内重设行号和文件名,以便错误信息指向宏定义处,而不是宏展开处
#define MY_MACRO() \
 do { \
   #line __LINE__ + 1 __FILE__ \
   /* ... macro body ... */ \
 } while(0)
 
# 范围限制
#define LimitRange(x, min, max)  x = x < min ? min : (x > max ? max : x)

# std::call_once
保证函数或者一些代码段在并发或者多线程的情况下,始终只会被执行一次。
#include <iostream>
#include <mutex>
#include <thread>
std::once_flag flag;

void initializeResource() {
    std::cout << "Resource initialized." << std::endl;
    // 进行初始化工作
}

void doSomething() {
    std::call_once(flag, initializeResource);
    // 在这里可以安全地使用已初始化的资源
    std::cout << "Doing something with the resource." << std::endl;
}

int main() {
    std::thread t1(doSomething);
    std::thread t2(doSomething);

    t1.join();
    t2.join();

    return 0;
}

// bitset初始化
template <typename... T>
constexpr auto BitSet(T... t)
{
    return ((1 << (t)) | ...);
}

// 超长枚举
enum Flag : uint16_t{
	A = 1,
	B,
	C,
	AB = BitSet(A, B);
	ABC = BitSet(A, B, C);
};


/*
 * C++ IMPL通用模板设计
 */

/*
 * 数据层
 */
class Data {
public:
	float a;
	float b;
	float c;
};

/*
 * 接口层
 */
class Accessor {
public:
    using Impl = Data;
    using ImplSP = std::shared_ptr<Data>;
	
	Accessor() : m_impl(std::make_shared<Impl>) {}
	
	void setA(float v) { m_impl->a = v; }
	float getA() const { return m_impl->a; }
private:
	ImplSP m_impl;
};

C++ 现代教程三-CSDN博客


创作不易,小小的支持一下吧!

相关推荐
asdzx673 分钟前
C#:从 URL 下载 PDF 文档到本地
开发语言·pdf·c#
阿凤217 分钟前
uniapp如何修改下载文件位置
开发语言·前端·javascript
m0_716765238 分钟前
数据结构--循环链表、双向链表的插入、删除、查找详解
开发语言·数据结构·c++·学习·链表·青少年编程·visual studio
聆风吟º9 分钟前
【C标准库】深入理解C语言strstr函数:子字符串查找的实用指南
c语言·开发语言·库函数·strstr
XY_墨莲伊9 分钟前
【编译原理】实验一:基于正则文法的词法分析器设计与实现
开发语言·数据结构·算法
Tirzano10 分钟前
springsession全能序列化方案
java·开发语言
坐吃山猪11 分钟前
Python20_MCP添加鉴权
开发语言·python
剑挑星河月13 分钟前
394.字符串解码
数据结构·算法·leetcode
算法鑫探14 分钟前
C语言实现 简易计算器教程
c语言·数据结构·算法·新人首发
gihigo199829 分钟前
分布式发电的配电网有功-无功综合优化 MATLAB 实现
开发语言·分布式·matlab