[英]convert a char* to std::string
I need to use an std::string
to store data retrieved by fgets()
.我需要使用std::string
来存储fgets()
检索到的数据。 To do this I need to convert the char*
return value from fgets()
into an std::string
to store in an array.为此,我需要将fgets()
的char*
返回值转换为std::string
以存储在数组中。 How can this be done?如何才能做到这一点?
std::string
has a constructor for this: std::string
为此有一个构造函数:
const char *s = "Hello, World!";
std::string str(s);
Note that this construct deep copies the character list at s
and s
should not be nullptr
, or else behavior is undefined.请注意,此构造深度复制s
处的字符列表,并且s
不应为nullptr
,否则行为未定义。
If you already know size of the char*, use this instead如果您已经知道 char* 的大小,请改用它
char* data = ...;
int size = ...;
std::string myString(data, size);
This doesn't use strlen.这不使用 strlen。
EDIT: If string variable already exists, use assign():编辑:如果字符串变量已经存在,使用assign():
std::string myString;
char* data = ...;
int size = ...;
myString.assign(data, size);
Most answers talks about constructing std::string
.大多数答案都在谈论构造std::string
。
If already constructed, just use assignment operator .如果已经构造,只需使用赋值运算符。
std::string oString;
char* pStr;
... // Here allocate and get character string (e.g. using fgets as you mentioned)
oString = pStr; // This is it! It copies contents from pStr to oString
I need to use std::string to store data retrieved by fgets().我需要使用 std::string 来存储 fgets() 检索到的数据。
Why using fgets()
when you are programming C++?为什么在编写 C++ 时使用fgets()
? Why not std::getline()
?为什么不std::getline()
?
Pass it in through the constructor:通过构造函数传入:
const char* dat = "my string!";
std::string my_string( dat );
You can use the function string.c_str() to go the other way:您可以使用函数 string.c_str() 去另一种方式:
std::string my_string("testing!");
const char* dat = my_string.c_str();
const char* charPointer = "Hello, World!\n";
std::string strFromChar;
strFromChar.append(charPointer);
std::cout<<strFromChar<<std::endl;
char* data;
stringstream myStreamString;
myStreamString << data;
string myString = myStreamString.str();
cout << myString << endl;
I would like to mention a new method which uses the user defined literal s
.我想提一个使用用户定义文字s
的新方法。 This isn't new, but it will be more common because it was added in the C++14 Standard Library.这不是新的,但它会更常见,因为它是在 C++14 标准库中添加的。
Largely superfluous in the general case:在一般情况下基本上是多余的:
string mystring = "your string here"s;
But it allows you to use auto, also with wide strings:但它允许您使用 auto,也可以使用宽字符串:
auto mystring = U"your UTF-32 string here"s;
And here is where it really shines:这里是它真正闪耀的地方:
string suffix;
cin >> suffix;
string mystring = "mystring"s + suffix;
I've just been struggling with MSVC2005 to use the std::string(char*)
constructor just like the top-rated answer.我一直在努力使用 MSVC2005 来使用std::string(char*)
构造函数,就像最受好评的答案一样。 As I see this variant listed as #4 on always-trusted http://en.cppreference.com/w/cpp/string/basic_string/basic_string , I figure even an old compiler offers this.当我看到这个变体在始终受信任的http://en.cppreference.com/w/cpp/string/basic_string/basic_string上列为 #4 时,我认为即使是旧的编译器也能提供这个。
It has taken me so long to realize that this constructor absolute refuses to match with (unsigned char*)
as an argument !我花了很长时间才意识到这个构造函数绝对拒绝与(unsigned char*)
作为参数匹配! I got these incomprehensible error messages about failure to match with std::string
argument type, which was definitely not what I was aiming for.我收到了关于未能与std::string
参数类型匹配的这些难以理解的错误消息,这绝对不是我的目标。 Just casting the argument with std::string((char*)ucharPtr)
solved my problem... duh !只需使用std::string((char*)ucharPtr)
转换参数就解决了我的问题...... duh !
Converting from C style string to C++ std string is easier从 C 风格字符串转换为 C++ 标准字符串更容易
There is three ways we can convert from C style string to C++ std string我们可以通过三种方式将 C 样式字符串转换为 C++ 标准字符串
First one is using constructor,第一个是使用构造函数,
char chText[20] = "I am a Programmer";
// using constructor
string text(chText);
Second one is using string::assign
method第二个是使用string::assign
方法
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
// using assign function
text.assign(chText);
Third one is assignment operator(=), in which string class uses operator overloading第三个是赋值运算符(=),其中字符串类使用运算符重载
// char string
char chText[20] = "I am a Programmer";
// c++ string
// convertion from char string to c++ string using assignment operator overloading
string text = chText;
third one can be also write like below -第三个也可以像下面这样写 -
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
text = chText;
Third one is little straight forward and can be used in both situation第三个有点直截了当,可以在两种情况下使用
char* data;
std::string myString(data);
Not sure why no one besides Erik mentioned this, but according to this page , the assignment operator works just fine.不知道为什么除了 Erik 之外没有人提到这一点,但根据这个页面,赋值运算符工作得很好。 No need to use a constructor, .assign(), or .append().无需使用构造函数、.assign() 或 .append()。
std::string mystring;
mystring = "This is a test!"; // Assign C string to std:string directly
std::cout << mystring << '\n';
char* c1 = 'z';
char* c2 = 'w';
string s1{c1};
string s12{c1, c2};
Exchange functions of data types in C/C++ convenience. C/C++ 方便的数据类型交换函数。 In this repo there are convenience functions of all c++ data types and I wrote them all as in python.在这个 repo 中有所有 c++ 数据类型的便利函数,我把它们都写在 python 中。
Use This Repo:使用这个回购:
https://github.com/azizovrafael/Simple-CPlusPlus https://github.com/azizovrafael/Simple-CPlusPlus
#simplecplusplus #simplecplusplus
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.