列出比较容易忘记的 C++(C)语言语法及标准库

列出比较容易忘记的 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
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值