c++创建string
时间: 2023-08-18 07:07:35 浏览: 47
要创建一个字符串对象,你可以使用string类的构造函数。以下是一些示例代码来创建字符串对象:
示例1:
```c++
#include <iostream>
#include <string>
int main(){
std::string S1{ "jn10010537" }; //用一个字符串常量创建string对象;
char charArray1[] = { 'j', 'n', '1', '0', '0','1', '0','5','3','7', '\0' };
std::string S2{ charArray1 };//用字符串数组创建string对象
char charArray2[] = { 'j', 'n', '1', '0', '0','1', '0','5','3','7'};
std::string S3{ charArray2 };//用字符串数组创建string对象
std::cout << (S1 == S2) << std::endl;
std::cout << (S2 == S3) << std::endl;
std::cout << S1 << std::endl;
std::cout << S2 << std::endl;
return 0;
}
```
这段代码演示了三种不同的方式创建字符串对象,分别是使用字符串常量、字符串数组和字符数组。通过在构造函数中传递不同的参数,可以创建不同的字符串对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [【C++】4-1.16 创建 string 对象](https://blog.csdn.net/jn10010537/article/details/116277585)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [C++自定义实现string类型](https://blog.csdn.net/qq_51004011/article/details/125967829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)