博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thread: C++ String: How to assign or compare strings?
阅读量:6578 次
发布时间:2019-06-24

本文共 2933 字,大约阅读时间需要 9 分钟。

Thread:

    • Linear Mode
    Gabriel Fleseriu is offline
    Moderator/Microsoft MVP
    Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    C++ String: How to assign or compare strings?

    Q: How to assign or compare strings?
    A: Assignment and comparison are not necessarily related, but we decided to handle them together because the mistakes made by programmers beginning with C++ have a common root, that is, the tendence to use a 'natural' syntax.
    Assignment
    This actually is an issue only when working with C-style strings, i.e. 'char*'. The most common mistake is this:
    Code:
    char *s1, *s2;//...s2 = s1; // might not do what one would think
    This statement assigns to the char pointer 's2' the same vale as 's1'. With other words, 's1' and 's2' will point to one and the same address in memory. By no means is the string 's1' copied into 's2'.
    Another mistake that is frequently made by beginners is something like this:
    Code:
    char* s = "Hello";s[1] = 'a';             // attempting to change the 'e' to an 'a'//...
    Chances are to get an access violation when running this code. The reason is that, again, the assignment applies to the char pointer 's', i.e. it will point to the address of the constant string literal "Hello'. Since the compiler is allowed to place such const literals in read-only memory, attempting to modify it has undefined results.
    The correct solution is to use the 'strcpy()' function or one of its relatives.
    Code:
    char *s;s = new char[strlen("Hello") + 1];  // reserve one byte for the trailing '\0'strcpy(s, "Hello");//...delete[] s;
    Both 'CString' and the Standard C++ class 'std::string' are user-friendly and take care by themselves of this details. This code is perfectly ok:
    Code:
    #include 
    std::string s;s = "Hello";s[1] = 'a';
    Comparison
    This also is an issue only when working with C-style strings, i.e. 'char*'. The most common mistake is this:
    Code:
    char *s1, *s2;if(s1 == s2)       // wrong!!!!
    This statement may seem to be the natural syntax, but it unfortunately compares the value of two char pointers and by no means the strings they point to. To compare C-style strings you have to use the 'strcmp()' function or one of its relatives:
    Code:
    char *s1, *s2;if(!strcmp(s1, s2))
    Note, that 'strcmp()' returns 0 if the strings are identical, thus, the '
    !strcmp(...)' in the if statement. Have a look in e.g. for further details on 'strcmp()' and other string comparison functions.
    Both 'CString' and the Standard C++ class 'std::string' have overloaded comparison operators, which leads to a more natural syntax:
    Code:
    #include 
    std::string s1, s2;if(s1 == s2)
    All this being said, it should be clear why you always should prefere working with a string wrapper class over working with C-style strings. And there is much more std::string can do for you than this!
    Last edited by Andreas Masur; July 24th, 2005 at
    07:32 AM.

转载地址:http://kiyno.baihongyu.com/

你可能感兴趣的文章
关于Apache (httpd)服务器防DDOS模块mod_evasive的使用说明
查看>>
雕虫小技---仅复制可见单元格的技巧
查看>>
Python练手,封装日志模块,v2
查看>>
Android 屏幕适配
查看>>
暗黑3发布!又有多少人为之疯狂
查看>>
Android 照片墙
查看>>
20140419-MCSA 2012 Server R2 Command
查看>>
WEM, Citrix 桌面虚拟化方案之神器2号
查看>>
linux档案权限和目录配置
查看>>
面见袁总
查看>>
Golang学习笔记(1)---go程序一般结构
查看>>
Linux nc (netcat) 详解
查看>>
Redis——AOF持久化
查看>>
接口测试
查看>>
两表查询-其中一表查询出的结果为条件做为主查询的条件来查询(快客密码查询)...
查看>>
C# Dictionary 的几种遍历方法
查看>>
坑爹属性之【automaticallyAdjustsScrollViewInsets】
查看>>
TinyTemplate(Velocity Plus版)即将火热推出~~~
查看>>
邮件安全
查看>>
提问帖,方便贴代码
查看>>