溫馨提示×

string數組如何添加元素

小億
830
2023-12-27 21:48:17
欄目: 編程語言

要向string數組添加元素,可以使用數組的push()方法或直接通過索引賦值的方式。

使用push()方法:

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

int main() {
    std::vector<std::string> myArray;

    myArray.push_back("Hello");
    myArray.push_back("World");

    for (std::string element : myArray) {
        std::cout << element << " ";
    }

    return 0;
}

直接通過索引賦值:

#include <iostream>
#include <string>

int main() {
    std::string myArray[2];

    myArray[0] = "Hello";
    myArray[1] = "World";

    for (std::string element : myArray) {
        std::cout << element << " ";
    }

    return 0;
}

無論使用哪種方法,都可以向string數組添加元素。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女