列出比较容易忘记的 C++(C)语言语法及标准库 知识点;
Author:kagula LastUpdateInBlogAt:2011-04-07
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
用_access判断文件或文件夹是否存在及其读写权限
http://blog.csdn.net/hurtmanzc/archive/2007/02/13/1508961.aspx
-------------------------------------------------------------------------------------------------------------
使用assert()函数,需要注意,在Debug模式下该语句才会被编译器编译。
-------------------------------------------------------------------------------------------------------------
比特运算符(&)的优先级要低于==、>等运算符!
-------------------------------------------------------------------------------------------------------------
取当前时间
http://bytes.com/topic/c/answers/542675-time_t
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
string和wstring相互转换
http://blog.csdn.net/li_guotao/archive/2008/04/20/2308260.aspx
-------------------------------------------------------------------------------------------------------------
如何將字串前後的空白去除? (C/C++) (template) (boost)
http://www.cnblogs.com/oomusou/archive/2007/02/22/653592.html
-------------------------------------------------------------------------------------------------------------
一个用于Debug的小函数
http://www.wangchao.net.cn/bbsdetail_66023.html
-------------------------------------------------------------------------------------------------------------
string与int/float间的转换
http://www.cnblogs.com/dracohan/archive/2008/12/25/1362616.html
-------------------------------------------------------------------------------------------------------------
class AddFunctor
{
public:
int operator()(int x, int y) const
{
return x + y;
}
};
由于它实现调用运算符,因此可像使用函数一样来使用函数对象:
复制代码
AddFunctor fo;
int result = fo(4, 5);
ASSERT(4 + 5 == result);
-------------------------------------------------------------------------------------------------------------
Q: How could a singleton class be implemented?
A: There are several ways of creating a singleton class. The most simple approach is shown below:
Code:
class CMySingleton
{
public:
static CMySingleton& Instance()
{
static CMySingleton singleton;
return singleton;
}
// Other non-static member functions
private:
CMySingleton() {}; // Private constructor
CMySingleton(const CMySingleton&); // Prevent copy-construction
CMySingleton& operator=(const CMySingleton&); // Prevent assignment
};
---------------------------------------------------------------------------------------------
关于C++ const 的全面总结
http://blog.csdn.net/Eric_Jo/archive/2009/04/30/4138548.aspx
---------------------------------------------------------------------------------------------
如何用vector申请一个多维数组
http://faq.csdn.net/read/139.html
---------------------------------------------------------------------------------------------
运算符重载示例
class test{
friend class test;
friend std::ostream &operator<<(std::ostream &stream,test &a);
friend test& operator>>(std::istream &stream,test &a);
friend test operator+(test &a,test &b); //可以使用c=a+b,d=a+b,则c==d。
public:
test()
{
m_nV=0;
}
test(int a)
{
m_nV=a;
}
test(test &a)
{
m_nV=a.m_nV;
}
test& operator ++() //++c
{
this->m_nV++;
return *this;
}
test& operator++(int) //a++
{
this->m_nV++;
return *this;
}
/*
test& operator+(test &b) //c=a+b,d=a+b结果c<>d,因为第一次a,事实已经变成a=a+b,所以c!=d
{
this->m_nV += b.m_nV;
return *this;
}
*/
test& operator[](int i) //a[任意整数],例:a[5312]
{
return *this;
}
test& operator=(int b)
{
this->m_nV = b;
return *this;
}
private:
int m_nV;
};
std::ostream &operator<<(std::ostream &stream,test &a) //cout<<a<<endl;
{
stream<<a.m_nV;
return stream;
}
test& operator>>(std::istream &stream,test &a) //cin>>a;
{
char t[64];
stream.getline(t,64);
a.m_nV=atoi(t);
return a;
}
test operator+(test &a,test &b)
{
return a.m_nV+b.m_nV;
}
---------------------------------------------------------------------------------------------
map容器的使用
第一步:定义
map<string,string> m_map;
第二步:往里面填东西
m_map.insert(map<string,string>::value_type(key,value)); //string key,value;
//m_map["key"]=strValue; //string strValue;
第三步:找指定key的value
map<string,string>::const_iterator iter = m_map.find(key);
if(iter!=m_map.end())
{
//找到了,返回value
return iter->second;
}
else
{
//没找到
}
//cout<<m_map["key"];
---------------------------------------------------------------------------------------------
友元class,
下面的例子,表示B能访问A的私有成员
class A{
private:
...
friend class B;
};
class B{
friend class A;
...
};
友元函数和友元类
http://www.cppblog.com/twzheng/articles/21020.html
---------------------------------------------------------------------------------------------
如何新建模板类例子
template<class T>class A
{
public:
A(T t)
{
cout<<t<<endl;
}
void sayHello();
};
..............
template <class T> void A<T>::sayHello()
{
cout<<"fasfjasdjkfaskl;!"<<endl;
}
---------------------------------------------------------------------------------------------
大小数端变换
#define CHANGE_ENDING(r) (((r & 0xff)<<24) |((r & 0xff00)<<8) |((r & 0xff0000)>>8) |((r & 0xff000000)>>24))
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
常见问题
http://www.seebs.net/faqs/c-iaq.html
---------------------------------------------------------------------------------------------
初始化类的静态成员变量
class CSpellChecker {
static int m_nRefCount; // How many others are using this?
......
};
int CSpellChecker:: m_nRefCount = 0;
---------------------------------------------------------------------------------------------
C++参考手册
http://www.cplusplus.com/reference/stl/list/
---------------------------------------------------------------------------------------------
如果你想要把自己的Class(含new 、delete内存管理代码)放在STL容器里,正常使用
需要在你的class里实现Copy Constructor Function
再谈拷贝构造函数(Copy Constructor Function)
http://www.cppblog.com/dawnbreak/archive/2009/02/11/68697.html
对需要保存到容器中的对象的类型来说,每个类都必须至少实现拷贝构造函数,最好还能重载赋值操作符。
class TMyClass {
private:
...
public:
TMyClass(..);
// 拷贝构造函数
TMyClass(const TMyClass& obj) { *this = obj; }
// 赋值操作符
TMyClass& operator=(const TMyClass& obj);
...
};
当你把一个对象的实例插入到容器中,STL会自己重新生成一个此对象的拷贝,因此拷贝构造函数就成为必须的了。
如果你没有为拷贝构造函数写正确的代码,
这样,就有可能造成list中对象的某些数据成员没有初始化。
Source:STL使用入门( Using STL) http://oss.org.cn/?action-viewnews-itemid-3746
---------------------------------------------------------------------------------------------
前向引用示例
template <class T> class CCellMemoryContainer;
下面的Class引用CCellMemoryContainer
对CCellMemoryContainer进行定义
---------------------------------------------------------------------------------------------
取int能表示的最大整数
#include <limits>
numeric_limits<int>::max()
---------------------------------------------------------------------------------------------
带可变参数列表的宏定义 可变参数列表在函数中的传递 使用方式举例
...omit...
static void output(std::string strLevel,std::string strFN,int nLine,std::string lpFmt, va_list _ArgList)
{
...omit...
vsprintf_s(Msg,lpFmt.c_str(),_ArgList);
...omit...
}
...omit...
static void err(std::string strFN,int nLine,std::string lpFmt,...)
{
std::string strArgument("Err");
output(strArgument,strFN,nLine,lpFmt, va_list(1+&lpFmt));
}
...omit...
#define info(lpFmt,...) err(__FILE__,__LINE__,lpFmt,## __VA_ARGS__)
...omit...
info("%s %d",strT.c_str(),nT);
...omit...
---------------------------------------------------------------------------------------------
class AddFunctor
{
public:
int operator()(int x, int y) const
{
return x + y;
}
};
由于它实现调用运算符,因此可像使用函数一样来使用函数对象:
复制代码
AddFunctor fo;
int result = fo(4, 5);
ASSERT(4 + 5 == result);
---------------------------------------------------------------------------------------------
测试函数的执行时间(精确到微秒,一毫秒=1000微秒,一秒=1000毫秒)
http://www.cnblogs.com/diylab/archive/2009/01/09/1372313.html
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------