如何在 C++ 中将字符串转换为字符数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13294067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:09:46  来源:igfitidea点击:

How to convert string to char array in C++?

c++stringtype-conversion

提问by Brian Brown

I would like to convert stringto chararray but not char*. I know how to convert string to char*(by using mallocor the way I posted it in my code) - but that's not what I want. I simply want to convert stringto char[size]array. Is it possible?

我想转换stringchar数组但不是char*. 我知道如何将字符串转换为char*(通过使用malloc或我在代码中发布它的方式) - 但这不是我想要的。我只是想转换stringchar[size]数组。是否可以?

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

int main()
{
    // char to string
    char tab[4];
    tab[0] = 'c';
    tab[1] = 'a';
    tab[2] = 't';
    tab[3] = '
string temp = "cat";
char tab2[1024];
strcpy(tab2, temp.c_str());
'; string tmp(tab); cout << tmp << "\n"; // string to char* - but thats not what I want char *c = const_cast<char*>(tmp.c_str()); cout << c << "\n"; //string to char char tab2[1024]; // ? return 0; }

回答by Chowlett

Simplest way I can think of doing it is:

我能想到的最简单的方法是:

string temp = "cat";
char tab2[1024];
strncpy(tab2, temp.c_str(), sizeof(tab2));
tab2[sizeof(tab2) - 1] = 0;

For safety, you might prefer:

为了安全起见,您可能更喜欢:

string temp = "cat";
char * tab2 = new char [temp.length()+1];
strcpy (tab2, temp.c_str());

or could be in this fashion:

或者可以采用这种方式:

const char *array = tmp.c_str();

回答by rad

Ok, i am shocked that no one really gave a good answer, now my turn. There are two cases;

好吧,我很震惊,没有人真正给出好的答案,现在轮到我了。有两种情况;

  1. A constant char arrayis good enough for you so you go with,

    char *array = &tmp[0];
    
  2. Or you need to modifythe char array so constant is not ok, then just go with this

    const char *array = tmp.c_str();
    
  1. 一个常量字符数组对你来说已经足够了,所以你可以使用,

    char *array = &tmp[0];
    
  2. 或者你需要修改char 数组所以常量不行,然后就用这个

    std::string myWord = "myWord";
    char myArray[myWord.size()+1];//as 1 char space for null is also required
    strcpy(myArray, myWord.c_str());
    

Both of them are just assignment operationsand most of the time that is just what you need, if you really need a new copy then follow other fellows answers.

它们都只是分配操作,大多数时候这正是您所需要的,如果您确实需要一个新副本,请遵循其他人的答案。

回答by rad

Easiest way to do it would be this

最简单的方法是这样

str.copy(cstr, str.length()+1); // since C++11
cstr[str.copy(cstr, str.length())] = '
string line="hello world";
char * data = new char[line.size() + 1];
copy(line.begin(), line.end(), data);
data[line.size()] = '
strcpy(tab2, tmp.c_str());
';
'; // before C++11 cstr[str.copy(cstr, sizeof(cstr)-1)] = '
auto tab2 = std::make_unique<char[]>(temp.size() + 1);
std::strcpy(tab2.get(), temp.c_str());
'; // before C++11 (safe)

回答by Youka

string n;
cin>> n;
char b[200];
for (int i = 0; i < sizeof(n); i++)
{
    b[i] = n[i];
    cout<< b[i]<< " ";
}

It's a better practice to avoid C in C++, so std::string::copyshould be the choice instead of strcpy.

在 C++ 中避免使用 C 是一种更好的做法,因此应该选择std::string::copy而不是strcpy

回答by David Schwartz

Just copy the string into the array with strcpy.

只需将字符串复制到数组中strcpy

回答by Tharindu Dhanushka

Try this way it should be work.

试试这种方式它应该是有效的。

##代码##

回答by Marrow Gnawer

Try strcpy(), but as Fred said, this is C++, not C

尝试 strcpy(),但正如 Fred 所说,这是 C++,而不是 C

回答by Fred Larson

You could use strcpy(), like so:

你可以使用strcpy(),像这样:

##代码##

Watch out for buffer overflow.

注意缓冲区溢出。

回答by emlai

If you don't know the size of the string beforehand, you can dynamically allocate an array:

如果事先不知道字符串的大小,可以动态分配一个数组:

##代码##

回答by SquircKle

Well I know this maybe rather dumb thanand simple, but I think it should work:

好吧,我知道这可能简单更愚蠢,但我认为它应该有效:

##代码##