偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何進(jìn)行C++ Builder的Visual構(gòu)件庫(kù)

開發(fā) 后端
其實(shí)C#的語(yǔ)法與C++非常相似,有些變化引起的錯(cuò)誤卻很嚴(yán)重,本文就對(duì)于大多數(shù)用戶頭疼的C++ Builder進(jìn)行詳細(xì)的剖析與介紹。

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

關(guān)于每個(gè)函數(shù)的詳細(xì)說(shuō)明和實(shí)例,見C++ Builder聯(lián)機(jī)幫助。

  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語(yǔ)言中的字串處理方法。大多數(shù)C++編譯器提供了cstring類,可以簡(jiǎn)化字串的處理(C++ Builder的Visual構(gòu)件庫(kù)中有個(gè)AnsiString類,可以處理字串操作。C++ Builder聯(lián)機(jī)幫助中詳細(xì)介紹了AnsiString類)。

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

  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個(gè)字符的字符數(shù)組,最初指定需要9個(gè)字節(jié)的字符串(記住終止null)。后來(lái)可能忘記了數(shù)組長(zhǎng)度,將需要16個(gè)字節(jié)的字串復(fù)制到了緩沖區(qū),對(duì)數(shù)組重載了六個(gè)字節(jié)。這個(gè)小小錯(cuò)誤就擦去了某個(gè)內(nèi)存位置上的六個(gè)字節(jié)。

所以將數(shù)據(jù)復(fù)制到字符數(shù)組中時(shí)要特別小心。另一個(gè)常用的字串函數(shù)是sprintf()。這個(gè)函數(shù)可以混合文本和數(shù)字建立格式化字串。下面例子將兩個(gè)數(shù)相加,然后用sprintf()建立字串以報(bào)告結(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ù)此處有個(gè)整型值,格式字串末尾插入變量x,告訴sprintf()在字串的這個(gè)位置放上變量x的值。sprintf()是個(gè)特別的函數(shù),可以取多個(gè)變?cè)?。你必須提供目?biāo)緩沖區(qū)和格式字串,但格式字串后面的變?cè)獢?shù)是個(gè)變量。下面的sprintf()例子用了另外三個(gè)變?cè)?

  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;  

許多編程人員因?yàn)橥诉@個(gè)簡(jiǎn)單的事實(shí)而夜不能寐,苦苦折騰。這是個(gè)常見的錯(cuò)誤,別說(shuō)我沒有告訴你。C++ Builder有個(gè)兄弟叫wsprintf(),是Windows版的sprintf().Windows程序中可能同時(shí)用這兩個(gè)函數(shù)。

wsprintf()與sprintf()的作用相似,***的差別是不能在格式字串中放上浮點(diǎn)數(shù)。C++ Builder程序中兩個(gè)函數(shù)均可使用,但用sprintf()更好,因?yàn)樗耆С指↑c(diǎn)數(shù)(還可以少輸入一個(gè)字符)。關(guān)于sprintf()的進(jìn)一步介紹,見C++ Builder聯(lián)機(jī)幫助。

【編輯推薦】

  1. 簡(jiǎn)介學(xué)習(xí)C++總結(jié)之談
  2. 對(duì)C++庫(kù)函數(shù)進(jìn)行學(xué)習(xí)探索總結(jié)筆記
  3. C++類庫(kù)設(shè)計(jì)的基本構(gòu)思與方法
  4. C++語(yǔ)言真的還有市場(chǎng)價(jià)值?
  5. C++類庫(kù)設(shè)計(jì)的基本構(gòu)思與方法
責(zé)任編輯:chenqingxiang 來(lái)源: NET130
相關(guān)推薦

2010-01-20 10:49:29

Visual C++界

2010-01-12 09:59:08

Visual C++

2010-01-22 16:21:50

C++ Builder

2010-01-20 09:32:30

C++類

2010-01-27 15:29:45

C++異常處理

2010-01-20 13:29:40

C++環(huán)境

2010-01-26 14:10:22

Visual C++

2010-02-02 13:04:03

C++頭文件

2014-09-19 10:46:36

LuaCC++

2010-01-15 19:05:42

學(xué)習(xí)C++

2010-02-05 17:16:05

C++構(gòu)造函數(shù)

2010-01-27 10:45:21

C++單例模式

2010-01-11 11:02:59

Visual C++

2009-12-10 17:19:05

Visual Stud

2010-01-08 17:13:46

Visual C++環(huán)

2010-01-12 15:13:37

Visual C++環(huán)

2010-01-12 14:22:26

Visual C++

2010-01-15 17:31:18

C++Test

2010-01-21 16:33:44

C++Builder

2010-01-15 09:58:38

C++ Builder
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)