lua保留n位小数方法 本文转载至http://www.cnblogs.com/pk-run/p/4444582.html1. string.format()function GetPreciseDecimal(nNum, n) if type(nNum) ~= "number" then return nNum; end n = n or
设置函数环境——setfenv 文章转载至: http://www.cnblogs.com/sifenkesi/p/3843348.html 当我们在全局环境中定义变量时经常会有命名冲突,尤其是在使用一些库的时候,变量声明可能会发生覆盖,这时候就需要一个非全局的环境来解决这问题。setfenv函数可以满足我们的需求。 setfenv(f, table):设置一个函数的环境 (1)当第一个参数为一个
multimap用法 #include #include #include #include using namespace std;int main(int argc, char** argv){ multimap mmap; mmap.insert(pair(1, 1)); mmap.insert(pair(1, 2)); mmap.insert(pair(2, 3)); multimap:
list的用法,基本把所有成员试了一遍 #include #include #include using namespace std;bool RemoveOdd(const int& val){ return val % 2;}struct Desc : public binary_function{ result_type operator()(first_argument_type _Left, secon
单向队列queue的使用 #include #include #include using namespace std;int main(int argc, char** argv){ //deque没有迭代器,stack也没有,因此都不能排序 //deque插入元素只能从尾部插入,弹出智能从头部弹出 //创建单向队列 queue q; printf("size(): %d", q.size()
deque的用法 #include #include #include using namespace std;class GreaterComp : public binary_function{public: result_type operator()(first_argument_type& _Left, second_argument_type& _Right) { return _
stack的使用 #include #include #include using namespace std;int main(int argc, char** argv){ stack s; //返回栈顶元素 //printf("top(): %d", s.top()); //出错 //检测栈是否为空 printf("empty(): %d",s.empty()); //从栈
stl set的用法 #include #include #include #include using namespace std;struct GreaterComp : public binary_function{ result_type operator()(first_argument_type& _Left, second_argument_type& _Rigth) { retur
vector的使用 #include #include #include using namespace std;int main(int argc, char** argv){ vector ivec; //想vector尾部插入元素,返回void ivec.push_back(1); ivec.push_back(11); ivec.push_back(12); //得到首元素和尾元素
hash_map的用法续 如果要在hash_map中把自已定义的类作为key的话要怎么做?这种情况下需要定义计算自定义的hash函数和比较自定义类的比较函数#include #include #include using namespace std;class A{public: A(int a, int b) : m_a(a), m_b(b) {} A(const A& a) { prin
hash_map的使用 例子:#include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ hash_map hm; //插入元素 for (int i = 0; i < 10; i++) hm.insert(make_pair(i, i)); //正序输出 hash_map::i
map容器的一些方法说明 #include #include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ map dmap; dmap.insert(pair(3, 1)); dmap.insert(pair(4, 1)); dmap.insert(pair(5, 1)); dmap.inse
map容器的erase用法 删除指定map中指定key的元素:#include #include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ map dmap; dmap.insert(pair(3, 1)); dmap.insert(pair(4, 1)); dmap.insert(pair
map容器的insert用法总结 例子:#include #include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ map imap; map jmap; jmap[1] = 1; jmap[2] = 2; imap.insert(jmap.begin(), jmap.end()); m
杂项 Q: #include 和 #include “...”的区别?A:前者优先在库Include路径中搜索,后者优先在项目Include路径中搜索.Q:C++中, extern "C" 在哪些情况下会被用到,作用是什么?A: 因为C++的函数重载等特征,会在编译时在函数名称前后添加修饰符(取决于实现), 这一点与C语言不兼容. 用 extern "C" 修饰后的函数
shared_ptr的线程安全性 shared_ptr 的线程安全级别和内建类型、标准库容器、std::string 一样,即:• 一个 shared_ptr 对象实体可被多个线程同时读取;• 两个 shared_ptr 对象实体可以被两个线程同时写入,“析构”算写操作;• 如果要从多个线程读写同一个 shared_ptr 对象,那么需要加锁;请注意,以上是 shared_ptr 对象本身的线程安全
shared_ptr的用法 先看一个例子:#include #include #include using namespace std;class A{public: A() { printf("A constructor"); } ~A() { printf("A destructor"); } void Print() { printf("This is A"); }};
operator new和operator delete 1. 为什么要重载 operator new ?答: 效率问题: 通常系统默认提供的分配器速度极慢, 而且分配小型对象时空间浪费严重.改变行为:默认的分配器失败时会抛出异常, 或许你想改变这种行为.2. operator new 的行为答:区分三个不同的 new: new 操作符(new 表达式, new operator, new expression):
关于指针的删除 先上一个例子:#include #include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char* dst = "This is a str"; char* str = (char*)malloc(strlen(dst) + 1); if (str == NUL