【问题标题】:What is use of c_str function?c++中c_str函数有什么用
【发布时间】:2011-11-17 00:04:37
【问题描述】:

我刚开始阅读 C++,发现 C++ 具有丰富的字符串操作函数,而 C 没有。我正在阅读这些函数并遇到c_str(),据我所知c_str 将一个可能为空终止或可能不是空终止的字符串转换为空终止字符串。是真的吗?

谁能给我一些例子让我理解c_str函数的使用??

【问题讨论】:

    标签: c++ c string


    【解决方案1】:

    c_str 返回一个 const char*,它指向一个以 null 结尾的字符串(即 C 风格的字符串)。当您想将 std::string 的“内容”¹传递给期望使用 C 样式字符串的函数时,它很有用。

    例如,考虑以下代码:

    std::string string("Hello world!");
    std::size_t pos1 = string.find_first_of('w');
    
    std::size_t pos2 = static_cast<std::size_t>(std::strchr(string.c_str(), 'w') - string.c_str());
    
    if (pos1 == pos2) {
        std::printf("Both ways give the same result.\n");
    }
    

    See it in action.

    注意事项:

    ¹ 这并不完全正确,因为 std::string(与 C 字符串不同)可以包含 \0 字符。如果是这样,接收到返回值c_str() 的代码会误以为字符串比实际短,因为它会将\0 解释为字符串的结尾。

    【讨论】:

    • 您在笔记中提出的非常有趣的观点:我想知道 std::string 是否已经包含 \0 然后 c_str 还在字符串末尾附加 \0 ??
    • @AmitSinghTomar:是的,所以你将有两个空字节——一个是字符串的合法部分,一个应该是空终止符。但是接收指针的 c 风格函数不知道这一点。
    • 注意:许多 C-API 会要求提供两个参数 (char const*, size_t),当然,第二个是大小。
    • 这不起作用:string str("0.85"); newVolume = _tstof((TCHAR*)str.c_str()); 如何将 TCHAR* argv[] 输入 0.85 转换为该值?
    • @YumYumYum 你不能将std::string 与 TCHAR 的东西一起使用,TCHAR 的全部意义在于自动在 char/wchar 之间切换,而 std::string 仅适用于 char。您将不得不使用适当的抽象,或者根本不使用 TCHAR 并始终进行 Unicode 构建。
    【解决方案2】:

    在 C++ 中,您将字符串定义为

    std::string MyString;

    而不是

    char MyString[20];.

    在编写 C++ 代码时,您会遇到一些需要 C 字符串作为参数的 C 函数。
    如下:

    void IAmACFunction(int abc, float bcd, const char * cstring);

    现在有一个问题。您正在使用 C++ 并且正在使用 std::string 字符串变量。但是这个 C 函数要求一个 C 字符串。如何将std::string 转换为标准 C 字符串?

    像这样:

    std::string MyString;
    // ...
    MyString = "Hello world!";
    // ...
    IAmACFunction(5, 2.45f, MyString.c_str());
    

    这就是c_str() 的用途。

    请注意,对于std::wstring 字符串,c_str() 返回一个const w_char *

    【讨论】:

      【解决方案3】:

      大多数旧的 c++ 和 c 函数,在处理字符串时,使用const char*
      使用 STL 和std::string,引入string.c_str() 可以将std::string 转换为const char*

      这意味着如果您承诺不更改缓冲区,您将能够使用只读字符串内容。承诺 = const char*

      【讨论】:

      • @没有stl::string 这样的东西。事实上,除了在某处的博物馆里,连“STL”之类的东西都没有。
      【解决方案4】:

      在 C/C++ 编程中有两种类型的字符串:C 字符串和标准字符串。使用&lt;string&gt; 标头,我们可以使用标准字符串。另一方面,C 字符串只是一个普通字符数组。因此,为了将标准字符串转换为 C 字符串,我们使用 c_str() 函数。

      例如

      // a string to a C-style string conversion//
      
      const char *cstr1 = str1.c_str();
      cout<<"Operation: *cstr1 = str1.c_str()"<<endl;
      cout<<"The C-style string c_str1 is: "<<cstr1<<endl;
      cout<<"\nOperation: strlen(cstr1)"<<endl;
      cout<<"The length of C-style string str1 = "<<strlen(cstr1)<<endl;
      

      输出将是,

      Operation: *cstr1 = str1.c_str()
      The C-style string c_str1 is: Testing the c_str 
      Operation: strlen(cstr1)
      The length of C-style string str1 = 17
      

      【讨论】:

        【解决方案5】:

        c_str() 将 C++ 字符串转换为 C 风格的字符串,该字符串本质上是一个以空字符结尾的字节数组。当您想将 C++ 字符串传递给需要 C 样式字符串的函数时(例如,许多 Win32 API、POSIX 样式函数等),您可以使用它。

        【讨论】:

        • 我不会说“转换”。相反,该函数“提供访问”一个合适的只读字符数组——很可能是一个数组,该数组在 std::string 的实现中一直存在。
        【解决方案6】:

        它用于使 std::string 与需要以 null 终止的 char* 的 C 代码互操作。

        【讨论】:

          【解决方案7】:

          哦,必须在这里添加我自己的选择,当您编码/解码一些在两个程序之间传输的字符串 obj 时,您将使用它。

          假设您在 python 中使用 base64encode 一些数组,然后您想将其解码为 c++。一旦你有了你在 C++ 中从 base64decode 解码的字符串。为了让它回到浮点数组,你需要做的就是

          float arr[1024];
          memcpy(arr, ur_string.c_str(), sizeof(float) * 1024);
          

          我想这是很常见的用法。

          【讨论】:

          • 这种代码不可移植(字节序和浮点格式等)
          【解决方案8】:

          const char* c_str() const; 返回一个指向 array 的指针,该数组包含一个 null-terminated 序列 characters(即一个 C - 字符串),表示当前值字符串对象。

          该数组包含相同序列的字符,它们构成字符串对象的值加上一个额外的终止空字符('\0') 结尾。

          std::string str = "hello";
          std::cout << str;          // hello
          printf("%s", str);         // ,²/☺
          printf("%s", str.c_str()); // hello
          

          【讨论】:

            猜你喜欢
            • 2011-09-17
            • 2012-05-06
            • 1970-01-01
            • 2011-02-09
            • 2011-12-15
            • 2023-01-23
            • 2021-11-06
            相关资源
            最近更新 更多