Giter VIP home page Giter VIP logo

Comments (3)

pezy avatar pezy commented on August 19, 2024

我copy下来编译后运行说不支持的16位应用程序,这是为何呢?

这个应该是 编译器/IDE 的问题,我用 VS 2013 以及 GCC 4.9+, Clang 3.4+ 都没问题。

另外为什么不用
std::ostream& operator<<(std::ostream& os, const String& s) { auto p = s.elements; while (p != s.end) os << *p++; return os; }
而是用这个来重载呢
std::ostream& operator<<(std::ostream& os, const String& s) { char* c = const_cast<char*>(s.c_str()); while (*c != '\0') os << *c++; return os; }

先搞清楚这两个差别。最简单的办法就是用一个 vector 来存储输出的每一个字符。

你会发现上面那种(书上的写法)存储的字符数量为 12. 而下面那种存储的字符数量为 11. 差在哪呢?细一看就知道,差在最后那个 '\0' 上了。上面的写法,输出的其实是:

H e l l o _ W o r l d \0

而下面的写法输出的是:

H e l l o _ W o r l d

在这道题里,我认为 '\0' 不属于我所定义 String 的一部分。'\0' 的存在只为了兼容 C 接口(c_str())。所以我输出时,不会包含这一特殊字符。

from cppprimer.

xyz4826757 avatar xyz4826757 commented on August 19, 2024

恩,的确如此,那为什么要用char* c = const_cast<char*>(s.c_str());而不是之间auto c = s.c_str(),让c为const char_呢,因为我觉得后面并未有改写字符的操作,为何要显示转化为char_呢?

from cppprimer.

pezy avatar pezy commented on August 19, 2024

额,有道理。回头有空改一下。

from cppprimer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.