Posts

如何使用vsprintf, vsnprintf等函数

12 Dec 2016

前言

在一些c++的框架中,有很多小的util函数,通常每个框架里都会有一个log函数,用法类似于printf(fmt, ...), 比如cocos2d-x的log函数用法大概像下面这样: log("size is %d, %d", w, h); 它们的实现就是借助于vsnprintf族函数。本质在于格式化一个字符串。 下面通过一个例子来说明如何使用vsnprintf族函数来实现一个字符串格式化函数

示例

这个格式化函数的用法像下面这样:
int count(10);
cout << format("count is %d\n", count);

format函数实现

...

阅读全文 ...


【C++ STL应用与实现】72: 标准库里的堆--如何使用标准库的heap算法

29 Nov 2016

本系列文章的目录在这里:目录. 通过目录里可以对STL总体有个大概了解

前言

本文介绍如何使用STL里的heap(堆)算法。第一次接触heap这种数据结构是在大学的数据结构教材上,它是一棵完全二叉树。在STL中,heap是算法的形式提供给我们使用的。包括下面几个函数:
  • make_heap: 根据指定的迭代器区间以及一个可选的比较函数,来创建一个heap. O(N)
  • push_heap: 把指定区间的最后一个元素插入到heap中. O(logN)
  • pop_heap: 弹出heap顶元素, 将其放置于区间末尾. O(logN)
  • sort_heap:堆排序算法,通常通过反复调用pop_heap来实现. N*O(logN)
C++11加入了两个新成员:
  • is_heap: 判断给定区间是否是一个heap. O(N)
  • is_heap_until: 找出区间中第一个不满足heap条件的位置. O(N)
因为heap以算法的形式提供,所以要使用这几个api需要包含 #include <algorithm> ...

阅读全文 ...


C++的The Big Three and A Half 是什么

28 Nov 2016

前言

big three and a half or rule of three. 回答内容摘录自:stack overflow - what is rule of three C++ treats variables of user-defined types with value semantics. This means that objects are implicitly copied in various contexts, and we should understand what “copying an object” actually means. Let us consider a simple example:
class person
{
    std::string name;
    int age;

public:

    person(const std::string& name, int age) : name(name), age(age)
    {
    }
};

int main()
{
    person a("Bjarne Stroustrup", 60);
    person b(a);   // What happens here?
    b = a;         // And here?
}
(If you are puzzled by the name(name), age(age) part, this is called a member initializer list.) ...

阅读全文 ...


C++的 copy-and-swap idiom 是什么

23 Nov 2016

前言

回答内容摘录自:stack overflow - What is the copy-and-swap idiom? Overview Why do we need the copy-and-swap idiom? Any class that manages a resource (a wrapper, like a smart pointer) needs to implement The Big Three. While the goals and implementation of the copy-constructor and destructor are straightforward, the copy-assignment operator is arguably the most nuanced and difficult. How should it be done? What pitfalls need to be avoided? The copy-and-swap idiom is the solution, and elegantly assists the assignment operator in achieving two things: avoiding code duplication, and providing a strong exception guarantee. How does it work? Conceptually, it works by using the copy-constructor’s functionality to create a local copy of the data, then takes the copied data with a swap function, swapping the old data with the new data. The temporary copy then destructs, taking the old data with it. We are left with a copy of the new data. In order to use the copy-and-swap idiom, we need three things: a working copy-constructor, a working destructor (both are the basis of any wrapper, so should be complete anyway), and a swap function. A swap function is a non-throwing function that swaps two objects of a class, member for member. We might be tempted to use std::swap instead of providing our own, but this would be impossible; std::swap uses the copy-constructor and copy-assignment operator within its implementation, and we’d ultimately be trying to define the assignment operator in terms of itself! (Not only that, but unqualified calls to swap will use our custom swap operator, skipping over the unnecessary construction and destruction of our class that std::swap would entail.) An in-depth explanation The goal Let’s consider a concrete case. We want to manage, in an otherwise useless class, a dynamic array. We start with a working constructor, copy-constructor, and destructor: ...

阅读全文 ...


【OpenGL Programming On macOS using glfw 】0: Build a Simple OpenGL Program

18 Nov 2016

前言

OpenGL红宝书第九版已经开始使用glfw作为窗口管理和Context创建工具, 取代了第八版里的freeglutglew。 本文讲解如何使用glfw在macOS上来构建一个OpenGL程序(OpenGL 3.0+) 包括以下步骤:
  • 从github下载glfw源码
  • 使用CMake构建glfw,并运行示例,以确保glfw确实可用
  • 编写自己的OpenGL代码,并链接glfw来展示结果
本机环境:macOS Sierra (10.12) + glfw 3.2.1 + OpenGL 4.1

下载源码

官方代码仓库: 可以使用SourceTree来下载,下载完切换到latest分支,这是最新的stable分支。 官方入门文档:

build glfw

编译 假设你已经熟悉CMake,这里我使用out-of-tree的构建方式,打开terminal,切换到glfw根目录,比如: ~/codes/glfw/
cd ~/codes/glfw
mkdir build && cd build            
cmake ..     # macOSglfw的依赖项目除了完整的Xcode工具链就只需要一个CMakeok了,所以这一步正常应该不会出问题的。
make         # 开始编译, 如果要安装到系统,执行make install 我不需要安装,只是跑个demo
运行示例 make成功后,在build目录下输入命令(或者在finder中直接打开build/examples/simple.app)
open ./examples/simple.app
可以看到如下的一个旋转的三角形: glfw-simple.png 官方编译参考文档:compile glfw

开始写自己的OpenGL项目

正如构建其它的c++程序一样,构建自己的基于glfw的OpenGL项目,无非就两步:第一,包含正确的头文件;第二,链接正确的库文件。 ...

阅读全文 ...