C++ÖеÄÊý¾ÝÀàÐÍת»»·½·¨
ÕªÒª£º±¾ÎÄ×ܽáÁËC/C++ÖеĶàÖÖÊý¾ÝÀàÐÍת»»·½·¨£¬²¢±È½ÏÁ˸÷×ÔµÄÓÅÁÓ¡£¸ø³öÁËÍƼöµÄʹÓý¨Òé¡£
´Óintµ½char*£¬»òÕß·´¹ýÀ´´Óchar*µ½int£¬ÔÚC/C++Öе½µ×ÓжàÉÙÖÖת»»·½·¨ÄØ£¿·ûºÏ±ê×¼µÄ´ó¸ÅÓÐËÄÖÖ¡£¼´CÊý¾Ýת»»º¯Êý×å¡¢sprintf/snprintf/sscanfº¯Êý×å¡¢×Ö·û´®Á÷std::stringstream¡¢std::strsteam¡£²»·ûºÏ±ê׼ȴÓÖ¹ãΪʹÓõİüÀ¨CStringºÍboost::lexical_cast¡£±¾ÎÄÖ»ÌÖÂÛ·ûºÏ±ê×¼µÄת»»·½·¨£¬ÆäÖÐstd::strstreamÓÉÓÚÒѾ±»C++±ê׼ίԱΪָ¶¨Îª²»ÍƼöʹÓõģ¨deprecated£©£¬ËùÒÔ²»Ó迼ÂÇÁË¡£ÏÂÃæÖصãÌÖÂÛÈýÖÖ±ê׼ת»»·½·¨Ö®¼äµÄÓÅÁÓ¡£Ô´´úÂëµÄµØÖ·ÊÇ£ºhttp://download.csdn.net/source/631475
1. IntºÍchar*»òstd::stringÖ®¼äµÄת»»
CÊý¾Ýת»»º¯Êý×å
CÊý¾Ýת»»º¯Êý×å¼´°üÀ¨itoa¡¢atoiµÈÊý¾ÝÀàÐÍת»»º¯ÊýÔÚÄÚµÄÒ»´óÅúCº¯Êý£¬ÔÚCÓïÑÔʱ´úÔø¾±»´óÁ¿Ê¹Óá£Ô´´úÂëÈçÏ£º
int i = 10;
char szBuf[10] = "";
itoa(i, szBuf, 10);
cout<<"itoa: szBuf = "<<szBuf<<endl;
i = 0;
i = atoi(szBuf);
cout<<"atoi: i = "<<i<<endl;
ʹÓû¹ÊDZȽϼòµ¥µÄ¡£Ò»¸ö×î´óµÄÎÊÌâÊÇ£ºÃ»ÓнøÐÐchar*µÄÔ½ ......
ÁùÄêÇ°£¬ÎÒ¸ÕÈÈÁµ“ÃæÏò¶ÔÏ󔣨Object-Oriented£©Ê±£¬Ò»¿ÚÆø¼ÇסÁ˽üÊ®¸ö¶¨Òå¡£ÁùÄêºó£¬ÎÒ´Ó¼¸Ê®ÍòÐгÌÐòÖйöÅÀ³öÀ´×¼±¸Ð´µãÐĵÃÌå»áʱ£¬È´ÎÞ·¨½âÊÍʲôÊÇ“ÃæÏò¶ÔÏ󔣬¾ÍÏó˵²»Çå³þʲôÊÇÊýѧÄÇÑù¡£Èí¼þ¹¤³ÌÖеÄʱ÷ÖÊõÓï“ÃæÏò¶ÔÏó·ÖÎö”ºÍ“ÃæÏò¶ÔÏóÉè¼Æ”£¬Í¨³£ÊÇÕë¶Ô“ÐèÇó·ÖÎö”ºÍ“ϵͳÉè¼Æ”»·½ÚµÄ¡£“ÃæÏò¶ÔÏó”Óм¸´óѧÅÉ£¬¾ÍÏóÈçÀ´·ð¡¢ÉϵۺÍÕæÖ÷Óø÷×Եķ½Ê½¶¨ÒåÁËÕâ¸öÊÀ½ç£¬²¢ÁôÏÂÒ»¶Ñ¾ÊéÀ´½âÊÍÕâ¸öÊÀ½ç¡£
¡¡¡¡ÓÐЩѧÕß½¨ÒéÕâÑùÕÒ“¶ÔÏ󔣺·ÖÎöÒ»¸ö¾ä×ÓµÄÓï·¨£¬ÕÒ³öÃû´ÊºÍ¶¯´Ê£¬Ãû´Ê¾ÍÊǶÔÏ󣬶¯´ÊÔòÊǶÔÏóµÄ·½·¨£¨¼´º¯Êý£©¡£
¡¡¡¡µ±Äê¹úÃñµ³µÄÎÄÈËΪÁ˶Կ¹Ã«Ô󶫵ġ¶ÇßÔ°´º·Ñ©¡·£¬ÌØÒâÇëÇ峯ÒÅÀÏÃÇдÁËһЩ¶ÔÕ̹¤ÕûµÄÊ«£¬Ç뽯½éʯ¹ýÄ¿¡£ÀϽ¯¿´ÁËÆøµÃ´óÂ“Äïϣƥ£¬È«¶¼ÓÐÒ»¹É¹×²ÄÀ︯ʬµÄÆøζ¡£”ÎÒ¿´Á˼¸Ç§Ò³µÄÈí¼þ¹¤³Ì×ÊÁÏ£¬ÖÕÓÚ·¢ÏÖ×Ô¼ºÓÐЩ“ÈõÖÇ”£¬ÎÞ·¨Àí½â“ÃæÏò¶ÔÏó”µÄÀíÂÛ£¬Í¬Ê±ÐÑÎòµ½“±à³ÌÊÇÓ²µÀÀí¡£”
¡¡¡¡ÃæÏò¶ÔÏó³ÌÐòÉè¼ÆÓïÑԺܶ࣬ÈçSmalltalk¡¢Ada¡¢Eiffel¡¢Object Pascal¡¢Visual
Basic¡¢C++µ ......
À´ÁËcsdnºÜ¾ÃÁË£¬¶¼Ã»Ð´¹ýÎÄÕ£¬Óеã¹ýÒⲻȥ£¬ÓÚÊDZÆ×Å×Ô¼ºÕâ¸öÀÁÈËдÁËÒ»ÏÂһЩ±£ÁôÔÚ¸öÈ˱ʼDZ¾ÉϵÄСµãµÎ£¬Ï£Íû²»»áÎóµ¼´ó¼Ò¡£¡£¡£¡£¡£Ò»ÏÂÖ»ÊDZʼÇÖеÄһС²¿·Ö£¬ÒÔºóÓпÕÔÚ¼ÌÐø·ÅÉÏÀ´¡£¡£¡£¡£¡£¡£
1 Ð麯ÊýÊÇΪÁ˼̳нӿںÍĬÈÏÐÐΪ¡£´¿Ð麯ÊýÖ»ÊǼ̳нӿڣ¬ÐÐΪ±ØÐëÖØж¨Òå¡£
2 ¼¯ºÍ¶à¼¯µÄÇø±ð£ºsetÖ§³ÖΨһ¼üÖµ£¬setÖеÄÖµ¶¼ÊÇÌض¨µÄ£¬¶øÇÒÖ»³öÏÖÒ»´Î£»¶ømultsetÖпÉÒÔ³öÏÖ¸±±¾¼ü£¬Í¬Ò»¸öÖµ¿ÉÒÔ³öÏÖ¶à´Î¡£
3 mapµÄ½ÚµãÊÇÒ»¶ÔÊý¾Ý¡£
setµÄ½ÚµãÊÇÒ»¸öÊý¾Ý¡£
setÖеÄkeyºÍvalueÊÇkeyÀàÐ͵ġ£
mapÖеÄkeyºÍvalueÊÇÒ»¸öpair½á¹¹ÖеÄÁ½¸ö·ÖÁ¿¡£
4 c++µÄÒ»°ã¹æÔòÊÇ£¬³ýÁËÒÔtypenameÐÞÊÎÖ®Í⣬templateÄÚµÄÈκαíʾ·ûºÅ¶¼±»ÊÓΪһ¸öÖµ¶ø·ÇÐͱð¡£
5 ¾²Ì¬³ÉÔ±±äÁ¿µÄ³õʼ»¯Ó¦¸ÃÔÚÖ÷º¯Êýµ÷ÓÃ֮ǰ£¬²¢ÇÒ²»ÄÜÔÚÀàµÄÉùÃ÷ÖгöÏÖ¡£
ÀàµÄ¾²Ì¬³ÉÔ±ÊÇÊôÓÚÀàµÄ¶ø²»ÊÇÊôÓÚÒ»°ã¶ÔÏóµÄ£¬¾²Ì¬³ÉÔ±µÄ״̬²»»áËæ×ÅÒ»¸öеĶÔÏóµÄн¨¶øÖØж¨Òå¡£
6 ¾²Ì¬³ÉÔ±º¯ÊýÓÉÓÚÓë¶ÔÏóÎ޹أ¬ËùÒÔÔÚÆäÖÐÊDz»ÄܶÔÀàµÄÆÕͨ³ÉÔ±½øÐвÙ×÷µÄ¡£
¾²Ì¬³ÉÔ±º¯ÊýÓëÆÕͨ³ÉÔ±º¯ÊýµÄÇø±ðÔÚÓÚȱÉÙthisÖ¸Õë¡£
7 ÓÑÔªµÄ×÷ÓÃÊÇÌá¸ß³ÌÐòµ ......
C++ÖеÄStaticÓÐÁ½¸ö×÷Óá£
1¡¢ ¾²Ì¬¾Ö²¿±äÁ¿
¾²Ì¬¾Ö²¿±äÁ¿ÔÚº¯ÊýÄÚ¶¨Òå ËüµÄÉú´æÆÚΪÕû¸öÔ´³ÌÐò£¬µ«ÊÇÆä×÷ÓÃÓòÔڸú¯ÊýÄÚ£¬Ö»ÄÜÔÚ¶¨Òå¸Ã±äÁ¿µÄº¯ÊýÄÚʹÓøñäÁ¿¡£ ÔÊÐí¶Ô¹¹ÔìÀྲ̬¾Ö²¿Á¿¸³³õÖµ£¬Èôδ¸³ÒÔ³õÖµ£¬ÔòÓÉϵͳ×Ô¶¯¸³ÒÔ0Öµ¡£
void fun1()
{
static int i = 0;
cout << ++i << endl;
}
int main()
{
for(int i = 0; i < 5; i++)
{
fun1();
}
}
Output :
1
2
3
4
2¡¢¾²Ì¬È«¾Ö±äÁ¿
È«¾Ö±äÁ¿Ç°ÔÙ¼ÓÉÏstatic ¾Í³ÉÁ˾²Ì¬µÄÈ«¾Ö±äÁ¿¡£È«¾Ö±äÁ¿±¾Éí¾ÍÊǾ²Ì¬´æ´¢·½Ê½£¬ ¾²Ì¬È«¾Ö±äÁ¿µ±È»Ò²ÊǾ²Ì¬´æ´¢·½Ê½¡£È«¾Ö±äÁ¿×÷ÓÃÓòÊÇÕû¸öÔ´³ÌÐò£¬¶ø¾²Ì¬È«¾Ö±äÁ¿×÷ÓÃÓòÈ·ÊǸÃÎļþ¡£¾²Ì¬º¯ÊýÀàËÆ¡£
......
C++ÖеÄStaticÓÐÁ½¸ö×÷Óá£
1¡¢ ¾²Ì¬¾Ö²¿±äÁ¿
¾²Ì¬¾Ö²¿±äÁ¿ÔÚº¯ÊýÄÚ¶¨Òå ËüµÄÉú´æÆÚΪÕû¸öÔ´³ÌÐò£¬µ«ÊÇÆä×÷ÓÃÓòÔڸú¯ÊýÄÚ£¬Ö»ÄÜÔÚ¶¨Òå¸Ã±äÁ¿µÄº¯ÊýÄÚʹÓøñäÁ¿¡£ ÔÊÐí¶Ô¹¹ÔìÀྲ̬¾Ö²¿Á¿¸³³õÖµ£¬Èôδ¸³ÒÔ³õÖµ£¬ÔòÓÉϵͳ×Ô¶¯¸³ÒÔ0Öµ¡£
void fun1()
{
static int i = 0;
cout << ++i << endl;
}
int main()
{
for(int i = 0; i < 5; i++)
{
fun1();
}
}
Output :
1
2
3
4
2¡¢¾²Ì¬È«¾Ö±äÁ¿
È«¾Ö±äÁ¿Ç°ÔÙ¼ÓÉÏstatic ¾Í³ÉÁ˾²Ì¬µÄÈ«¾Ö±äÁ¿¡£È«¾Ö±äÁ¿±¾Éí¾ÍÊǾ²Ì¬´æ´¢·½Ê½£¬ ¾²Ì¬È«¾Ö±äÁ¿µ±È»Ò²ÊǾ²Ì¬´æ´¢·½Ê½¡£È«¾Ö±äÁ¿×÷ÓÃÓòÊÇÕû¸öÔ´³ÌÐò£¬¶ø¾²Ì¬È«¾Ö±äÁ¿×÷ÓÃÓòÈ·ÊǸÃÎļþ¡£¾²Ì¬º¯ÊýÀàËÆ¡£
......
¹ØÓÚ×éºÏÊý¼ÆËãµÄËã·¨ÎÒдÁ˺ü¸¸ö£¬·¢ÏÖÑî»ÔÈý½Ç²é±í·¨Ð§ÂʱȽϸߣ¡
²¢ÇÒÕâ¸öËã·¨ÀûÓÃC(n,k) = C(n,n-k)À´¼ò»¯Ñî»ÔÈý½ÇµÄÊý×飬Ëã·¨µÄʱ¼ä¸´ÔӶȱȽϵ͡£
Ô´´£¬Ë®Æ½ÓÐÏÞ£¬ÈçÓв»×㣬»¶ÓÖ¸³ö¡£
Mail£ºrainday163@163.com
ÏÂÃæÊÇÎÒдµÄ´úÂë²¢¸½´ø²âÊÔº¯Êý£ºDev-C++ 4.9.9.2ϲâÊÔͨ¹ý¡£
/*---------------------------------------------------------------------
//file name:combination.cpp
//Coder: rainday163
//E-mail: rainday1631@163.com
//Create date: 2009.9.4
//Last modify date: 2009.9.28
//Test platform: WinXP sp2 & Dev-C++ 4.9.9.2
---------------------------------------------------------------------*/
#include <typeinfo>
#include <vector>
#include <stdexcept>
#include <iostream>
template <typename T1,typename T2>
T1 combination(T2 const &n,T2 const &k)
{
//ÀàÐͼì²é
if( !( (typeid(T1) == typeid(unsigned long long))
||(typeid(T1) == typeid(long long))
||(typeid(T1) == typeid(unsigned lo ......
char c[] = "hello world" ·ÖÅä¾Ö²¿Êý×é
char* c = "hello world" ·ÖÅäÈ«¾ÖÊý×é È«¾ÖÇøÓòµÄÖµÊDz»ÄܽøÐÐÐ޸ĵÄ
1 ³£Á¿Ö¸Õ룡Èç int b, c; int * const a = &b;
±íʾaÊÇÒ»¸ö³£Á¿Ö¸ÕëËüÖ¸Ïò±äÁ¿bµÄÄÚ´æ¡£µ«ÊÇÒòΪÊdz£Á¿Ö¸ÕëËùÒÔ²»ÄÜÔÙÓÃaÖ¸ÏòÆäËû±äÁ¿£¬Èç a = &c; ´íÎ󣡿ÉÒÔÐÞ¸ÄÖ¸ÏòÄÚ´æµÄÖµ£¬Èç:*a = 20; BTW ³£Á¿Ö¸ÕëÉùÃ÷µÄʱºò±ØÐëÏòÎÒÄÇÑù¸³³õÖµ¡£
2 Ö¸Ïò³£Á¿µÄÖ¸Õ룡Èç int b, c; int const *a; a = &b; a = &c;
¶¼¿ÉÒÔ£¬Î¨¶ÀËüÖ¸ÏòµÄÄÚ´æ²»Äܱ»Ð޸ġ£È磺*a=20;ÕâÊÇÎ¥·¨µÄ£¡´íÎó£¡
Õâ¾ÍÊÇÖ÷ÒªÇø±ð£¡
BTW »¹ÓÐÒ»¸ö¼ÇסËûÃDz»Í¬ÐÎʽµÄ¼¼ÇÉ£¡¿´const¹Ø¼ü×Ö£¬ËûºóÃæµÄ²»¿ÉÐ޸ģ¬Èçint * const a = &b; ºóÃæÊÇa,Ôò˵Ã÷a²»ÄÜÐ޸ģ¡
int const * a = &b;ºóÃæÊÇ*aÔò˵Ã÷*a²»¿É±»Ð޸ģ¡
ÔںöàÊéÉÏ»òMSDNÊǾ³£Óà const int a=5;
int b=6;
const int *p=&b;
Æäʵ const int* ºÍint const* Ò»Ñù£¬¾ÍÊdz£Ö¸Õë Ò²¾ÍÊÇËüËùÖ¸ÏòµÄÊý¾Ý£¨ÔÚÕâÊÇint£©Êdz£Á¿£¬Ëü×Ô¼ºµÄÊý¾ÝÀàÐÍÊÇconst int*
»¹ÓÐconst int *p=&b;ÊÇ¿ÉÒ﵀ ËäÈ»b²»Êdz£Á¿¡£
µ«ÊÇ const int a=6;
&nbs ......
char c[] = "hello world" ·ÖÅä¾Ö²¿Êý×é
char* c = "hello world" ·ÖÅäÈ«¾ÖÊý×é È«¾ÖÇøÓòµÄÖµÊDz»ÄܽøÐÐÐ޸ĵÄ
1 ³£Á¿Ö¸Õ룡Èç int b, c; int * const a = &b;
±íʾaÊÇÒ»¸ö³£Á¿Ö¸ÕëËüÖ¸Ïò±äÁ¿bµÄÄÚ´æ¡£µ«ÊÇÒòΪÊdz£Á¿Ö¸ÕëËùÒÔ²»ÄÜÔÙÓÃaÖ¸ÏòÆäËû±äÁ¿£¬Èç a = &c; ´íÎ󣡿ÉÒÔÐÞ¸ÄÖ¸ÏòÄÚ´æµÄÖµ£¬Èç:*a = 20; BTW ³£Á¿Ö¸ÕëÉùÃ÷µÄʱºò±ØÐëÏòÎÒÄÇÑù¸³³õÖµ¡£
2 Ö¸Ïò³£Á¿µÄÖ¸Õ룡Èç int b, c; int const *a; a = &b; a = &c;
¶¼¿ÉÒÔ£¬Î¨¶ÀËüÖ¸ÏòµÄÄÚ´æ²»Äܱ»Ð޸ġ£È磺*a=20;ÕâÊÇÎ¥·¨µÄ£¡´íÎó£¡
Õâ¾ÍÊÇÖ÷ÒªÇø±ð£¡
BTW »¹ÓÐÒ»¸ö¼ÇסËûÃDz»Í¬ÐÎʽµÄ¼¼ÇÉ£¡¿´const¹Ø¼ü×Ö£¬ËûºóÃæµÄ²»¿ÉÐ޸ģ¬Èçint * const a = &b; ºóÃæÊÇa,Ôò˵Ã÷a²»ÄÜÐ޸ģ¡
int const * a = &b;ºóÃæÊÇ*aÔò˵Ã÷*a²»¿É±»Ð޸ģ¡
ÔںöàÊéÉÏ»òMSDNÊǾ³£Óà const int a=5;
int b=6;
const int *p=&b;
Æäʵ const int* ºÍint const* Ò»Ñù£¬¾ÍÊdz£Ö¸Õë Ò²¾ÍÊÇËüËùÖ¸ÏòµÄÊý¾Ý£¨ÔÚÕâÊÇint£©Êdz£Á¿£¬Ëü×Ô¼ºµÄÊý¾ÝÀàÐÍÊÇconst int*
»¹ÓÐconst int *p=&b;ÊÇ¿ÉÒ﵀ ËäÈ»b²»Êdz£Á¿¡£
µ«ÊÇ const int a=6;
&nbs ......