1. 欲使用向量,必須先在標頭檔寫入 #include <vector>
2. 在記憶空間上,向量的每個元素之間所佔的記憶空間是相鄰的(這點和矩陣相同)。但為使向量可以隨時加入元素,一個向量所使用的記憶空間便不是恆定的。
向量所佔有的記憶空間分為兩種:一為既用空間,即向量既有元素所佔有的空間。一為預留空間,即向量在建構時預留給元素用的空間(既用空間+尚未用到的空間)。若加入的元素溢出了預留空間,系統會自動將向量搬移至與更多空白空間連接的空間,並且增加預留空間以供更多元素加入。
3. 向量的建構有許多方式:
vector<資料形態> 向量名稱 ; //建構向量,但不設向量大小與初始值
vector<資料形態> 向量名稱(n) ; //建構向量,設向量大小為可容納n個元素
vector<資料形態> 向量名稱(n, 資料值) ; //建構向量,設向量大小為可容納n個元素,且每個元素都是資料值。如:vector<int> V1(10, 5); vector<char> V2(10,’a’)
註:資料型態不只可為一般常見的資料形態,如int、string、bool、double…等,也可以為結構物件或者類別物件。不過物件必須寫在主程式之前:
class person //建構類別person
{
public:
string name;
int age;
person(string name_,int age_,int money_)
{name=name_;age=age_; money = money_;}
private:
int money;
};
void main( ){ //主程式在此
person person1("wiewie",18,2000);
person person2("nunu",19,2500);
person person3("ting",23,1800);
vector<person> a; //建立存放person物件的向量a
vector<person>::iterator i; //宣告存放person物件之向量的迭代器i(請參看迭代器篇)
a.push_back(person1);
a.push_back(person2);
a.push_back(person3);
for(i=a.begin();i!=a.end();i++)
{cout<<(*i).name<<",";} //顯示:weiwei,nunu,ting,
}
註一:cout<<(*i).name<<",";若是寫成cout<<*i.name<<",";,編譯器會以為是cout<<*(i.name)<<",";,即會發生編譯錯誤。
註二:二維向量之宣告法為:
vector< vector<資料形態> > 向量名稱;
要注意的是,>與>必須要用空白字元隔開,如果寫成>>,編譯器會以為這是輸入運算子,而產生錯誤。

老師好 我目前試著要把我之前建立的檔案讀回,並放入struct的vector內,再顯示在螢幕上 卻都無法成功 1.我不知道這樣的寫法能不能把資料存回vector內 2.我是把存檔的方法類似倒著寫成讀檔 3.我用記事本確認 d:\\1data\\strc.txt 是存在並且有資料的 能不能請老師幫我看一下程式碼,並看看我觀念上是否不正確 謝謝 #include
#include
#include
#include
using namespace std;
struct Company{
string name;
string ceo;
float income;
int employess;
};
int main(){
vector comp;
const char* filename = "d:\\1data\\strc.txt";
fstream ifile(filename, ios::in | ios::out);
vector::iterator it1;
if(!ifile)
{
cout << filename << "開啟失敗";
return 1;
}
for(it1=comp.begin(); it1!=comp.end(); it1++)
{
ifile >> (*it1).name;
ifile >> (*it1).ceo;
ifile >> (*it1).income;
ifile >> (*it1).employess;
}
ifile.close();
cout << "\n\n開始顯示....\n\n";
vector::iterator it2;
for(it2=comp.begin(); it2!=comp.end(); it2++)
{
cout << "Name: " << (*it2).name << endl
<< "CEO: " << (*it2).ceo << endl
<< "Income: " << (*it2).income << endl
<< "Employees: " << (*it2).employess << endl << endl;
}
return 0;
}
.
你好 你的comp向量從頭到尾都沒有任何元素 所以comp.begin()==comp.end() 如此一來,兩個for loop 中的程式碼是不會執行的
原來是這樣 難怪一直沒辦法 cout comp內的成員 謝謝指點 我再想想該如何改進程式碼
老師好 我又來了 這次是... 如果我的檔案內是兩筆資料..在列印出來的時候第二筆會列兩次 只要把資料增到三筆或更多筆時就能正常 請老師指導一下...我又掉入哪個坑了 執行結果: Name: 英特爾 CEO: Bob Income: 91213.1 Employees: 110823 Name: LaLa CEO: Tim Income: 131231 Employees: 137031 Name: LaLa CEO: Tim Income: 131231 Employees: 137031 -------------------------------- Process exited after 0.0133 seconds with return value 0 請按任意鍵繼續 . . . Name: LaLa CEO: Tim Income: 131231 Employees: 137031 就是列兩次 我的程式碼: #include
#include
#include
#include // 讀寫檔
using namespace std;
struct Company{
string name;
string ceo;
float income;
int employess;
};
int main(){
vector comp;
const char* filename = "d:\\1data\\strc.txt";
fstream ifile(filename, ios::in | ios::out);
if(!ifile)
{
cout << filename << "開啟失敗";
return 1;
}
string strA ;
string strB ;
float fltC ;
int intD ;
while(!ifile.eof())
{
ifile >> strA >> strB >> fltC >> intD;
comp.push_back({strA, strB, fltC, intD});
}
ifile.close();
vector::iterator it;
for(it=comp.begin(); it!=comp.end(); it++)
{
cout << "Name: " << (*it).name << endl
<< "CEO: " << (*it).ceo << endl
<< "Income: " << (*it).income << endl
<< "Employees: " << (*it).employess << endl << endl;
}
return 0;
}
謝謝喔~~
可能是你的strc.txt中,在資料的末尾多打了空白鍵或換行, 導致讀完資料後還沒到達ifile.eof(),又再讀一次, 但ifile >> strA >> strB >> fltC >> intD;沒執行完整,而向量多加進一次相同的元素。 你這樣讀資料很危險,建議改成一行一行讀。 讀進來的每行先確定他是不是空白字元、或根本無字元、或不合預期中的變數型態,再push_back進向量。
哇~~ 真的在strc.txt資料的末尾多打了空白鍵 刪除那一個空白鍵就正常了 (((神!))) 謝謝老師 另外... 讀進來的每行先確定他是不是空白字元、或根本無字元、或不合預期中的變數型態,再push_back進向量。 毫無頭緒啊~~~ 我再想想!!!