如何在C++中声明向量?

Mor*_*ock 1 c++ vector

我试图在我的代码中使用字符串向量而不是字符串数组,但显然我错过了向量声明中的一些细节.使用以下代码,我收到此错误:‘vector’ was not declared in this scope

// Try to implement a vector of string elements

#include<iostream>

using namespace std;

int main() {
    const int MAX_ITEMS = 10;
    vector<string> my_vector(MAX_ITEMS);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我该如何正确地声明向量?

hel*_*ium 10

你必须包括标题:

#include <vector>
#include <string>
Run Code Online (Sandbox Code Playgroud)


IVl*_*lad 9

你应该添加这些包括:

#include <vector>
#include <string>
Run Code Online (Sandbox Code Playgroud)