C/C++±ÊÊÔÃæÊÔÌâÄ¿
1£®ÒÑÖªstrcpy º¯ÊýµÄÔÐÍÊÇ£º
char *strcpy(char *strDest, const char *strSrc);
ÆäÖÐstrDest ÊÇÄ¿µÄ×Ö·û´®£¬strSrc ÊÇÔ´×Ö·û´®¡£²»µ÷ÓÃC++/C µÄ×Ö·û´®¿âº¯Êý£¬Çë±àдº¯Êý strcpy
´ð°¸£º
char *strcpy(char *strDest, const char *strSrc)
{
if ( strDest == NULL || strSrc == NULL)
return NULL ;
if ( strDest == strSrc)
return strDest ;
char *tempptr = strDest ;
while( (*strDest++ = *strSrc++) != ‘’)
;
return tempptr ;
}
2£®ÒÑÖªÀàString µÄÔÐÍΪ£º
class String
{
public:
String(const char *str = NULL); // ÆÕͨ¹¹Ô캯Êý
String(const String &other); // ¿½±´¹¹Ô캯Êý
~ String(void); // Îö¹¹º¯Êý
String & operate =(const String &other); // ¸³Öµº¯Êý
private:
char *m_data; // ÓÃÓÚ±£´æ×Ö·û´®
};
Çë±àдString µÄÉÏÊö4 ¸öº¯Êý¡£
´ð°¸£º
String::String(const char *str)
{
if ( str == NULL ) //strlenÔÚ²ÎÊýΪNULLʱ»áÅ×Òì³£²Å»áÓÐÕâ²½ÅжÏ
{
m_data = new char[1] ;
m_data[0] = '' ;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}
String::String(const String &other)
{
m_data = new char[strlen(other.m_data) + 1];
strcpy(m_data,other.m_data);
}
String & String::operator =(const String &other)
{
if ( this == &other)
return *this ;
delete []m_data;
m_data = new char[strlen(other.m_data) + 1];
strcpy(m_data,other.m_data);
return *this ;
}
String::~ String(void)
{
delete []m_data ;
}
3.¼ò´ð
3.1 Í·ÎļþÖеÄifndef/define/endif ¸ÉʲôÓã¿
´ð£º·ÀÖ¹¸ÃÍ·Îļþ±»Öظ´ÒýÓá£
3.2££i nclude <filename.h> ºÍ££i nclude “filename.h” ÓÐʲôÇø±ð£¿
´ð£º¶ÔÓÚ££i nclude <filename.h> £¬±àÒëÆ÷´Ó±ê×¼¿â·¾¶¿ªÊ¼ËÑË÷filename.h
¶ÔÓÚ££i nclude “filename.h”£¬±àÒëÆ÷´ÓÓû§µÄ¹¤×÷·¾¶¿ªÊ¼ËÑË÷filename.h
3.3 ÔÚC++ ³ÌÐòÖе÷Óñ»C ±àÒëÆ÷±àÒëºóµÄº¯Êý£¬ÎªÊ²Ã´Òª¼Óextern “C”£¿
´ð£ºC++ÓïÑÔÖ§³Öº¯ÊýÖØÔØ£¬C ÓïÑÔ²»Ö§³Öº¯ÊýÖØÔØ¡£º¯Êý±»C++±àÒëºóÔÚ¿âÖеÄÃû×ÖÓëC ÓïÑԵIJ»Í¬¡£¼ÙÉèij¸öº¯ÊýµÄÔÐÍΪ£º void foo(int x, int y);
¸Ãº¯Êý±»C ±àÒëÆ÷±àÒëºóÔÚ¿âÖеÄÃû×ÖΪ_foo £¬ ¶øC++ ±àÒëÆ÷Ôò»á²úÉúÏñ_foo_int_int
Ïà¹ØÎĵµ£º
(1)
Êý×éÃûµÄÄÚºÔÚÓÚÆäÖ¸´úʵÌåÊÇÒ»ÖÖÊý¾Ý½á¹¹£¬ÕâÖÖÊý¾Ý½á¹¹¾ÍÊÇÊý×飻
(2)
Êý×éÃûµÄÍâÑÓÔÚÓÚÆä¿ÉÒÔת»»ÎªÖ¸ÏòÆäÖ¸´úʵÌåµÄÖ¸Õ룬¶øÇÒÊÇÒ»¸öÖ¸Õë³£Á¿£»
(3)
Ö¸ÏòÊý×éµÄÖ¸ÕëÔòÊÇÁíÍâÒ»ÖÖ±äÁ¿ÀàÐÍ£¨ÔÚWIN32ƽ̨Ï£¬³¤¶ÈΪ4£©£¬½ö½öÒâζ×ÅÊý×éµÄ´æ·ÅµØÖ·
£¨4£©
Êý×éÃû×÷Ϊº¯ÊýÐβÎʱ£¬ÔÚº¯ÊýÌåÄÚ£¬ÆäʧȥÁ˱¾ÉíµÄÄÚº ......
CµÄËÄÖÖ´æ´¢ÀàÏÞ¶¨·û£º
extern:Íⲿ±äÁ¿
static £º¾²Ì¬
register£º´æ´¢Æ÷
auto£º×Ô¶¯¡¡
¾²Ì¬¾Ö²¿±äÁ¿£º
´æ´¢ÇøºÍÈ«¾Ö±äÁ¿ÊÇÒ»ÑùµÄ¡£¾²Ì¬¾Ö²¿ºÍÈ«¾Ö±äÁ¿¼äµÄÖ÷ÒªÇø±ðÊÇ£º¾²Ì¬¾Ö²¿±äÁ¿½ö¶ÔËüÔÚÆäÖÐÉùÃ÷µÄ¿éÊÇ¿ÉÖªµÄ¡£
¾²Ì¬È«¾Ö±äÁ¿£º
¸æËß±àÒëÆ÷´´½¨Ò»¸ö½öÔÚÉùÃ÷ËüµÄÎļþÖпÉÖªµÄÈ«¾Ö±äÁ¿¡£ÕâÒâζ×Å£º¼´Ê¹±äÁ¿Ê ......
//ÊäÈëÒ»¸öÊý×飬ÔÙÐÞ¸ÄÕâ¸öÊý×éËùÓÐÔªËØ£¬ÈçºÎʵÏÖ£¿
int main()
{
vector<int> a;
int i(0);
while(cin>>i)
a.push_back(i);
//////////////////////////////////////////////////////Êä³ö½¨Á¢µÄÊý×飺
cout << "µÃµ½µÄÊý×éΪ£º" << ......
¶þ·Ö²éÕҵĴúÂë.
int bfind(int* a,int len,int val)
{
int m = len/2;
int l = 0;
int r = len;
while(l!=m && r!= m)
{
if(a[m] > val)
& ......