它與std::string有什麼不同?什麼是以空字符結尾的字符串?
回答
以空字符結尾的字符串是連續的字符序列,最後一個字符串的二進制位模式全爲零。我不確定「常用字符串」是什麼意思,但如果您的意思是std::string
,則不需要std::string
(until C++11)是連續的,並且不需要有終結符。此外,std::string
的字符串數據始終由包含它的std::string
對象進行分配和管理;對於以空字符結尾的字符串,不存在這樣的容器,並且通常使用裸指針來引用和管理這樣的字符串。
所有這些都應該在任何體面的C++教科書中真正涵蓋 - 我建議您抓住Accelerated C++,這是其中最好的一個。
以空字符結尾的字符串表示字符串的末尾是通過出現空字符(所有位爲零)定義的。
「其他字符串」例如必須存儲自己的長度。
「字符串」實際上只是一個數組char
s;以空字符結尾的字符串是空字符'\0'
標記字符串的結尾(不一定是數組的結尾)。代碼中的所有字符串(由雙引號""
分隔)都由編譯器自動終止爲空。
因此,例如,"hi"
與{'h', 'i', '\0'}
相同。
比接受的答案更好理解。 +1 – Mike 2015-04-17 21:39:00
值得一提的是,編譯器會查找空字符來確定字符串的長度。 – 2015-07-15 13:56:37
我有一個字符串臨時工,我存儲了一個,b,c作爲臨時[0],臨時[1]和臨時[2]。現在,當我做「cout << temp」時,它不會給出 - 「abc」。我該怎麼辦? 我知道'\ 0'在這裏也不能用作字符串終結符。 – 2016-08-23 00:57:09
以空字符結尾的字符串是C中的本地字符串格式。例如,字符串文字實現爲以null結尾。因此,大量代碼(以C運行時庫開始)假定字符串以空字符結尾。
有表示一個字符串的兩種主要方式:
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的實現也會令人驚訝。
'\0'
與代碼0,null終止,空字符,NUL一個ASCII字符。在C語言中,它充當用於表示字符串結尾的保留字符。許多標準函數(如strcpy,strlen,strcmp等)都依賴於此。否則,如果沒有NUL,另一種方式來發信號串的末尾必須已被用於:
這允許字符串是僅與一個 字節的開銷任何長度;存儲計數的替代方法需要字符串 長度限制爲255或多於一個字節的開銷。
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
。
以空字符結尾的字符串(c-string)是char的數組,並且該數組的最後一個元素是0x0值。 std :: string本質上是一個向量,它是一個值的自動調整大小的容器。它不需要空終止符,因爲它必須跟蹤大小以知道何時需要調整大小。
說實話,我更喜歡c-strings而不是std,他們只是在基本庫中有更多的應用程序,只有最少的代碼和分配的應用程序,因此很難使用。
- 1. 字符串結尾空格
- 2. C#字符串以空字符串結尾嗎?
- 3. 字符串中的C字符串指針如何以空字符結尾?
- 4. 讀取以空字符結尾的字符串
- 5. 什麼是空字符串?
- 6. 字符串結尾的垃圾字符?
- 7. 在.NET中反序列化以空字符結尾的字符串的最佳方法是什麼?
- 8. 當格式字符串以換行符結尾時,scanf的行爲是什麼?
- 9. 將一個以空字符結尾的字符串與一個非空終止的字符串進行比較
- 10. 確定字符串是否以空格結尾
- 11. 以美元結尾的字符串
- 12. 查找字符串是否以另一個字符串結尾的最快方法是什麼?
- 13. 測試字符串是以另一個字符串開始還是以另一個字符串結尾
- 14. 以特定字符結尾的字符串的grep
- 15. 爲什麼字符串總是空的?
- 16. 使字符串---以0結尾
- 17. 匹配所有不以指定字符串結尾的字符
- 18. 以特殊字符結尾的C#字符串到整數
- 19. 如何將以空字符結尾的字符串轉換爲AnsiString?
- 20. 如何在Awk中輸出以空字符結尾的字符串?
- 21. 檢查字符串的末尾是否爲空白字符
- 22. Python-如何驗證字符串是否以特定字符串結尾?
- 23. 如何檢查一個字符串是否以特定子字符串結尾?
- 24. 以十六進制結尾爲空的python字符串
- 25. Ruby:空字符串和空字符串有什麼不同?
- 26. 字符串以'\ n'結尾什麼是最有效的操作方式?
- 27. 查找以 - 開頭並以換行符結尾的字符串
- 28. 爲什麼s [0]是C++中空字符串s的空字符串?
- 29. 什麼是「.1」。在一個bstrProgId結尾的字符串意味着什麼?
- 30. 捕獲任意字符串或字符串的結尾
什麼是「常用」字符串? – 2010-01-10 14:12:15
std :: string 無論如何,我已經得到了答案,thx。 – lhj7362 2010-01-10 14:15:11