前言
此文章中會整理所有在 C/C++ 字串 的互相轉換用法,
包含 char string stringstream 的互相轉換,
其中 char 我們又會分成 char array, char pointer 介紹。
這篇文章也是將我另一篇文章中的「互相轉換」獨立出來討論的內容,沒經驗者建議可以先參考前一篇文章。請見下文:
【C++ 字串處理 #2】字串 char string stringstream 相關用法總整理 (內含範例程式碼) 與利用 sprinf, snprinf assign 值的方法
先講結論,我們先觀察以下程式碼與結果
建議先自己實際操作過後,會比直接看結論學得更快哦!
before→ / after↓ | Char array | Char pointer | String | stringstream |
---|---|---|---|---|
Char array | strcpy(dst, src) | strcpy(Char array, Char pointer) | strcpy(Char array, String.c_str() ) | strcpy(Char array, stringstream.str().c_str()) |
Char pointer | Char pointer = Char array 或 Char pointer = &Char array[0] | x | Char pointer = String.c_str() | Char pointer = stringstream.str().c_str() |
String | string = Char array | String = Char pointer | x | String = stringstream.str() |
stringstream | stringstream << Char array | stringstream << Char pointer | stringstream << String | x |
從 Char array 出發,轉成其他的字串型態
可以特別注意在 Char array 轉換成 Char pointer 中,我們有兩種轉換方式,
- 其中一種是直接將值傳入 「*p」(值) = 傳入值,
- 另外一種是將位置傳入 = &s0[0] (string 的第 0 個位置)
範例程式碼
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
char s0[30] = "Hello World!";
char* s1 = s0;
cout << s1 << endl;
char* s2 = &s0[0];
cout << s2 << endl;
string s3 = s0;
cout << s1 << endl;
stringstream s4;
s4 << s0;
cout << s4.str() << endl;
return 0;
}
編譯與結果
> g++ test4.cpp -o a.out && ./a.out
Hello World!
Hello World!
Hello World!
Hello World!
從 Char pointer 出發,轉成其他的字串型態
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream s0;
s0 << "Hello World!";
char s1[30];
strcpy(s1, s0.str().c_str());
cout << s1 << endl;
const char *s2 = s0.str().c_str();
cout << s2 << endl;
string s3 = s0.str();
cout << s3 << endl;
return 0;
}
編譯與結果
> g++ test4.cpp -o a.out && ./a.out
Hello World!
Hello World!
Hello World!
從 String 出發,轉成其他的字串型態
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string s0 = "Hello World!";
char s1[30];
strcpy(s1, s0.c_str());
cout << s1 << endl;
const char *s2 = s0.c_str();
cout << s2 << endl;
stringstream s3;
s3 << s0;
cout << s3.str() << endl;
return 0;
}
編譯與結果
> g++ test4.cpp -o a.out && ./a.out
Hello World!
Hello World!
Hello World!
從 stringstream 出發,轉成其他的字串型態
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream s0;
s0 << "Hello World!";
char s1[30];
strcpy(s1, s0.str().c_str());
cout << s1 << endl;
const char *s2 = s0.str().c_str();
cout << s2 << endl;
string s3 = s0.str();
cout << s3 << endl;
return 0;
}
編譯與結果
> g++ test4.cpp -o a.out && ./a.out
Hello World!
Hello World!
Hello World!
Reference
- char array, string, stringstream
- c++:像sprintf一樣的std :: string格式
- How to convert a char array to a string?
- 字串長度、複製、串接
- strlen() – C語言庫函數
- Finding the length of a Character Array in C
- C++ – pointer to char、array of pointer to char、pointer to array of char (C++軟體開發 – 指標與字元與陣列 概念與實例)
- stringstream用法整理
[…] https://www.wongwonggoods.com/cplusplus/convert-char-string-stringstream/ […]