日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何進行C++Builder的Visual構(gòu)件庫

如果你用過具有string數(shù)據(jù)類型的編程語言,你可能很不習慣,別人也有同感,所以標準C++語言庫中提供了幾個字串操作函數(shù),希望大家能夠在這邊文章中得到自己想要的信息。

從網(wǎng)站建設(shè)到定制行業(yè)解決方案,為提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計服務(wù)體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設(shè)解決方案,助力業(yè)務(wù)快速發(fā)展。成都創(chuàng)新互聯(lián)公司將不斷加快創(chuàng)新步伐,提供優(yōu)質(zhì)的建站服務(wù)。

關(guān)于每個函數(shù)的詳細說明和實例,見C++ Builder聯(lián)機幫助。

 
 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

這里介紹的字串操作是C語言中的字串處理方法。大多數(shù)C++編譯器提供了cstring類,可以簡化字串的處理(C++ Builder的Visual構(gòu)件庫中有個AnsiString類,可以處理字串操作。C++ Builder聯(lián)機幫助中詳細介紹了AnsiString類)。

盡管C語言中的字串處理方法比較麻煩,但并不過時,C++編程人員經(jīng)常在使用cstring類和AnsiString類等字串類的同時使用C語言中的字串處理方法。這里不想對表中的每個函數(shù)進行舉例說明,只想舉兩個最常用的函數(shù)。strcpy()函數(shù)將一個字串復制到另一字串中,源字串可以是變量或直接字串。例如下列代碼:

 
 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

這里建立了放10個字符的字符數(shù)組,最初指定需要9個字節(jié)的字符串(記住終止null)。后來可能忘記了數(shù)組長度,將需要16個字節(jié)的字串復制到了緩沖區(qū),對數(shù)組重載了六個字節(jié)。這個小小錯誤就擦去了某個內(nèi)存位置上的六個字節(jié)。

所以將數(shù)據(jù)復制到字符數(shù)組中時要特別小心。另一個常用的字串函數(shù)是sprintf()。這個函數(shù)可以混合文本和數(shù)字建立格式化字串。下面例子將兩個數(shù)相加,然后用sprintf()建立字串以報告結(jié)果:

 
 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

本例中%d告訴sprintf()函數(shù)此處有個整型值,格式字串末尾插入變量x,告訴sprintf()在字串的這個位置放上變量x的值。sprintf()是個特別的函數(shù),可以取多個變元。你必須提供目標緩沖區(qū)和格式字串,但格式字串后面的變元數(shù)是個變量。下面的sprintf()例子用了另外三個變元:

 
 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

許多編程人員因為忘了這個簡單的事實而夜不能寐,苦苦折騰。這是個常見的錯誤,別說我沒有告訴你。C++ Builder有個兄弟叫wsprintf(),是Windows版的sprintf().Windows程序中可能同時用這兩個函數(shù)。

wsprintf()與sprintf()的作用相似,***的差別是不能在格式字串中放上浮點數(shù)。C++ Builder程序中兩個函數(shù)均可使用,但用sprintf()更好,因為它完全支持浮點數(shù)(還可以少輸入一個字符)。關(guān)于sprintf()的進一步介紹,見C++ Builder聯(lián)機幫助。

【編輯推薦】

  1. 簡介學習C++總結(jié)之談
  2. 對C++庫函數(shù)進行學習探索總結(jié)筆記
  3. C++類庫設(shè)計的基本構(gòu)思與方法
  4. C++語言真的還有市場價值?
  5. C++類庫設(shè)計的基本構(gòu)思與方法

文章標題:如何進行C++Builder的Visual構(gòu)件庫
本文來源:http://m.5511xx.com/article/dhdodod.html