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


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

相关推荐
Nuyoah11klay12 小时前
华清远见25072班C++学习day7
c++
lifallen12 小时前
从Apache Doris 学习 HyperLogLog
java·大数据·数据仓库·算法·apache
bkspiderx12 小时前
C++设计模式之行为型模式:迭代器模式(Iterator)
c++·设计模式·迭代器模式
智驱力人工智能12 小时前
使用手机检测的智能视觉分析技术与应用 加油站使用手机 玩手机检测
深度学习·算法·目标检测·智能手机·视觉检测·边缘计算
姚瑞南12 小时前
【AI 风向标】四种深度学习算法(CNN、RNN、GAN、RL)的通俗解释
人工智能·深度学习·算法
Knight_AL13 小时前
浅拷贝与深拷贝详解:概念、代码示例与后端应用场景
android·java·开发语言
枫叶丹413 小时前
【Qt开发】输入类控件(六)-> QDial
开发语言·qt
补三补四13 小时前
SMOTE 算法详解:解决不平衡数据问题的有效工具
人工智能·算法
思考的笛卡尔13 小时前
Go语言实战:高并发服务器设计与实现
服务器·开发语言·golang
RTC老炮13 小时前
webrtc弱网-RobustThroughputEstimator源码分析与算法原理
网络·算法·webrtc