`
java-mans
  • 浏览: 11341538 次
文章分类
社区版块
存档分类
最新评论

difference between data() and c_str() as member function of template class basic_string

 
阅读更多

<THE C++ PROGRAMMING LANGUAGE> writes: In other words, data() produces an array of characters, whereas c_str() produces a C-style string.

以下是cygwin(basic_string.h ISO C++ 14882: 21 Strings library)与M$ Vs2005(xstring.h)分别对data()和c_str()的实现,由代码可知,data()与c_str()功能已经完全相同了。

// VS2005

由VS2005的代码可知,data()调用c_str(),c_str()调用_Myptr(),很显然,如果编译器没有做优化处理的话,

data()的效率要比c_str()慢一点。

而cygwin的代码中,两个函数实现是相同的,因此不存在效率差异。

通过两者对与data()与c_str()的实现方法不同点可以看出两者的设计观点不同。

VS2005是为了能够保证data()与c_str()得功能一致,而cygwin则仍然保持了data()与c_str()的独立性。

或许data()的存在只是为了兼容性吧。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics