简体   繁体   English

在哪里可以看到C ++字符串函数c_str的代码

[英]Where can I see code for the C++ String function c_str

I want to see the code of c_str function in c++. 我想查看c ++中c_str函数的代码。

How can I see that--> 我怎么看->

(1) In Intel Compiler (2) IN Visual Studio 2012 Compiler (3) In GCC/MinGW. (1)在Intel编译器中(2)在Visual Studio 2012编译器中(3)在GCC / MinGW中。

Open the <string> header file, and follow the trail of #include s until you find the basic_string<T> template. 打开<string>头文件,并遵循#include的路径,直到找到basic_string<T>模板。 That's where the source of the c_str will be. 这就是c_str的来源。

For example, in Visual Studio 2012 the function is located in the <xstring> header, and looks as follows: 例如,在Visual Studio 2012中,该函数位于<xstring>标头中,如下所示:

const _Elem *c_str() const _NOEXCEPT
    {   // return pointer to null-terminated nonmutable array
    return (this->_Myptr());
    }

With a decent ide, you can right click the function and the select "Go to Definition". 有了一个不错的主意,您可以右键单击该功能,然后选择“转到定义”。 Visual studio has this option, so that will be the quickest for MSVC's implementation. Visual Studio具有此选项,因此它将是MSVC实施最快的。 For gcc (libstdc++), I like using there online doxygen docs . 对于gcc(libstdc ++),我喜欢在那里使用在线doxygen docs Here is a link to the implementation of c_str() . 这是c_str()实现的链接 For intel, I believe they use libstdc++ (gcc's implementation). 对于intel,我相信他们使用libstdc ++(gcc的实现)。

With MinGW, you can find the header file at ...\\MinGW\\lib\\gcc\\mingw32\\4.7.1\\include\\c++\\debug\\string. 使用MinGW,您可以在... \\ MinGW \\ lib \\ gcc \\ mingw32 \\ 4.7.1 \\ include \\ c ++ \\ debug \\ string中找到头文件。 The c_str() function is defined directly in that header, not in an include; c_str()函数直接在该标头中定义,而不在包含中定义; I've placed the text below. 我将文字放在下面。

const _CharT* c_str() const _GLIBCXX_NOEXCEPT
    {
      const _CharT* __res = _Base::c_str();
      this->_M_invalidate_all();
      return __res;
    }

Rather than trying to find and open the include file where this function is defined (supposing it is defined in an include file—this is usually the case, but isn't required): what I usually do when I want to see the definition of a class is write a one line program which includes the appropriate header, and nothing else. 而不是尝试查找并打开定义此函数的包含文件(假设它是在包含文件中定义的-通常是这种情况,但不是必需的):当我想查看以下内容的定义时通常要做的事情一个类是编写一个单行程序,其中包含适当的标头,仅此而已。 Here: 这里:

#include <string>

(Yes, that's the complete program.) I then compile with the /E option (VC++) or the -E option (most other compilers), redirecting the output to a file, which I can then peruse at my leisure. (是的,那是完整的程序。)然后,我使用/E选项(VC ++)或-E选项(大多数其他编译器)进行编译,将输出重定向到文件,然后我可以在闲暇时仔细阅读。

This is easy to do directly from the command line, but if you want to use VS, open up the properties on a source file which includes <string> , go to the entry C/C++ -> Command Line, and enter /E in the Additional Options pane. 可以直接从命令行轻松完成此操作,但是如果要使用VS,请打开包含<string>的源文件的属性,转到条目C / C ++-> Command Line,然后在/E中输入/E其他选项窗格。 Then compile. 然后编译。 The output will appear in the output window; 输出将出现在输出窗口中。 click on it, then control-A followed by control-C, and you can paste it in whatever editor you please. 单击它,然后按Control-A和Control-C,然后可以将其粘贴到所需的任何编辑器中。

Either way, once you've got the preprocessor output in an editor, search from the top for c_str as a full word. 无论哪种方式,一旦在编辑器中获得了预处理器输出,就从顶部搜索c_str作为完整单词。

If your on linux or using MinGW or WSL if you create a file called string.txt 如果您在Linux上或使用MinGW或WSL,则创建一个名为string.txt的文件
and put 并把

#include <string>

and run cpp string.txt > string.txt from terminal or got to this site 并从终端运行cpp string.txt > string.txt或到达此站点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM