《C++ Primer》學習筆記:向vector對象添加元素蘊含的編程假定。本站提示廣大學習愛好者:(《C++ Primer》學習筆記:向vector對象添加元素蘊含的編程假定)文章只能為提供參考,不一定能成為您想要的結果。以下是《C++ Primer》學習筆記:向vector對象添加元素蘊含的編程假定正文
練習《C++ Primer》中的3.14節時,當敲入:
#include <iostream>
#include <string>
using namespace std;
int main(){
string word;
vector<string> text;
while (cin >> word)
text.push_back(word);
return 0;
}
程序會報錯:
error: use of undeclared identifier 'vector'
其實應該插入一行:
#include <vector>
變成:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string word;
vector<string> text;
while (cin >> word)
text.push_back(word);
return 0;
}
才不會報錯。
需要注意的是vector需要使用命名空間std,所以需要鍵入std::vector或在開頭敲入using namespace std; 。