阅读量:138
要向string数组添加元素,可以使用数组的push()方法或直接通过索引赋值的方式。
使用push()方法:
#include
#include
#include
int main() {
std::vector myArray;
myArray.push_back("Hello");
myArray.push_back("World");
for (std::string element : myArray) {
std::cout << element class="hljs-string">" ";
}
return 0;
}
直接通过索引赋值:
#include
#include
int main() {
std::string myArray[2];
myArray[0] = "Hello";
myArray[1] = "World";
for (std::string element : myArray) {
std::cout << element class="hljs-string">" ";
}
return 0;
}
无论使用哪种方法,都可以向string数组添加元素。