Just My Life & My Work




	Theme: string & char[] transform
	Compiler: Dev C++
	Date: 100/05/01
	Author: ShengWen
	Blog: https://cg2010studio.wordpress.com/
using namespace std;
int main(){
	string test_string="test_string";
	char test_char[]="test_char", result_char[20];

	string result_string1(test_char);
	string result_string2;
	strcpy(result_char, test_string.c_str());

	cout<<"test_string: "<<test_string<<endl;
	cout<<"test_char: "<<test_char<<endl;
	cout<<"result_string1: "<<result_string1<<endl;
	cout<<"result_string2: "<<result_string2<<endl;
	cout<<"result_char: "<<result_char<<endl;



test_string: test_string
test_char: test_char
result_string1: test_char
result_string2: test_char
result_char: test_string
請按任意鍵繼續 . . .

可見到stringchar[]相當簡單,只要呼叫string class的成員函式c_str(),即可將string轉為char[]
那麼char[]string呢?有兩種方法,第一種是初始string變數時,即把char[]當作參數來初始化,第二種則是使用string class的成員函式assign(char[])來將char[]指定為string變數。

Comments on: "[C++] string 和 char[]互轉 (string and char[] transform)" (7)

  1. penumbuh rambut said:

    Excellent article! We are linking to this great content on our website.
    Keep up the good writing.

  2. Hello are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you require any html coding expertise to make your own blog? Any help would be really appreciated!

  3. I don’t drop a comment, however after reading a few of the comments on this page [C++] string 和 char[]互轉 (string and char[] transform) | 逍遙文工作室. I actually do have some questions for you if you don’t mind.
    Could it be just me or does it look like some of these remarks come across like they are left by brain dead folks? 😛 And, if you are posting at additional sites, I would like to keep up with you. Would you list of all of your social sites like your linkedin profile, Facebook page or twitter feed?

  4. […] 就如同string 和 char[]互轉這篇文章一樣,時常需要將字串來回轉換,在OpenCV中處理對象為影像,有時也需要來回轉換,於是我想記錄一下:P […]

  5. […] [C++] string 和 char[]互轉 (string and char[] transform) […]



WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


%d 位部落客按了讚: