C++輕松實(shí)現(xiàn)字符串與字符數(shù)組的相互轉(zhuǎn)換
引言
本文重點(diǎn)介紹在 C++ 中將字符串轉(zhuǎn)換為 char 數(shù)組和將 char 數(shù)組轉(zhuǎn)換為字符串的不同方法。
一、將字符串轉(zhuǎn)換為 char 數(shù)組
C++ 提供了以下將字符串轉(zhuǎn)換為 char 數(shù)組的技術(shù):
- 使用 c_str()和 strcpy() 函數(shù)。
- 使用 for 循環(huán)。
1.1、C++ 中的 c_str()和 strcpy()函數(shù)
C++ 函數(shù)c_str()以及 C++ 字符串函數(shù)strcpy()可用于輕松地將字符串轉(zhuǎn)換為字符數(shù)組。
c_str()
方法表示字符串?dāng)?shù)組中的字符序列,后跟一個(gè)空字符 '\0'
。它返回指向字符串的 null 指針。
語法:
string-name.c_str();
- 首先使用 c_str() 方法來獲取字符串的所有字符以及終止 null 字符。
- 此外,聲明一個(gè) char 類型的空數(shù)組來存儲結(jié)果,即將字符串轉(zhuǎn)換為 char 數(shù)組的結(jié)果。
- 最后,使用
strcpy()
方法將c_str()
方法生成的字符序列復(fù)制到空 char 數(shù)組中。
示例:
#include <bits/stdc++.h> using namespace std; int main() { string str = ""; cout<<"Enter the string:\n"; cin>>str; char arr[str.length() + 1]; strcpy(arr, str.c_str()); cout<<"String to char array conversion:\n"; for (int i = 0; i < str.length(); i++) cout << arr[i]; return 0; }
1.2、使用 for 循環(huán)中的字符串到字符數(shù)組的轉(zhuǎn)換
為了將 char 數(shù)組轉(zhuǎn)換為字符串,可以使用 C++ for 循環(huán)。
- 首先創(chuàng)建一個(gè) char 類型的空數(shù)組。
- 然后遍歷輸入字符串。
- 在迭代時(shí)將字符存儲到 char 數(shù)組中。
示例:
#include <bits/stdc++.h> using namespace std; int main() { string str = ""; cout<<"Enter the string:\n"; cin>>str; char arr[str.length() + 1]; cout<<"String to char array conversion:\n"; for (int x = 0; x < sizeof(arr); x++) { arr[x] = str[x]; cout << arr[x]; } return 0; }
二、將 char 數(shù)組轉(zhuǎn)換為字符串
用于在 C++ 中將 char 數(shù)組轉(zhuǎn)換為字符串的技術(shù):
- “+”運(yùn)算符。
- C++ 重載“=”運(yùn)算符。
- C++ 內(nèi)置構(gòu)造函數(shù)。
2.1、C++ 運(yùn)算符 ‘+’
C++提供了將數(shù)據(jù)項(xiàng)連接或添加到變量的功能:'+' operator
。
- 創(chuàng)建一個(gè)新的空字符串來存儲結(jié)果。
- 接下來,使用 for 循環(huán)遍歷輸入 char 數(shù)組。
- 在遍歷數(shù)組的過程中,使用 ‘+’ 運(yùn)算符將字符連接到字符串。
示例:
#include <bits/stdc++.h> using namespace std; int main() { char arr[] = { 'L', 'I', 'O', 'N', 'L', 'O', 'N', 'G'}; int size_arr = sizeof(arr) / sizeof(char); string str = ""; for (int x = 0; x < size_arr; x++) { str = str + arr[x]; } cout<<"Converted char array to string:\n"; cout << str << endl; return 0; }
輸出:
Converted char array to string: LIONLONG
2.2、C++ 重載 ‘=’ 運(yùn)算符
C++具有重載的概念,它使操作員執(zhí)行基本或默認(rèn)操作以外的其他操作。
- 創(chuàng)建一個(gè)新的空字符串。
- 使用
'=' operator overload
將數(shù)據(jù)項(xiàng)逐個(gè)字符存儲到新創(chuàng)建的空字符串中。
示例:
#include <bits/stdc++.h> using namespace std; int main() { char arr[] = { 'L', 'I', 'O', 'N', 'L', 'O', 'N', 'G'}; int size_arr = sizeof(arr) / sizeof(char); string str = ""; str = arr; cout<<"Converted char array to string:\n"; cout << str << endl; return 0; }
輸出:
Converted char array to string: LIONLONG
2.3、C++ 字符串內(nèi)置構(gòu)造函數(shù)
在將 char 數(shù)組轉(zhuǎn)換為字符串的上下文中,可以使用 C++ 字符串構(gòu)造函數(shù)。
語法:
string string-name(char array-name);
此構(gòu)造函數(shù)采用以 null 字符結(jié)尾的字符序列作為輸入?yún)?shù)。
注意:這只能在整個(gè)程序中聲明字符串時(shí)使用。
示例:
#include <bits/stdc++.h> using namespace std; int main() { char arr[] = { 'L', 'I', 'O', 'N', 'L', 'O', 'N', 'G'}; int size_arr = sizeof(arr) / sizeof(char); string str(arr); cout<<"Converted char array to string:\n"; cout <<str<< endl; return 0; }
輸出:
Converted char array to string: LIONLONG
三、總結(jié)
在本文中了解了在 C++ 中將字符串轉(zhuǎn)換為 char 數(shù)組的各種技術(shù),反之亦然。
到此這篇關(guān)于C++輕松實(shí)現(xiàn)字符串與字符數(shù)組的相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)C++字符串與字符數(shù)組互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++函數(shù)指針與指針函數(shù)有哪些關(guān)系和區(qū)別
函數(shù)指針是一個(gè)指針變量,它可以存儲函數(shù)的地址,然后使用函數(shù)指針,這篇文章主要介紹了C++中函數(shù)指針與指針函數(shù)有哪些關(guān)系和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值2022-08-08C++實(shí)現(xiàn)中綴轉(zhuǎn)后綴的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)中綴轉(zhuǎn)后綴的問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09C語言實(shí)現(xiàn)隨機(jī)生成6位數(shù)密碼
這篇文章主要為大家詳細(xì)介紹了如何使用C語言實(shí)現(xiàn)一個(gè)簡單而實(shí)用的隨機(jī)密碼生成器,該生成器將生成包含字母、數(shù)字和特殊字符的隨機(jī)密碼,有需要的小伙伴可以參考下2023-11-11C++實(shí)現(xiàn)LeetCode(166.分?jǐn)?shù)轉(zhuǎn)循環(huán)小數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(166.分?jǐn)?shù)轉(zhuǎn)循環(huán)小數(shù))2021-07-07C/C++中關(guān)于字符串的常見函數(shù)操作大全
這篇文章主要介紹了C/C++中關(guān)于字符串的常見函數(shù)操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03