Essential C++读书笔记

2012-03-31

1、switch可以这样用

       switch(next_char)

       {

              case ‘a’:         case ’A’:

              case ’e’:        case ’E’:

              ……

              case ‘u’:        case ‘U’:

              cout<<”这个字母是元音”<<endl;

              break;

       }

2、vector初始化方法

       1,vector1<int> v(10);              v[0]=1……

       2,假设已经有一个长度为10的数组a(已经初始化)

              则可vector<int>v(a,a+10);即,以实际内存位置初始化

3、默认的文件操作为输出模式

       例:ofstream fout(“text.txt”)

              //若文件不存在,创建一个新的,否则,此文件用作输出,txt原内容被删

       但,ofstream fout(“text.txt”,ios_base::app);

              //以追加模式开启文件,将新数据增加到文件中,原有保留

              此时打开文件时,位置会位于文件尾

       ∴fstream iofile(“test.txt”,ios_base::in||ios_base::app);

       即可读也可写,但直接读会是文件尾(追加模式打开),所以需要重定位,可用seekg()重定位至文件头

4、冒泡排序:思路,一轮把最小的元素排到最前

       例如对序列3 5 8 4 1 7 2

       一轮过后 1 x x x x x x

       二轮过后 1 2 x x x x x

void Bubble_Sort(int *a,int length)

{

         int change=1;

         for(int i=0;i<length;i++)

         {

                   for(int j=i+1;j<length&&change;j++)

                   {        change=0;

                            if(a[j]<a[i])

                            {        SWAP(a[j],a[i]);

                                     change=1;

                            }

                   }

         }

}//可通过change变量减少比较次数

5、C++没有提供任何语法让我们从对中配置数组的同时为其设定初值

         ∴int *p=new int [5];

                   每个元素都没有初始化

但int p[5];则若定义于文件作用域内,初始化为0,若定义于local scope(如函数内)则不被初始化

6、如下情况只能用指针,不能用引用

         int func(int &a,ofstream *fo=0)

         {

                   if(fo!=0)    (*fo)<<…….

                   //将一些信息打印到文件中(可选功能)

                   若用户提供一个ofstream对象,则打印,否则,只处理,不打印

}//若用引用,则一定代表某个对象,不能设为0(引用必须初始化),则不符合可选功能这一要求

7、尽量不使用file scope对象(即全局变量→难以在其他环境中使用)

         ∴以参数传递作为函数间的沟通方式→如例6

         或使用局部static对象→不会消失

         如对于斐波那契数列,若已经计算过f(24),则计算f(30)的时候只需计算最后6个即可

         void f(int n)

         {

                   static vector<int >result;

                   for(int i=result.size();i<n;i++)

                   {

                            if(i==1||i==0)            result.pushback(1);

                            else                            result.pushback(result[i-1]+result[i-2]);

                                    

                   }

         }//效率提升明显

8、使用map举例           include<map>

         map<string,int>words;

         string temp;

         while(cin>>temp)     words[temp]++;

         //统计单词数,若temp不在map内,会被加入,并获得初值0,若以存在,则值+1

        

         打印:map<string,int>::iterator it=words.begin():

                   for(;it!=words.end(0;it++)        cout<<it->first<<it->second<<endl;

         查询:1,if(words[“test”]==0)                //缺点,会把test加入其中

                   2,用find,it=words.find(“test”);    if(it!=words.end())            cnt=it->second

                   3,用count,       if(words.count(“test”)!=0)                cnt=words[“test”];

9、时刻注意若类成员中含有指针对象,一定要重写复制构造函数和赋值运算符

         假设string类中有成员char *data;

         则,func()

         {

                   string s1=”hello”;

                   if(……)       string s2=s1;

         }//则执行到此处(即if语句结束时),s2已被析构,但由于s1.data与s2.data指向了同一处内存地址,当后面要用s1.data时会崩溃

10、在类中设置定值

         private: static const int size=1024;

                            int data[size];  //正确

         或者用enum

原文地址:https://www.cnblogs.com/yangtianxing/p/2444482.html