vector的主要函数及例题
构造
// constructing vectors
#include <iostream>
#include <vector>
using namespace std;
int main (){
unsigned int i;
// constructors used in the same order as described above:
vector<int> first; // 空vector
vector<int> second (4,100); // 4个100,{100,100,100,100}
vector<int> third (second.begin(),second.end()); // 给定区间初始化
vector<int> fourth (third); // 把vector变量third拷贝到fourth完成初始化
vector<int> five(100); // 100个0,但还是写成 five(100,0) 更好
// 也可以用数组初始化
int myints[] = {16,2,77,29};
vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
cout << "The contents of fifth are:";
for (i=0; i < fifth.size(); i++)
cout << " " << fifth[i];
cout << endl;
return 0;
}
Output: The contents of fifth are: 16 2 77 29
赋值,= 号操作符
// vector assignment
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> first (3,0);//{0,0,0}
vector<int> second (5,0);//{0,0,0,0,0}
second=first; //{0,0,0},相当于用first把second覆盖掉
first=vector<int>();//用一个空的vector赋值
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
return 0;
}
两个 int 元素向量都初始化为大小不同的零序列。 然后,将 second 分配给 first,因此现在两者相等且大小为 3。然后,将 first 分配给一个新构造的空对象,因此其大小最终为0。
vector::begin,vector::end
// vector::begin
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
vector<int>::iterator it;
cout << "myvector contains:";
for ( it=myvector.begin() ; it < myvector.end(); it++ )
cout << " " << *it;
cout << endl;
return 0;
}
Output: myvector contains: 1 2 3 4 5
vector::rbegin,vector::rend
// vector::rbegin/rend
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
cout << "myvector contains:";
vector<int>::reverse_iterator rit;
for ( rit=myvector.rbegin() ; rit < myvector.rend(); ++rit )
cout << " " << *rit;
cout << endl;
return 0;
}
Output: myvector contains: 5 4 3 2 1
vector::size
// vector::size
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myints;
cout << "0. size: " << (int) myints.size() << endl;
for (int i=0; i<10; i++) myints.push_back(i);
cout << "1. size: " << (int) myints.size() << endl;
myints.insert (myints.end(),10,100);
cout << "2. size: " << (int) myints.size() << endl;
myints.pop_back();
cout << "3. size: " << (int) myints.size() << endl;
return 0;
}
Output:
0. size: 0
1. size: 10
2. size: 20
3. size: 19
vector::empty
// vector::empty
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector;
int sum (0);
for (int i=1;i<=10;i++) myvector.push_back(i);
while (!myvector.empty()){
sum += myvector.back();
myvector.pop_back();
}
cout << "total: " << sum << endl;
return 0;
}
Output: total: 55
vector::operator[ ]
// vector::operator[]
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector (10); // 10 zero-initialized elements
unsigned int i;
vector<int>::size_type sz = myvector.size();
// assign some values:
for (i=0; i<sz; i++) myvector[i]=i;
// reverse vector using operator[]:
for (i=0; i<sz/2; i++){
int temp;
temp = myvector[sz-1-i];
myvector[sz-1-i]=myvector[i];
myvector[i]=temp;
}
cout << "myvector contains:";
for (i=0; i<sz; i++)
cout << " " << myvector[i];
cout << endl;
return 0;
}
Output: myvector contains: 9 8 7 6 5 4 3 2 1 0
vector::front
// vector::front
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector;
myvector.push_back(77);
myvector.push_back(16);
// now front equals 77, and back 16
myvector.front() -= myvector.back();
cout << "myvector.front() is now " << myvector.front() << endl;
return 0;
}
Output: myvector.front() is now 61
vector::back
// vector::back
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector;
myvector.push_back(10);
while (myvector.back() != 0){
myvector.push_back ( myvector.back() -1 );
}
cout << "myvector contains:";
for (unsigned i=0; i<myvector.size() ; i++)
cout << " " << myvector[i];
cout << endl;
return 0;
}
Output: myvector contains: 10 9 8 7 6 5 4 3 2 1 0
vector::push_back
// vector::push_back
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector;
int myint;
cout << "Please enter some integers (enter 0 to end):\n";
do {
cin >> myint;
myvector.push_back (myint);
} while (myint);
cout << "myvector stores " << (int) myvector.size() << " numbers.\n";
return 0;
}
vector::pop_back
// vector::pop_back
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector;
int sum (0);
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
while (!myvector.empty()){
sum+=myvector.back();
myvector.pop_back();
}
cout << "The elements of myvector summed " << sum << endl;
return 0;
}
vector::insert
// inserting into a vector
#include <iostream>
#include <vector>
using namespace std;
int main (){
vector<int> myvector (3,100);
vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
vector<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
cout << " " << *it;
cout << endl;
return 0;
}
Output: myvector contains: 501 502 503 300 300 400 400 200 100 100 100
vector::erase
// erasing from vector
#include <iostream>
#include <vector>
using namespace std;
int main (){
unsigned int i;
vector<unsigned int> myvector;
// set some values (from 1 to 10)
for (i=1; i<=10; i++) myvector.push_back(i);
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++)
cout << " " << myvector[i];
cout << endl;
return 0;
}
Output: myvector contains: 4 5 7 8 9 10
vector::swap
// swap vectors
#include <iostream>
#include <vector>
using namespace std;
main (){
unsigned int i;
vector<int> first (3,100); // three ints with a value of 100
vector<int> second (5,200); // five ints with a value of 200
first.swap(second);
cout << "first contains:";
for (i=0; i<first.size(); i++) cout << " " << first[i];
cout << "\nsecond contains:";
for (i=0; i<second.size(); i++) cout << " " << second[i];
cout << endl;
return 0;
}
Output: first contains: 200 200 200 200 200 second contains: 100 100 100
vector::clear
// clearing vectors
#include <iostream>
#include <vector>
using namespace std;
int main (){
unsigned int i;
vector<int> myvector;
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++) cout << " " << myvector[i];
myvector.clear();
myvector.push_back (1101);
myvector.push_back (2202);
cout << "\nmyvector contains:";
for (i=0; i<myvector.size(); i++) cout << " " << myvector[i];
cout << endl;
return 0;
}
Output: myvector contains: 100 200 300myvector contains: 1101 2202