search

Home  >  Q&A  >  body text

c++ 自带string类 的对象 字符串结尾带不带‘\0’?

c++ 自带string类 的对象 字符串结尾带不带‘0’?

黄舟黄舟2810 days ago819

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:46:57


    Picture above

    The string type treats '0' as an ordinary character, and the length is increased by one. It can be seen that '0' is not the end character and does not contain special functions. There is no need to add '0' at the end of the string

    There is no string type in C language, but a character array is used instead. Generally, character arrays can be of uncertain length.
    char * string = "ABCD". How does the compiler know the end of the string? It usually adds '0' at the end. '

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:46:57

    std::string and C-style string in C++ are two different strings. The former is a class defined in the standard library, and the latter is an alias for a character array.

    • C-style string: usually ends with .

    • std::string: The standard does not require to end the string. The compiler can add at the end during implementation, or not. However, when converting c_str() to data() via std::string or const char * (the two are equivalent in C++11 and later), you will find that the last character is (see the reason Appendix at the end of the article).


    C-style string generally cannot contain the character (because this character is treated as a special character indicating the end of the string). If this character is included, the following characters will be ignored. That is:

    char s[] = "ab
    std::string s{'a', 'b', '
    template< 
        class CharT, 
        class Traits = std::char_traits<CharT>, 
        class Allocator = std::allocator<CharT>
    > class basic_string;
    ', 'c'}; //std::string s = "ab
    string s{'a', 'b', 'rrreee', 'c'};
    cout << s.size() << endl;  // 输出 4
    s += 'rrreee';
    s += 'd';
    cout << s.size() << endl;  // 输出 6
    c"; // 这里由于是从 C-style string 构造 std::string,所以仍然会忽略 rrreee 之后的字符 cout << s << endl; // 输出 ab c
    c"; cout << s << endl; // 输出 ab

    and std::string do not have this restriction. That is:

    rrreee

    Appendix

    When converting c_str() to data() through std::string or const char * (the two are equivalent in C++11 and later), you will find that the last character is . A little background knowledge is needed to understand this question:

    One, std::string is a special casestd::basic_string<CharT, Traits, Allocator> of std::basic_string<char>this template. That is template:

    rrreee A special case when the parameter

    in CharT is char.

    Two, s.size() will return the number of characters in std::string, that is:

    rrreee

    3. When using the [] operator (i.e. std::basic_string::reference std::basic_string::operator[](size_type pos);) to obtain the characters in std::string, the character range pos is from 0 to size(). Among them, 0 to size()-1 are the stored characters, and for pos == size() there are the following provisions:

    If pos == size(), a reference to the character with value CharT() (the null character) is returned. For the first (non-const) version, the behavior is undefined if this character is modified.

    means that when accessing s[s.size()], a reference to the return value of CharT() will be returned, and CharT is std::string for char, and char() returns 00, so s[s.size()] will return .

    4. When accessing a string through the pointer returned by c_str() or data() (const CharT* c_str() const;), the effect is:

    Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].

    means that p[s.size()] is equivalent to p + s.size(), which is equivalent to &s.operator[](s.size()), which is equivalent to s[s.size()], so you will find that the return value is .

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:46:57

    0 is the character array in C language used as the end mark. C++ strings do not have this need

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:46:57

    Why not create a string object and print it to see its size?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:46:57

    When the C language uses a char* pointer as a string, a special character 0 is needed to mark the end position of the pointer when reading the string, which is generally considered the end of the string mark.
    The C++ language is object-oriented, and the length information is directly stored in the members of the object. Reading strings can be read directly based on this length, so there is no need for an end tag. Moreover, the end tag is not conducive to reading strings containing 0 characters in the string.

    参照std::string的实现 bits/basic_string.h

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:46:57

    String itself is encapsulated and will

    #include <iostream>
    #include <stdio.h>
    #include <string>
    using std::string;
    using namespace std;
    
    
    void test1(){
        string s1 = "Hello";
        const char * v1 = s1.data();
        printf("0x%.2x\n", v1[s1.size()]);
    
        string *s2 = new string("Hello");
        const char * v2 = s2->data();
        printf("0x%.2x\n", v2[s2->size()]);
    
    }
    
    void test2(){
        string s1 = "Hello";
        const char * v1 = s1.c_str();
        printf("0x%.2x\n", v1[s1.size()]);
    
        string *s2 = new string("Hello");
        const char * v2 = s2->c_str();
        printf("0x%.2x\n", v2[s2->size()]);;
    }
    
    int main()
    {
        test1();
        test2();
        return 0;
    }
    

    The output is all 0x00

    reply
    0
  • Cancelreply