所以我有一个std::string并且有一个函数可以接受char*并写入它.既然std::string::c_str()又std::string::data()回来了const char*,我不能用它们.所以我分配了一个临时缓冲区,用它调用一个函数并将其复制到std::string.
现在我计划处理大量信息,复制这个缓冲区会产生明显的影响,我想避免它.
有人建议使用&str.front()或&str[0]但是它会调用未定义的行为吗?
嗨,任何人都可以告诉这段代码有什么问题吗?
string s=getString(); //return string
if(!strcmp(s,"STRING")){
//Do something
}
Run Code Online (Sandbox Code Playgroud)
编译时我得到的错误就像
error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’|
Run Code Online (Sandbox Code Playgroud) void setVersion(char* buf, std::string version) {
buf = version;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将版本字符串写入buf,但上面的代码给了我这个错误"无法将'std :: string {aka std :: basic_string}'转换为'char*'的赋值".
修复它的最简单方法是什么?
这样做的现代方法是什么?类似<cstring>的标题已被弃用,某些编码样式禁止使用"类C"函数.我有三种做同样事情的方法.哪一个是现代C++最惯用的?
1.使用迭代器并包含null终止符
{
std::string test{"hello, world!"};
char* output = new char[test.size() + 1];
std::copy(test.begin(), test.end(),
output);
output[test.size() + 1] = '\0';
std::cout << output;
delete output;
}
Run Code Online (Sandbox Code Playgroud)
2.使用c_str()包含空终止符的用法
{
std::string test{"hello, world!"};
char* output = new char[test.size() + 1];
std::copy(test.c_str(), test.c_str() + std::strlen(test.c_str()),
output);
std::cout << output;
delete output;
}
Run Code Online (Sandbox Code Playgroud)
3.使用std::strcpy
{
std::string test{"hello, world!"};
char* output = new char[test.size() + 1];
std::strcpy(output, test.c_str());
std::cout << output;
delete output;
}
Run Code Online (Sandbox Code Playgroud)
我不希望看起来像一个面试官说"你用strcpy,你必须是C程序员"的菜鸟.
这与如何将vector <char*>转换为vector <string>/string问题相反.
我有一些遗留的例程,vector<char*>所以我需要转换我的vector<string>.
这是我提出来的:
std::vector<char*> charVec(strVec.size(),nullptr);
for (int i=0; i<strVec.size();i++) {
charVec[i]= new char(strVec[i].size()+1);
charVec[i][strVec[i].copy(charVec[i], strVec[i].size())] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
它是否正确?
有没有更好的方法来实现它?
ps当然最后我有:
for (int i=0; i<strVec.size();i++) {
delete charVec[i];
}
Run Code Online (Sandbox Code Playgroud) 是否可以将std :: string的指针传递给期望**char的函数?函数需要**char才能为其写入值.
目前我正在做以下事情:
char *s1;
f(&s1);
std::string s2 = s1;
Run Code Online (Sandbox Code Playgroud)
有没有更短的方式?很明显,s2.c_str()不起作用,因为它返回const *char.
string str1 = "hello";
const char* string1 = str1;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误..
在初始化时无法将'std :: string {aka std :: basic_string}'转换为'const char*'
我如何将字符串转换为const char*
谢谢你的帮助
目前我有一个复杂的功能,我和我们的团队不想重构使用std :: string,它需要修改一个char*.我如何正确地将string :: c_str()的深层复制到char*中?我不打算修改字符串的内部存储的char*.
char *cstr = string.c_str();
Run Code Online (Sandbox Code Playgroud)
因为c_str()是const而失败.
我正在为混合语言编写一个库,因此我们必须坚持使用C接口.我需要调用这个API函数:
void getproperty(const char * key, /* in */
char * value /* out */) {
Run Code Online (Sandbox Code Playgroud)
我需要使用std :: string的内容设置值.
std::string stdString = funcReturningString();
// set value to contents of stdString
Run Code Online (Sandbox Code Playgroud)
我已经看到很多关于可能策略的答案,但没有针对这个特定的用例,也没有被认为是"最佳"或"标准".我们正在使用C++ 11,它的价值.有些答案含糊地建议将字符串的内容复制到缓冲区并使用它.但怎么办呢?我想对这次谈话有更多的意见,所以我最终可以选择一个策略并坚持下去.
谢谢!
我使用uint32_t与其他API函数保持一致.bufferSize是一个引用,因为FORTRAN通过引用调用所有内容.
void getproperty(const char * key, /* in */
const uint32_t & bufferSize, /* in */
char * value, /* out */
uint32_t & error /* out */)
{
std::string stdString = funcReturningString();
if (bufferSize < str.size() + 1) {
// Set the error …Run Code Online (Sandbox Code Playgroud) 您不必从头开始完成完整的代码.问题出在main里面的execl(..)语句中.代码是 -
#include <cstdio>
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include <vector>
#define li long int
using namespace std;
char TypedCommandInTerminal[1001];
vector <string> ValidCommands,TypedCommand;
void ShowTerminal()
{
cout<<"User:$ ";
gets(TypedCommandInTerminal);
}
void PushCommands()
{
ValidCommands.push_back("mkdir");
}
void GetCommandIntoVector()
{
TypedCommand.clear();
char *p = strtok(TypedCommandInTerminal," ");
while(p)
{
TypedCommand.push_back(p);
p = strtok(NULL," ");
}
}
bool MatchCommand(string Command)
{
li i;
for(i=0;i<ValidCommands.size();i++)
{
if(ValidCommands[i].compare(Command)==0)
{
return true;
}
}
return false;
}
int main()
{
int status;
string StoredCommand; …Run Code Online (Sandbox Code Playgroud)