C++ 如何将std::string用于char*输出参数
对于任何声明为获取char*输出参数的函数,是否有方法将s std::string的“char”部分指定为函数的输出 我从以下几点开始:C++ 如何将std::string用于char*输出参数,c++,parameters,stdstring,C++,Parameters,Stdstring,对于任何声明为获取char*输出参数的函数,是否有方法将s std::string的“char”部分指定为函数的输出 我从以下几点开始: // EDIT: ADDED THESE TWO LINES FOR CLARITY sprintf(buf, "top -n 1 -p %s" , commaSeparatedListOfPIDs.c_str() ); fp = popen(buf, "r"); std::string replyStr; char reply[100
// EDIT: ADDED THESE TWO LINES FOR CLARITY
sprintf(buf, "top -n 1 -p %s" , commaSeparatedListOfPIDs.c_str() );
fp = popen(buf, "r");
std::string replyStr;
char reply[100];
rc = scanf( fp, "%s", reply );
replyStr = reply;
但这似乎有点,嗯,笨拙
那么,有没有办法说:
rc = scanf( fp, "%s", &replyStr.c_str() );
或者类似的
谢谢如果你想使用字符串,为什么不使用I/O的C++方式呢?看看如果你想使用字符串,为什么不使用I/O的C++方式呢?请看一下std::string replyStr(reply)代码>将生成字符数组的字符串 编辑: 但是。。。这没什么不同
使用C++风格I/OUT不必使用<代码> char */COD> < < /P>
cin>>replyStr
将获取下一个字符串,直到出现空白。
getline(cin,回复)
将字符串设置为整行输入std::string replyStr(reply)代码>将生成字符数组的字符串
编辑:
但是。。。这没什么不同
使用C++风格I/OUT不必使用<代码> char */COD> < < /P>
cin>>replyStr
将获取下一个字符串,直到出现空白。
getline(cin,回复)代码>将字符串设置为整行输入是这是可能的:
std::string replyStr(100, '\0');
//Requires C++11 (to guarantee that strings hold their characters
//in contiguous memory), and some way to ensure that the string in the file
//is less than 100 characters long.
rc = fscanf( fp, "%s", &reply.front() );
replyStr.erase(replyStr.find('\0'));
第二个条件很难满足,如果不满足,则该程序具有未定义的行为。是,这是可能的:
std::string replyStr(100, '\0');
//Requires C++11 (to guarantee that strings hold their characters
//in contiguous memory), and some way to ensure that the string in the file
//is less than 100 characters long.
rc = fscanf( fp, "%s", &reply.front() );
replyStr.erase(replyStr.find('\0'));
第二个条件很难满足,如果不满足,则该程序具有未定义的行为。直到c++0x,不需要&str[0]返回指向连续存储的指针。传统的方法是使用std::vector,即使在c++0x:
std::vector<char> reply(100);
rc = scanf(fp, "%s", &reply[0]);
std::向量回复(100);
rc=scanf(fp、%s、&reply[0]);
然而,在c++0x中,std::string也可以保证工作,而不是std::vector。直到c++0x之前,&str[0]不需要返回指向连续存储的指针。传统的方法是使用std::vector,即使在c++0x:
std::vector<char> reply(100);
rc = scanf(fp, "%s", &reply[0]);
std::向量回复(100);
rc=scanf(fp、%s、&reply[0]);
但是,在c++0x中,std::string也可以保证工作,而不是std::vector。来自std::string的字符*只有在字符串有效时才有效。如果std::字符串超出范围,则它不再是有效的字符指针
const char* bar;
{
std::String foo = "Hello";
bar = foo.c_str();
printf("%s", bar); //SAFE since the String foo is still in scope
}
printf("%s", bar); //UNSAFE String foo is no longer in scope
只要std::String变量存在,您就可以使用const char*,如果它超出范围,内存将被释放,并且const char*指针将成为一个悬空指针,不再安全使用
如果在std::String foo超出范围后需要它存在,那么必须将该字符串复制到char中*
char bar[100];
{
std::String foo = "Hello";
strncpy(bar, foo.c_str(), 100);
bar[100] = '\0'; //make sure string is null-terminated
printf("%s", bar); //SAFE
}
printf("%s", bar); //SAFE even though the std::String has gone out of scope.
如果字符串位于函数中,则当函数返回时,该字符串将不存在。来自std::string的字符*仅在字符串有效时才有效。如果std::字符串超出范围,则它不再是有效的字符指针
const char* bar;
{
std::String foo = "Hello";
bar = foo.c_str();
printf("%s", bar); //SAFE since the String foo is still in scope
}
printf("%s", bar); //UNSAFE String foo is no longer in scope
只要std::String变量存在,您就可以使用const char*,如果它超出范围,内存将被释放,并且const char*指针将成为一个悬空指针,不再安全使用
如果在std::String foo超出范围后需要它存在,那么必须将该字符串复制到char中*
char bar[100];
{
std::String foo = "Hello";
strncpy(bar, foo.c_str(), 100);
bar[100] = '\0'; //make sure string is null-terminated
printf("%s", bar); //SAFE
}
printf("%s", bar); //SAFE even though the std::String has gone out of scope.
如果您的字符串位于函数内,则当函数返回时,它将不存在。可能重复的I see I missed a要点,fd来自popen()。可能重复的I see I missed a要点,fd来自popen()。与replyStr=reply相反代码>,他已经有了?OP想要完全摆脱reply
,忘记=
操作符重载字符串为char*,将编辑我的replyAs而不是replyStr=reply代码>,他已经有了?OP想要完全摆脱reply
…忘记=
操作符对于string to char*重载,将编辑我的replyMaybe-只是看起来我们不在这里可能-只是看起来我们不在这里抱歉,但这里是函数的输入。我想要foo作为输出。在这种情况下,参数是变量的地址,因此函数可以回复到该变量中。对不起,这里是函数的输入。我想要foo作为输出。在这种情况下,参数是变量的地址,这样函数就可以回复到该变量中。但是std::vector不会像std::string那样具有所有这些子字符串、大小和任何其他属性。这些方法将应用于集合,而不是集合。但是std::vector不会像std::string那样具有所有这些子字符串和大小以及任何其他属性。这些方法将适用于收集,而不是收集。在我的情况下,因为我是从顶部偷窃,我知道最大线尺寸小于100。男孩,我错了。从man top:top显示的宽度将限制在512个位置。显示所有字段至少需要160个字符。剩余宽度可用于“命令”列。c/100/512/在我的情况下,因为我是从顶部偷窃,我知道最大线尺寸小于100。男孩,我错了。从man top:top显示的宽度将限制在512个位置。显示所有字段至少需要160个字符。剩余宽度可用于“命令”列。c/100/512/