温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎样进行C++ Builder的Visual构件库

发布时间:2021-10-27 17:31:12 来源:亿速云 阅读:127 作者:柒染 栏目:编程语言

怎样进行C++ Builder的Visual构件库,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

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

//set up a string to hold 29 characters    char buff[30];    //copy a string literal to the buffer    strcpy (buff,"This is a test.");//display it    cout << buff << end;    //initialize a second string buffer    char buff2[]="A second string.";    //copy the contents of this string to the first buffer    strcpy (buff,buff2);    cout << buff << end1;

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

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

//set up a string to hold 29 characters    char buff[30];    //copy a string literal to the buffer    strcpy (buff,"This is a test.");//display it    cout << buff << end;    //initialize a second string buffer    char buff2[]="A second string.";    //copy the contents of this string to the first buffer    strcpy (buff,buff2);    cout << buff << end1;

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

所以将数据复制到字符数组中时要特别小心。另一个常用的字串函数是sprintf()。这个函数可以混合文本和数字建立格式化字串。下面例子将两个数相加,然后用sprintf()建立字串以报告结果:

//set up a string to hold 29 characters    char buff[30];    //copy a string literal to the buffer    strcpy (buff,"This is a test.");//display it    cout << buff << end;    //initialize a second string buffer    char buff2[]="A second string.";    //copy the contents of this string to the first buffer    strcpy (buff,buff2);    cout << buff << end1;

%d告诉sprintf()函数此处有个整型值,格式字串末尾插入变量x,告诉sprintf()在字串的这个位置放上变量x的值。sprintf()是个特别的函数,可以取多个变元。你必须提供目标缓冲区和格式字串,但格式字串后面的变元数是个变量。下面的sprintf()例子用了另外三个变元:

//set up a string to hold 29 characters    char buff[30];    //copy a string literal to the buffer    strcpy (buff,"This is a test.");//display it    cout << buff << end;    //initialize a second string buffer    char buff2[]="A second string.";    //copy the contents of this string to the first buffer    strcpy (buff,buff2);    cout << buff << end1;

许多编程人员因为忘了这个简单的事实而夜不能寐,苦苦折腾。这是个常见的错误,别说我没有告诉你。C++ Builder有个兄弟叫wsprintf(),是Windows版的sprintf().Windows程序中可能同时用这两个函数。

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

看完上述内容,你们掌握怎样进行C++ Builder的Visual构件库的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI