相关疑难解决方法(0)

直接写入std :: string的char*buffer

所以我有一个std::string并且有一个函数可以接受char*并写入它.既然std::string::c_str()std::string::data()回来了const char*,我不能用它们.所以我分配了一个临时缓冲区,用它调用一个函数并将其复制到std::string.

现在我计划处理大量信息,复制这个缓冲区会产生明显的影响,我想避免它.

有人建议使用&str.front()&str[0]但是它会调用未定义的行为吗?

c++ string language-lawyer c++14 c++17

7
推荐指数
1
解决办法
4664
查看次数

无法将'std :: string'转换为'const char*

嗨,任何人都可以告诉这段代码有什么问题吗?

 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)

c++

6
推荐指数
3
解决办法
3万
查看次数

将C++字符串写入char*

可能重复:
将std :: string转换为const char*或char*

void setVersion(char* buf, std::string version) {
  buf = version;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将版本字符串写入buf,但上面的代码给了我这个错误"无法将'std :: string {aka std :: basic_string}'转换为'char*'的赋值".

修复它的最简单方法是什么?

c++ string

5
推荐指数
2
解决办法
7060
查看次数

现代C++将字符串复制到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程序员"的菜鸟.

c++ string c++11

3
推荐指数
2
解决办法
3652
查看次数

如何将vector <string>转换为vector <char*>

这与如何将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)

c++ stl char stdvector

3
推荐指数
1
解决办法
2118
查看次数

将std :: string传递给函数f(**char)

是否可以将std :: string的指针传递给期望**char的函数?函数需要**char才能为其写入值.

目前我正在做以下事情:

char *s1;
f(&s1);
std::string s2 = s1;
Run Code Online (Sandbox Code Playgroud)

有没有更短的方式?很明显,s2.c_str()不起作用,因为它返回const *char.

c++ string pointers

2
推荐指数
1
解决办法
1342
查看次数

将字符串转换为const char*issue

string str1 = "hello";
    const char* string1 = str1; 
Run Code Online (Sandbox Code Playgroud)

我收到一个错误..

在初始化时无法将'std :: string {aka std :: basic_string}'转换为'const char*'

我如何将字符串转换为const char*

谢谢你的帮助

c++ string

2
推荐指数
1
解决办法
2万
查看次数

将std :: string :: c_str()深层复制到char*

目前我有一个复杂的功能,我和我们的团队不想重构使用std :: string,它需要修改一个char*.我如何正确地将string :: c_str()的深层复制到char*中?我打算修改字符串的内部存储的char*.

char *cstr = string.c_str();
Run Code Online (Sandbox Code Playgroud)

因为c_str()是const而失败.

c++ string

2
推荐指数
1
解决办法
7716
查看次数

在c ++ 11中将std :: string转换为char*

我正在为混合语言编写一个库,因此我们必须坚持使用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)

c++ c++11

2
推荐指数
1
解决办法
4724
查看次数

为什么显示 - "无法传递非平凡可复制类型的对象"?

您不必从头开始完成完整的代码.问题出在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)

c++ linux os.execl

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×10

string ×6

c++11 ×2

c++14 ×1

c++17 ×1

char ×1

language-lawyer ×1

linux ×1

os.execl ×1

pointers ×1

stdvector ×1

stl ×1