使用C++将三个字符串转换为字符串数组

可以使用vector来存储这三个字符串,并将vector转换为字符串数组。

示例代码

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    string str1 = 'hello';
    string str2 = 'world';
    string str3 = '!';
    
    vector<string> strVec = {str1, str2, str3};
    
    string strArr[3];
    
    for(int i = 0; i < 3; i++) {
        strArr[i] = strVec[i];
    }
    
    // 输出string array
    for(int i = 0; i < 3; i++) {
        cout << strArr[i] << ' '; 
    }
    cout << endl;
    
    return 0;
}

输出结果

hello world ! 
C++ 将三个字符串转换为字符串数组

原文地址: https://www.cveoy.top/t/topic/mLnN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录