make_unique 可以存储文字或 iostream 输入吗?
can make_unique store the literals or iostream inputs?
当我用cin>>ptr
使用char类型的普通指针存储来自<iostream>
的输入时,它被转换为数组。
我推断因为 cout<<ptr;
给出了存储的字符串而不是 address.And 地址由 cout<<&pointer;
给出。
但是当我改用 make_unique
时,它给出了错误。
为什么他们的行为相同。
请解释这一点,以及使用 make_unique 将字符串输入存储为字符数组的正确方法是什么。
我附上了使用过的代码。
#include<iostream>
#include<string>
#include<memory>
using namespace std;
int main(){
//using of std::string
string str;
cout<<"Enter a str: ";
cin>>str;cout<<endl;
cout<<str<<"\n";
// using of ordinary pointer
char* str2;//as if u r assigning array to a pointer
cout<<"Enter a str: ";
cin>> str2;cout<<endl;
cout<<str2;
cout<<endl;
//using of arrays
char c[] = "ghfgtgbbb";
cout<<c;
char* cPtr = c;
cout<<"\n"<<cPtr;
cout<<endl;
//using of mak_unique (gives errors)
auto str3Ptr = make_unique<char>();
cout<<"Enter a str: ";
cin>> str3Ptr;cout<<endl;
cout<<str3Ptr;
cout<<endl;
return 0;}
首先,
// using of ordinary pointer
char* str2;//as if u r assigning array to a pointer
cout<<"Enter a str: ";
cin>> str2;cout<<endl;
cout<<str2;
cout<<endl;
可能 "work" 但它实际上是未定义的行为。 char* str2;
创建一个指向某物的指针,但我们不知道是什么。尝试使用 cin>> str2
将数据存储在其中是未定义的行为,因为您无权写入 str2
指向的任何内容。所以应该避免这段代码。
现在让我们看看
//using of arrays
char c[] = "ghfgtgbbb";
cout<<c;
char* cPtr = c;
cout<<"\n"<<cPtr;
cout<<endl;
这样更好,c
是一个包含 10 个字符的数组,并且 cPtr
被初始化为指向它,因此它是一个有效的指针。你不用它输入,但你可以输入,但你必须确保输入的字符不超过 9 个(你必须为空终止符留出空间)。
现在我们将查看 unique_ptr
代码。在
//using of mak_unique (gives errors)
auto str3Ptr = make_unique<char>();
cout<<"Enter a str: ";
cin>> str3Ptr;cout<<endl;
cout<<str3Ptr;
cout<<endl;
您基本上完成了与第一个代码块相同的事情。 auto str3Ptr = make_unique<char>();
创建一个指向单个 char
的唯一指针。所以至少你已经初始化了变量,但它不会大到足以存储输入。您需要使用数组版本并为您想要的输入分配足够的 space 。那看起来像
auto str3Ptr = make_unique<char[]>(80);
^^ number of elements to allocate
您也不能将它与 cin
或 cout
一起使用,因为它不提供这样做的重载。您要么必须自己编写,要么如果存在适用于指针类型的重载,那么您可以使用 get
获取指向存储在 unique_ptr
.[=27= 中的内容的实际指针]
所以,剩下的就是
//using of std::string
string str;
cout<<"Enter a str: ";
cin>>str;cout<<endl;
cout<<str<<"\n";
这是在 C++ 中处理字符串的正确方法。它将处理分配和释放,并构建为与标准流一起工作。
当我用cin>>ptr
使用char类型的普通指针存储来自<iostream>
的输入时,它被转换为数组。
我推断因为 cout<<ptr;
给出了存储的字符串而不是 address.And 地址由 cout<<&pointer;
给出。
但是当我改用 make_unique
时,它给出了错误。
为什么他们的行为相同。
请解释这一点,以及使用 make_unique 将字符串输入存储为字符数组的正确方法是什么。
我附上了使用过的代码。
#include<iostream>
#include<string>
#include<memory>
using namespace std;
int main(){
//using of std::string
string str;
cout<<"Enter a str: ";
cin>>str;cout<<endl;
cout<<str<<"\n";
// using of ordinary pointer
char* str2;//as if u r assigning array to a pointer
cout<<"Enter a str: ";
cin>> str2;cout<<endl;
cout<<str2;
cout<<endl;
//using of arrays
char c[] = "ghfgtgbbb";
cout<<c;
char* cPtr = c;
cout<<"\n"<<cPtr;
cout<<endl;
//using of mak_unique (gives errors)
auto str3Ptr = make_unique<char>();
cout<<"Enter a str: ";
cin>> str3Ptr;cout<<endl;
cout<<str3Ptr;
cout<<endl;
return 0;}
首先,
// using of ordinary pointer
char* str2;//as if u r assigning array to a pointer
cout<<"Enter a str: ";
cin>> str2;cout<<endl;
cout<<str2;
cout<<endl;
可能 "work" 但它实际上是未定义的行为。 char* str2;
创建一个指向某物的指针,但我们不知道是什么。尝试使用 cin>> str2
将数据存储在其中是未定义的行为,因为您无权写入 str2
指向的任何内容。所以应该避免这段代码。
现在让我们看看
//using of arrays
char c[] = "ghfgtgbbb";
cout<<c;
char* cPtr = c;
cout<<"\n"<<cPtr;
cout<<endl;
这样更好,c
是一个包含 10 个字符的数组,并且 cPtr
被初始化为指向它,因此它是一个有效的指针。你不用它输入,但你可以输入,但你必须确保输入的字符不超过 9 个(你必须为空终止符留出空间)。
现在我们将查看 unique_ptr
代码。在
//using of mak_unique (gives errors)
auto str3Ptr = make_unique<char>();
cout<<"Enter a str: ";
cin>> str3Ptr;cout<<endl;
cout<<str3Ptr;
cout<<endl;
您基本上完成了与第一个代码块相同的事情。 auto str3Ptr = make_unique<char>();
创建一个指向单个 char
的唯一指针。所以至少你已经初始化了变量,但它不会大到足以存储输入。您需要使用数组版本并为您想要的输入分配足够的 space 。那看起来像
auto str3Ptr = make_unique<char[]>(80);
^^ number of elements to allocate
您也不能将它与 cin
或 cout
一起使用,因为它不提供这样做的重载。您要么必须自己编写,要么如果存在适用于指针类型的重载,那么您可以使用 get
获取指向存储在 unique_ptr
.[=27= 中的内容的实际指针]
所以,剩下的就是
//using of std::string
string str;
cout<<"Enter a str: ";
cin>>str;cout<<endl;
cout<<str<<"\n";
这是在 C++ 中处理字符串的正确方法。它将处理分配和释放,并构建为与标准流一起工作。