随笔分类 -  C/C++

STL容器与拷贝构造函数
摘要:http://blog.csdn.net/HEYUTAO007/article/details/6702626所有容器提供的都是“value语意”而非“reference语意”。容器内进行元素的安插操作时,内部实施的是拷贝操作,置于容器内。因此STL容器 的每一个元素都必须能够拷贝。---<<C++标准程序库>> 侯捷、孟岩译 p144页原文以vector为例,往Vector中(实际上所有STL容器都是这样)放元素,Vector会调用元素类的拷贝构造函数生成的副本,当Vector走出生存期时(),会自动调用其中每个元素的析构函数。比如,如果 vector<mycl 阅读全文

posted @ 2013-02-05 16:21 androidme 阅读(236) 评论(0) 推荐(0)

boost::hash_combine
摘要:http://stackoverflow.com/questions/6014036/generate-unique-hash-keys-of-urlhttp://stackoverflow.com/questions/3611951/building-an-unordered-map-with-tuples-as-keyshttp://www.boost.org/doc/libs/1_53_0/doc/html/hash/combine.html 阅读全文

posted @ 2013-02-05 15:52 androidme 阅读(803) 评论(0) 推荐(0)

C++ boost shared_ptr as a hash_map key
摘要:http://www.cppblog.com/Solstice/archive/2013/01/28/197597.htmlhttp://stackoverflow.com/questions/6404765/c-shared-ptr-as-unordered-sets-keyhttp://stackoverflow.com/questions/900369/c-boost-shared-ptr-as-a-hash-map-keyhttp://stackoverflow.com/questions/8771440/stdset-and-boostshared-ptr-unique-key-id 阅读全文

posted @ 2013-02-05 15:10 androidme 阅读(411) 评论(0) 推荐(0)

STL map与Boost unordered_map
摘要:http://blog.csdn.net/orzlzro/article/details/7099231今天看到boost::unordered_map, 它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合适的位置插入到树 中。所以,如果对map进行遍历(中序遍历)的话,输出的结果是有序的。顺序就是按照operator< 定义的大小排序。而boost::unordered_map是计算元素的Hash值,根据Hash值判断元素是否相同。所以,对unordered_map进行遍历,结果是无序的。用法的区别就是, 阅读全文

posted @ 2013-02-05 11:31 androidme 阅读(268) 评论(0) 推荐(0)

BOOST_STATIC_ASSERT and BOOST_ASSERT
摘要:boost之编译期断言BOOST_STATIC_ASSERT与assert的区别assert是运行期断言,也就是在编译期出现的错误,它不会提示;而boost中的BOOST_STATIC_ASSERT是在编译期的断言,也就是说在编译的时候就可以断言出错误。比如下面的函数:void expects_ints_to_be_4bytes(){ BOOST_STATIC_ASSERT(sizeof(int)!=4); assert(sizeof(int)!=4);}编译的时候就会提示static_assert.cpp: In function 'void expects_ints_to_be_4 阅读全文

posted @ 2013-02-04 17:46 androidme 阅读(969) 评论(0) 推荐(0)

Member Functions of Template Classes & Member Function Templates
摘要:http://msdn.microsoft.com/en-us/library/swta9c6e.aspxhttp://msdn.microsoft.com/en-us/library/80dx1bas%28v=vs.80%29.aspx 阅读全文

posted @ 2013-02-04 16:12 androidme 阅读(151) 评论(0) 推荐(0)

Example of UUID generation in c++
摘要:http://stackoverflow.com/questions/3247861/example-of-uuid-generation-in-cA basic example:#include <boost/uuid/uuid.hpp> // uuid class#include <boost/uuid/uuid_generators.hpp> // generators#include <boost/uuid/uuid_io.hpp> // streaming operators etc.int main() { boost::uuids::u... 阅读全文

posted @ 2013-02-03 20:11 androidme 阅读(384) 评论(0) 推荐(0)

Boost智能指针——shared_ptr
摘要:boost::scoped_ptr虽然简单易用,但它不能共享所有权的特性却大大限制了其使用范围,而boost::shared_ptr可以解决这一局限。顾名思义,boost::shared_ptr是可以共享所有权的智能指针,首先让我们通过一个例子看看它的基本用法:#include <string>#include <iostream>#include <boost/shared_ptr.hpp>class implementation{public:~implementation() { std::cout <<"destroying i 阅读全文

posted @ 2013-02-03 15:12 androidme 阅读(782) 评论(0) 推荐(0)

shared_ptr<T> make_shared( Args && ... args );
摘要:shared_ptr很好地消除了显式的delete调用,如果读者掌握了它的用法,可以肯定delete将会在你的编程字典中彻底消失。但这还不够,因为shared_ptr的构造还需要new调用,这导致了代码中的某种不对称性。虽然shared_ptr很好地包装了new表达式,但过多的显式new操作符也是个问题,它应该使用工厂模式来解决。因此,shared_ptr在头文件<boost/make_shared.hpp> 中提供了一个自由工厂函数(位于boost名字空间)make_shared<T>(),来消除显式的new调用,它的名字模仿了标准库的 make_pair(),声明如 阅读全文

posted @ 2013-02-03 15:02 androidme 阅读(1093) 评论(0) 推荐(0)

Multiple Inheritance and enable_shared_from_this
摘要:http://www.codeproject.com/Articles/286304/Solution-for-multiple-enable_shared_from_this-in-ihttp://boost.2283326.n4.nabble.com/smart-ptr-enable-shared-from-this-and-multiple-inheritance-solution-td3058364.htmlhttp://stackoverflow.com/questions/5232712/questions-regarding-shared-from-this1)shared_fr 阅读全文

posted @ 2013-02-03 14:56 androidme 阅读(380) 评论(0) 推荐(0)

C++ load shared library
摘要:http://pocoproject.org/docs/Poco.SharedLibrary.htmlhttp://pocoproject.org/slides/120-SharedLibraries.pdfhttp://www.ros.org/wiki/class_loaderhttp://www.ros.org/wiki/pluginlibhttp://boost-extension.redshoelace.com/docs/boost/extension/index.htmlhttp://src.chromium.org/chrome/trunk/src/base/native_libr 阅读全文

posted @ 2013-02-02 22:37 androidme 阅读(305) 评论(0) 推荐(0)

Poco C++ Library
摘要:http://pocoproject.org/http://pocoproject.com/ 阅读全文

posted @ 2013-02-02 22:13 androidme 阅读(222) 评论(0) 推荐(0)

C++: Static Class Definition
摘要:In C++, the static specifier is only meaningful when applied on functions and variables. But surprisingly, C++ allows static to be used on a class definition:static class Car{ int _speed;};This specifier is useless on the class definition since it does not mean anything! This compiles successfull... 阅读全文

posted @ 2013-02-02 22:09 androidme 阅读(438) 评论(0) 推荐(0)

C++ static、const和static const 以及它们的初始化
摘要:http://blog.csdn.net/yjkwf/article/details/6067267const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间。 static表示的是静态的。类的静态成员函数、静态成员变量是和类相关的,而不是和类的具体对象相关的。即使没有具体对象,也能调用类的静态成员函数和成员变量。一般类的静态函数几乎就是一个全局函数,只不过它的作用域限于包含它的文件中。 在C++中,static静态成员变量不能在类的内部初始化。在类的内部只是声明,定义必须在类定义体的外部,通常在类的实现文件中初始化, 如:double A 阅读全文

posted @ 2013-01-31 14:06 androidme 阅读(212) 评论(0) 推荐(0)

函数模板参数的推导
摘要:http://zhidao.baidu.com/question/519854368.htmlhttp://bbs.chinaunix.net/thread-3678249-1-1.html 阅读全文

posted @ 2013-01-30 18:45 androidme 阅读(181) 评论(0) 推荐(0)

以boost::function和boost:bind取代虚函数
摘要:http://blog.csdn.net/Solstice/article/details/3066268这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不来了”,而借助boost::function和boost::bind,大多数情况下,你都不用上贼船。boost::function和boost::bind已经纳入了std::tr1,这或许是C++0x最值得期待的功能,它将彻底改变C++库的设计方式,以及应用程序的编写方式。Scott Meyers的Effective C++ 3rd ed.第35条款提到了以boost::function和boost:bind取代虚函数的 阅读全文

posted @ 2013-01-30 16:29 androidme 阅读(198) 评论(0) 推荐(0)

C++临时对象
摘要:http://blog.sina.com.cn/s/blog_7b3dad5b0100trnf.htmlhttp://www.cnblogs.com/liyiwen/archive/2009/12/02/1615711.htmlhttp://www.cnblogs.com/liyiwen/archive/2009/12/03/1616627.htmlhttp://www.programlife.net/cpp-return-value-optimization.html 阅读全文

posted @ 2013-01-23 14:10 androidme 阅读(126) 评论(0) 推荐(0)

++iter的效率比iter++的效率高
摘要:for(iterator it = begin(); it != end(); ++it)for(iterator it = begin(); it != end(); it++)两种方式iterator遍历的次数是相同的,但在STL中效率不同,前++--返回引用,后++--返回一个临时对象,因为iterator是类模板,使用it++这种形式要返回一个无用的临时对象,而it++是函数重载,所以编译器无法对其进行优化,所以每遍历一个元素,你就创建并销毁了一个无用的临时对象。不信的话你可以去看看C++的标准库,还有符合标准C++的教材,除了特殊需要和对内置类型外,基本都是使用++it来进行元素遍历 阅读全文

posted @ 2013-01-22 16:45 androidme 阅读(265) 评论(0) 推荐(0)

static constructors in C++? need to initialize private static objects
摘要:http://stackoverflow.com/questions/1197106/static-constructors-in-c-need-to-initialize-private-static-objectsclass MyClass{ public: static vector<char> a; static class _init { public: _init() { for(char i='a'; i<='z'; i++) a.push_back(i); } } _initi... 阅读全文

posted @ 2013-01-22 16:18 androidme 阅读(193) 评论(0) 推荐(0)

Best way to get the index of an iterator
摘要:http://stackoverflow.com/questions/2152986/best-way-to-get-the-index-of-an-iteratorit - vec.begin()std::distance(vec.begin(), it)it - vec.begin() takes constant time, but the operator - is only defined on random access iterators, so the code won't compile at all with list iterators, for example. 阅读全文

posted @ 2013-01-22 16:15 androidme 阅读(153) 评论(0) 推荐(0)

导航