C++字符串作為參數的傳遞


1.c++ 有兩種風格的字符串形式
1)char a[]={'h','e','l','l','o','\0'}  或者  char a[]="hello"; //C++ 編譯器會在初始化數組時,自動把 '\0' 放在字符串的末尾
;長度:strlrn(a);

2)  string a="hello"; 
  輸出:cout<<a
或者for(int i=0;i<strlen(a);i++)  cout<<a[i](或者a.at(i) );長度:a.size();
2.字符串作為參數傳入函數的兩種方法

#include <iostream>
#include <string> 

using namespace std;

 
int getFilePath(char *str_test1,const string& str_test2)
{  
     cout<< str_test1<<endl;
     printf("%s",str_test2.c_str());
     
}
 
int main ()
{
     char str_test1[] = "測試路徑1";
     string str_test2 = "測試路徑2\n";
     getFilePath(str_test1,str_test2);


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM