Chinaunix首页 | 论坛 | 博客
  • 博客访问: 85997
  • 博文数量: 19
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 281
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-07 14:42
个人简介

业精于勤,荒于嬉;凡事用心,事事皆成。

文章分类

全部博文(19)

文章存档

2017年(1)

2016年(9)

2015年(2)

2014年(2)

2013年(5)

我的朋友

分类: C/C++

2014-03-04 09:45:51

积累知识,慢慢沉淀

1.  char 转 wchar_t*


点击(此处)折叠或打开


  1. const WCHAR* g_strMsg = L"登录失败,请重试!";//message send to screen of the result

  2. wchar_t* wchMsg;

  3. char chMsg[100];
  4. sprintf(chMsg ,"登录失败,原因:用户PIN码错误(%d),错误%d次将锁定该用户5分钟!",nThis,nAlltime);
  5. int n = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,chMsg,-1,NULL,0);
  6. wchMsg = new wchar_t[n];
  7. MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,chMsg,-1,wchMsg,n);
  8. g_strMsg = wchMsg;


可以将char转换为wchar_t*的。
Write time: 2014.02.25


2.数组


 1)数组初始化必须指明大小如: int arr[10];
      其实arr相当于一个常量指针,在传递给一个函数的时候必须将大小(10)作为附加参数传递。
  2)如果数组大小未知,必须显示的声明一个指针,用动态内存来分配空间:new []
      int *arr = new int[n];但是不要忘记了 delete[] arr;不然会造成内存泄露。

Write time: 2014.02.28
     

3.函数模板    

4.类模板

5.函数对象

eg:

点击(此处)折叠或打开

  1. #include <string>
  2. #include <vector>
  3. using namespace std;
  4. //Write time:2014.03.04
  5. //By:zhengxianle

  6. //Generic findMax,with a function object,Version 1.0
  7. //Precondition: a.size() > 0.
  8. template <typename Object , typename Comparator>
  9. const Object & findMax(const vector<Object>&arr,Comparator cmp)
  10. {
  11.     int maxIndex = 0;
  12.     for(int i = 0 ; i < arr.size() ; i++)
  13.     {
  14.         if(cmp.isLessThan(arr[maxIndex],arr[i]))
  15.             maxIndex = i;
  16.     }
  17.     return arr[maxIndex];
  18. }

  19. class CaseInsensitiveCompare
  20. {
  21. public:
  22.     bool isLessThan(const string &lhs,const string &rhs) const
  23.     {
  24.         return stricmp(lhs.c_str(),rhs.c_str()) < 0 ;
  25.     }
  26. };
  27. void main()
  28. {
  29.     vector<string> arr(3);
  30.     arr[0] = "ZEBRA";
  31.     arr[1] = "alligator";
  32.     arr[2] = "crocodole";
  33.     cout<<findMax(arr,CaseInsensitiveCompare())<<endl;
  34.     
  35. }
The result is:ZEBRA

6.算法分析

    1)运行时间
         1.1一般法则
             法则1:for循环     一个for循环的运行时间至多是该for循环内语句(包括测试)的运行时间乘以迭代的次数。
             法则2:嵌套循环  从里向外分析这些循环。在一组嵌套循环内部的一条语句总的运行时间为该语句的运行时间乘以该组所有循环的大小的乘积。
                        eg:时间O(N2):
                        

点击(此处)折叠或打开

  1. for(int i = 0; i < n ; i++ )
  2.      for(int j = 0; j < n ; j++ )
  3.          k++;
             法则3:顺序语句   将各个语句的运行时间求和即可。
                       如果一个为O(N),另一个为O(N2).则总时间复杂度为O(N2)!
             法则4:if/else语句 具体问题具体分析,但一般是if分支或者else分支时间最长的一个。

    2)最大子序列和问题
         给定整数A1 ,A2 ,...,AN    (可能有负数),求Σk=ij Ak  的最大值。

        下面给的是最优解:

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;

  4. //Write time:2014.03.14
  5. //By:zhengxianle
  6. //时间复杂度为:O(N) 一次for循环!
  7. int maxSubSum(const vector<int> &a )
  8. {
  9.     int nMaxSum = 0, nThisSum = 0;
  10.     int nBegin = 0, nEnd = 0, nExchange = 0;

  11.     for( int nThis = 0; nThis < a.size() ; nThis++ )
  12.     {
  13.         nThisSum += a[nThis];
  14.         if( nThisSum > nMaxSum )
  15.         {
  16.             nMaxSum = nThisSum ;
  17.             nBegin = nExchange + 1;
  18.             nEnd = nThis + 1;
  19.         }
  20.         else if( nThisSum < 0 )
  21.         {
  22.             nThisSum = 0;
  23.             nExchange = nThis + 1;
  24.         }
  25.     }

  26.     cout<<"Begin is: " <<nBegin <<" End is: " <<nEnd <<endl;

  27.     return nMaxSum;
  28. }
  29. int main()
  30. {
  31.     vector<int> vData;

  32.     //容器放入数据
  33.     vData.push_back(-9);
  34.     vData.push_back(3);
  35.     vData.push_back(7);
  36.     vData.push_back(1);
  37.     vData.push_back(-5);
  38.     vData.push_back(6);
  39.     vData.push_back(-4);
  40.     vData.push_back(-3);
  41.     vData.push_back(5);
  42.     vData.push_back(-6);
  43.     vData.push_back(9);

  44.     //求最大子序列和
  45.     cout<<maxSubSum(vData)<<endl;

  46.     return 0;
  47. }

   3)运行时间中的对数

      1、二分搜索 O(log N)
           给定一个整数X和整数A0,A1,...,AN,后者已经预先排序并在内存中,求下标 i 是的Ai = X,如果X不在数据中,则返回 i =  -1.

点击(此处)折叠或打开

  1. //Write time:2014.03.17
  2. //By:zhengxianle
  3. //二分搜索法(已排序),时间复杂度:O(log N)
  4. template <typename DataType>
  5. int binaryFind(const vector<DataType> &a, const DataType &value)
  6. {
  7.     int low = 0, hight = a.size() - 1;
  8.     
  9.     while(low <= hight)
  10.     {
  11.         int mid = (low + hight) / 2;

  12.         if(a[mid] < value)
  13.         {
  14.             low = mid + 1;
  15.         }
  16.         else if(a[mid] > value)
  17.         {
  18.             hight = mid - 1;
  19.         }
  20.         else
  21.             return mid;
  22.     }
  23.     
  24.     return -1;
  25. }
  26. int main()
  27. {
  28.     vector<int> a;
  29.     a.push_back(10);
  30.     a.push_back(11);
  31.     a.push_back(12);
  32.     a.push_back(13);
  33.     a.push_back(14);
  34.     a.push_back(15);
  35.     a.push_back(16);
  36.     a.push_back(17);
  37.     a.push_back(18);

  38.     cout<<"The location is at:"<<binaryFind(a,16)<<endl;
  39.     //Result is 6
  40.     return 0;
  41. }
      2、欧几里得算法 O(log N)
            两个整数的最大公约数(gcd)是同时整除二者的最大整数。gcd(50,15) = 5 ,gcd(M,N),假设M >= N。

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. //Write time: 2014.03.17
  6. //By :zhengxianle
  7. //欧几里得算法:计算最大公约数
  8. long gcd( long m , long n)
  9. {
  10.     while( n != 0 )
  11.     {
  12.         long ex = m % n;
  13.         m = n;
  14.         n = ex ;
  15.     }
  16.     return m;
  17. }
  18. int main ()
  19. {
  20.     cout<<gcd(50,15)<<endl;
  21.     //The result is : 5
  22.     return 0;
  23. }
      3、幂运算 O(log N)
           

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <time.h>
  5. using namespace std;

  6. //Write time: 2014.03.17
  7. //By :zhengxianle
  8. //幂运算:最少次数的乘法
  9. bool isEven(const int power)
  10. {
  11.     return power % 2 == 0 ? true : false ;
  12. }

  13. long double pow(long double x,int power)
  14. {
  15.     if(power < 0)
  16.     {
  17.         return 0;
  18.     }
  19.     else if(0 == power)
  20.     {
  21.         return 1;
  22.     }
  23.     else if(1 == power)
  24.     {
  25.         return x;
  26.     }
  27.     if(isEven(power))
  28.     {
  29.         return pow(x * x, power / 2);
  30.     }
  31.     else
  32.     {
  33.         return pow(x * x ,(power - 1) / 2) * x ;
  34.     }
  35. }
  36. int main ()
  37. {
  38.     long x = 0;
  39.     int power = 0;
  40.     cin>>x>>power;
  41.     cout<<"The "<<x<<"^ "<<power<<" is:"<<pow(x,power)<<endl;    
  42.     return 0;
  43. }


    
阅读(970) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~