迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何在 C++ 中把字符串转换为 Char 数组

作者:迹忆客 最近更新:2023/04/09 浏览次数:

本文介绍了将字符串数据转换为 char 数组的多种方法。


使用 std::basic_string::c_str 方法将字符串转换为 char 数组

这个版本是解决上述问题的 C++ 方法。它使用了 string 类内置的方法 c_str,该方法返回一个指向以 null 终止的 char 数组的指针。

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl
using std::string;

int main(){
    string tmp_string = "This will be converted to char*";
    auto c_string = tmp_string.c_str();

    cout << c_string <<  endl;
    return EXIT_SUCCESS;
}

输出:

This will be converted to char*

如果你打算修改存储在 c_str() 方法返回的指针处的数据,首先,你必须用 memmove 函数把这个范围复制到不同的位置,然后对 char 字符串进行如下操作。

#include <iostream>
#include <string>
#include <cstring>

using std::cout;
using std::cin;
using std::endl
using std::string;

int main(){
    string tmp_string = "This will be converted to char*";

    char *c_string_copy = new char[tmp_string.length() + 1];
    memmove(c_string_copy, tmp_string.c_str(), tmp_string.length());

    /* do operations on c_string_copy here */

    cout << c_string_copy;
    delete [] c_string_copy;
    return EXIT_SUCCESS;
}

注意,你可以使用各种函数复制 c_string 数据,例如:[memcpy]。memcpy, memccpy, mempcpy, strcpystrncpy, 但请记住阅读手册页面并仔细考虑其边缘情况/错误。


使用 std::vector 容器将字符串转换为 Char 数组

另一种方法是将字符串中的字符存储到 vector 容器中,然后使用其强大的内置方法安全地操作数据。

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

int main(){
    string tmp_string = "This will be converted to char*\n";

    vector<char> vec_str(tmp_string.begin(), tmp_string.end());
    std::copy(vec_str.begin(), vec_str.end(), std::ostream_iterator<char>(cout, ""));

    return EXIT_SUCCESS;
}

使用指针操作操作将字符串转换为字符数组

在这个版本中,我们定义了一个名为 tmp_ptrchar 指针,并将 tmp_string 中第一个字符的地址分配给它。

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl
using std::string;

int main(){
    string tmp_string = "This will be converted to char*\n";
    string mod_string = "I've been overwritten";

    char *tmp_ptr = &tmp_string[0];
    cout << tmp_ptr;

    return EXIT_SUCCESS;
}

但请记住,修改 tmp_ptr 中的数据也会改变 tmp_string 的值,你可以参考下面的代码示例。

#include <iostream>
#include <string>
#include <cstring>

using std::cout;
using std::cin;
using std::endl
using std::string;

int main(){
    string tmp_string = "This will be converted to char*\n";
    string mod_string = "I've been overwritten";

    char *tmp_ptr = &tmp_string[0];
    memmove(tmp_ptr, mod_string.c_str(), mod_string.length());

    cout << tmp_string;
    return EXIT_SUCCESS;
}

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便