How to copy a string of std::string type in C++?

前端 未结 4 1935
滥情空心
滥情空心 2020-12-24 12:25

I used the strcpy() function and it only works if I use C-string arrays like:

char a[6] = "text";
char b[6] = "image";
strcpy         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 13:26

    strcpy example:

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

    Output: str1: Sample string

    Your case:

    A simple = operator should do the job.

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

提交回复
热议问题