C++ 如何复制2个字符串数据类型的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12678819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:30:36  来源:igfitidea点击:

How to copy 2 strings of string data type

c++string

提问by Muhammad Arslan Jamshaid

How to copy 2 strings (here I mean string data type). I used strcpyfunction and it only works if

如何复制 2 个字符串(这里我指的是字符串数据类型)。我使用了strcpy函数,它只适用于

char a[7]="text";
char b[5]="image";
strcpy(a,b);

but Whenever I use

但每当我使用

string a="text";
string b="image";
strcpy(a,b);

I get this error

我收到这个错误

functions.cpp no matching function for call to `strcpy(std::string&, std::string&)

functions.cpp 没有匹配的函数调用`strcpy(std::string&, std::string&)

回答by Caesar

You shouldn't use strcpy to copy std::string, only use it for C-Style strings.

您不应该使用 strcpy 来复制 std::string,只能将其用于C-Style strings

If you want to copy ato bthen just use the = operator.

如果你想复制ab那么只需使用= operator.

string a = "text";
string b = "image";
b = a;

回答by bames53

strcpy is only for C strings. For std::string you copy it like any C++ object.

strcpy 仅适用于 C 字符串。对于 std::string,您可以像复制任何 C++ 对象一样复制它。

std::string a = "text";
std::string b = a; // copy a into b

If you want to concatenate strings you can use the +operator:

如果要连接字符串,可以使用+运算符:

std::string a = "text";
std::string b = "image";
a = a + b; // or a += b;

You can even do many at once:

你甚至可以一次做很多:

std::string c = a + " " + b + "hello";

Although "hello" + " world"doesn't work as you might expect. You need an explicit std::string to be in there: std::string("Hello") + "world"

虽然"hello" + " world"不像你期望的那样工作。你需要一个显式的 std::string 在那里:std::string("Hello") + "world"

回答by th3an0maly

strcpy example:

strcpy 示例:

#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string" ;
  char str2[40] ;
  strcpy (str2,str1) ;
  printf ("str1: %s\n",str1) ;
  return 0 ;
}

Output: str1: Sample string

输出: str1: Sample string

Your case:

你的情况:

A simple =operator should do the job.

一个简单的=操作员应该可以完成这项工作。

string str1="Sample string" ;
string str2 = str1 ;

回答by th3an0maly

Caesar's solution is best in my opinion, but if you still insist to use the strcpyfunction, then after you have your strings ready:

Caesar的解决方案在我看来是最好的,但是如果您仍然坚持使用该strcpy功能,那么在您准备好字符串之后:

string a="text";
string b="image";

You can try either:

您可以尝试

strcpy(a.data(), b.data());

or

或者

strcpy(a.c_str(), b.c_str());

Just call either the data()or c_str()member functions of the std::stringclass, to get the char*pointer of the string object.

只需调用类的data()c_str()成员函数std::string,即可获取char*字符串对象的指针。

strcpy function doesn't have overload to accept two std::string objects as parameters. It has only one overload to accept two char*pointers as parameters.

strcpy 函数没有重载来接受两个 std::string 对象作为参数。它只有一个重载来接受两个char*指针作为参数。

Both dataand c_strreturn what does strcpywant exactly.

双方datac_str返回什么strcpy到底要。