2010-01-10 331 views

回答

36

以空字符結尾的字符串是連續的字符序列,最後一個字符串的二進制位模式全爲零。我不確定「常用字符串」是什麼意思,但如果您的意思是std::string,則不需要std::stringuntil C++11)是連續的,並且不需要有終結符。此外,std::string的字符串數據始終由包含它的std::string對象進行分配和管理;對於以空字符結尾的字符串,不存在這樣的容器,並且通常使用裸指針來引用和管理這樣的字符串。

所有這些都應該在任何體面的C++教科書中真正涵蓋 - 我建議您抓住Accelerated C++,這是其中最好的一個。

+4

基本上,歸零字節決定了C中字符串的長度。 – Costique 2010-01-10 14:08:20

+0

這很簡單,thx! – lhj7362 2010-01-10 14:10:17

+0

最後一個字符不需要所有零的位模式,它只需要*值*爲0. – avakar 2010-01-10 14:10:29

2

以空字符結尾的字符串表示字符串的末尾是通過出現空字符(所有位爲零)定義的。

「其他字符串」例如必須存儲自己的長度。

46

「字符串」實際上只是一個數組char s;以空字符結尾的字符串是空字符'\0'標記字符串的結尾(不一定是數組的結尾)。代碼中的所有字符串(由雙引號""分隔)都由編譯器自動終止爲空。

因此,例如,"hi"{'h', 'i', '\0'}相同。

+4

比接受的答案更好理解。 +1 – Mike 2015-04-17 21:39:00

+1

值得一提的是,編譯器會查找空字符來確定字符串的長度。 – 2015-07-15 13:56:37

+0

我有一個字符串臨時工,我存儲了一個,b,c作爲臨時[0],臨時[1]和臨時[2]。現在,當我做「cout << temp」時,它不會給出 - 「abc」。我該怎麼辦? 我知道'\ 0'在這裏也不能用作字符串終結符。 – 2016-08-23 00:57:09

1

以空字符結尾的字符串是C中的本地字符串格式。例如,字符串文字實現爲以null結尾。因此,大量代碼(以C運行時庫開始)假定字符串以空字符結尾。

14

有表示一個字符串的兩種主要方式:

1)與ASCII空(NUL)字符,0的字符序列,在末端。您可以通過搜索終止符來判斷它有多長。這被稱爲以空字符結尾的字符串,或者有時以nul結尾。

2)一個字符序列,加上一個單獨的字段(整數長度或指向字符串末尾的指針),告訴你它有多長。

我不太確定「通常的字符串」,但經常發生的是,當談到某種特定的語言時,「字符串」一詞用於表示該語言的標準表示。所以在Java中,java.lang.String是一個2類字符串,所以這就是「string」的含義。在C中,「字符串」可能意味着1類字符串。爲了準確,標準相當冗長,但人們總是想要忽略什麼是「明顯的」。

在C++中,不幸的是,這兩種類型都是標準的。 std :: string是一個2型字符串[*],但從C繼承的標準庫函數對類型1字符串進行操作。 [*]實際上,std :: string通常是作爲一個字符數組來實現的,其中一個單獨的長度字段是一個nul終止符。這樣就可以實現c_str()函數而不需要複製或重新分配字符串數據。我不記得在不存儲長度字段的情況下實現std :: string是否合法:問題是標準需要什麼複雜性保證。對於一般容器size()建議爲O(1),但實際上並不需要。因此,即使它是合法的,只使用nul-terminators的std :: string的實現也會令人驚訝。

6
'\0' 

與代碼0,null終止,空字符,NUL一個ASCII字符。在C語言中,它充當用於表示字符串結尾的保留字符。許多標準函數(如strcpy,strlen,strcmp等)都依賴於此。否則,如果沒有NUL,另一種方式來發信號串的末尾必須已被用於:

這允許字符串是僅與一個 字節的開銷任何長度;存儲計數的替代方法需要字符串 長度限制爲255或多於一個字節的開銷。

wikipedia

C++std::string遵循此其他公約和其數據由稱爲_Rep結構表示:

// _Rep: string representation 
     // Invariants: 
     // 1. String really contains _M_length + 1 characters: due to 21.3.4 
     //  must be kept null-terminated. 
     // 2. _M_capacity >= _M_length 
     //  Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). 
     // 3. _M_refcount has three states: 
     //  -1: leaked, one reference, no ref-copies allowed, non-const. 
     //  0: one reference, non-const. 
     //  n>0: n + 1 references, operations require a lock, const. 
     // 4. All fields==0 is an empty string, given the extra storage 
     //  beyond-the-end for a null terminator; thus, the shared 
     //  empty string representation needs no constructor. 

     struct _Rep_base 
     { 
    size_type  _M_length; 
    size_type  _M_capacity; 
    _Atomic_word  _M_refcount; 
     }; 

struct _Rep : _Rep_base 
     { 
    // Types: 
    typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc; 

    // (Public) Data members: 

    // The maximum number of individual char_type elements of an 
    // individual string is determined by _S_max_size. This is the 
    // value that will be returned by max_size(). (Whereas npos 
    // is the maximum number of bytes the allocator can allocate.) 
    // If one was to divvy up the theoretical largest size string, 
    // with a terminating character and m _CharT elements, it'd 
    // look like this: 
    // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) 
    // Solving for m: 
    // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 
    // In addition, this implementation quarters this amount. 
    static const size_type _S_max_size; 
    static const _CharT _S_terminal; 

    // The following storage is init'd to 0 by the linker, resulting 
     // (carefully) in an empty string with one reference. 
     static size_type _S_empty_rep_storage[]; 

     static _Rep& 
     _S_empty_rep() 
     { 
     // NB: Mild hack to avoid strict-aliasing warnings. Note that 
     // _S_empty_rep_storage is never modified and the punning should 
     // be reasonably safe in this case. 
     void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage); 
     return *reinterpret_cast<_Rep*>(__p); 
    } 

     bool 
    _M_is_leaked() const 
     { return this->_M_refcount < 0; } 

     bool 
    _M_is_shared() const 
     { return this->_M_refcount > 0; } 

     void 
    _M_set_leaked() 
     { this->_M_refcount = -1; } 

     void 
    _M_set_sharable() 
     { this->_M_refcount = 0; } 

    void 
    _M_set_length_and_sharable(size_type __n) 
    { 
#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING 
     if (__builtin_expect(this != &_S_empty_rep(), false)) 
#endif 
     { 
      this->_M_set_sharable(); // One reference. 
      this->_M_length = __n; 
      traits_type::assign(this->_M_refdata()[__n], _S_terminal); 
      // grrr. (per 21.3.4) 
      // You cannot leave those LWG people alone for a second. 
     } 
    } 

    _CharT* 
    _M_refdata() throw() 
    { return reinterpret_cast<_CharT*>(this + 1); } 

    _CharT* 
    _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2) 
    { 
     return (!_M_is_leaked() && __alloc1 == __alloc2) 
       ? _M_refcopy() : _M_clone(__alloc1); 
    } 

    // Create & Destroy 
    static _Rep* 
    _S_create(size_type, size_type, const _Alloc&); 

    void 
    _M_dispose(const _Alloc& __a) 
    { 
#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING 
     if (__builtin_expect(this != &_S_empty_rep(), false)) 
#endif 
     if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount, 
           -1) <= 0) 
      _M_destroy(__a); 
    } // XXX MT 

    void 
    _M_destroy(const _Alloc&) throw(); 

    _CharT* 
    _M_refcopy() throw() 
    { 
#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING 
     if (__builtin_expect(this != &_S_empty_rep(), false)) 
#endif 
      __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1); 
     return _M_refdata(); 
    } // XXX MT 

    _CharT* 
    _M_clone(const _Alloc&, size_type __res = 0); 
     }; 

的實際數據可能與獲得:

_Rep* _M_rep() const 
     { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } 

這個代碼片斷來自哪個我的機器上位於usr/include/c++/4.4/bits/basic_string.h

因此,大家可以看到,不同的是顯著文件basic_string.h

0

以空字符結尾的字符串(c-string)是char的數組,並且該數組的最後一個元素是0x0值。 std :: string本質上是一個向量,它是一個值的自動調整大小的容器。它不需要空終止符,因爲它必須跟蹤大小以知道何時需要調整大小。

說實話,我更喜歡c-strings而不是std,他們只是在基本庫中有更多的應用程序,只有最少的代碼和分配的應用程序,因此很難使用。

相關問題