如何进行C++Builder的Visual构件库

如果你用过具有string数据类型的编程语言,你可能很不习惯,别人也有同感,所以标准C++语言库中提供了几个字串操作函数,希望大家能够在这边文章中得到自己想要的信息。

从网站建设到定制行业解决方案,为提供成都网站建设、网站设计服务体系,各种行业企业客户提供网站建设解决方案,助力业务快速发展。成都创新互联公司将不断加快创新步伐,提供优质的建站服务。

关于每个函数的详细说明和实例,见C++ Builder联机帮助。

 
 
 
  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语言中的字串处理方法。大多数C++编译器提供了cstring类,可以简化字串的处理(C++ Builder的Visual构件库中有个AnsiString类,可以处理字串操作。C++ Builder联机帮助中详细介绍了AnsiString类)。

尽管C语言中的字串处理方法比较麻烦,但并不过时,C++编程人员经常在使用cstring类和AnsiString类等字串类的同时使用C语言中的字串处理方法。这里不想对表中的每个函数进行举例说明,只想举两个最常用的函数。strcpy()函数将一个字串复制到另一字串中,源字串可以是变量或直接字串。例如下列代码:

 
 
 
  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个字符的字符数组,最初指定需要9个字节的字符串(记住终止null)。后来可能忘记了数组长度,将需要16个字节的字串复制到了缓冲区,对数组重载了六个字节。这个小小错误就擦去了某个内存位置上的六个字节。

所以将数据复制到字符数组中时要特别小心。另一个常用的字串函数是sprintf()。这个函数可以混合文本和数字建立格式化字串。下面例子将两个数相加,然后用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;  

本例中%d告诉sprintf()函数此处有个整型值,格式字串末尾插入变量x,告诉sprintf()在字串的这个位置放上变量x的值。sprintf()是个特别的函数,可以取多个变元。你必须提供目标缓冲区和格式字串,但格式字串后面的变元数是个变量。下面的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程序中可能同时用这两个函数。

wsprintf()与sprintf()的作用相似,***的差别是不能在格式字串中放上浮点数。C++ Builder程序中两个函数均可使用,但用sprintf()更好,因为它完全支持浮点数(还可以少输入一个字符)。关于sprintf()的进一步介绍,见C++ Builder联机帮助。

【编辑推荐】

  1. 简介学习C++总结之谈
  2. 对C++库函数进行学习探索总结笔记
  3. C++类库设计的基本构思与方法
  4. C++语言真的还有市场价值?
  5. C++类库设计的基本构思与方法

文章标题:如何进行C++Builder的Visual构件库
URL分享:http://www.hantingmc.com/qtweb/news19/5219.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联