单击此处获取向量集1。
修饰符
1.1分配(首先输入input_iterator,最后输入input_iterator)–将新内容分配给矢量并调整大小
1.2 Assign(size_type n,const value_type g)–将新内容分配给vector并调整大小
#include
#include
using namespace std;
int main()
{
vector g1;
vector g2;
vector g3;
g1.assign(5, 10); // 5 elements with value 10 each
vector :: iterator it;
it = g1.begin() + 1;
g2.assign(it, g1.end() - 1); // the 3 middle values of g1
int gquiz[] = {1, 2};
g3.assign(gquiz, gquiz + 2); // assigning from array
cout << "Size of g1: " << int(g1.size()) << '\n';
cout << "Size of g2: " << int(g2.size()) << '\n';
cout << "Size of g3: " << int(g3.size()) << '\n';
return 0;
}
上面程序的输出是:
Size of g1: 5
Size of g2: 3
Size of g3: 2
2. push_back(const value_type g)–在向量的末尾添加一个新元素“ g”,并将向量容器的大小增加1
3. pop_back()–删除向量结尾处的元素,即最后一个元素,并将向量容器的大小减小1
#include
#include
using namespace std;
int main()
{
vector gquiz;
int sum = 0;
gquiz.push_back(10);
gquiz.push_back(20);
gquiz.push_back(30);
while (!gquiz.empty())
{
sum += gquiz.back();
gquiz.pop_back();
}
cout << "The sum of the elements of gquiz is : "
<< sum << '\n';
return 0;
}
上面程序的输出是:
The sum of the elements of gquiz is : 60
4.1 insert(const_iterator q,const value_type g)–在迭代器“ q”引用的元素之前添加元素“ g”,并返回指向新添加的元素的迭代器
4.2insert(const_iterator q,size_type n,const value_type g)–在迭代器“ q”当前引用的元素之前添加值分别为“ g”的“ n”个元素,并返回指向新添加元素中第一个的迭代器
4.3 insert(const_iterator q,InputIterator在前,InputIterator在后)–从第一个到最后添加一系列元素,这些元素被插入到当前由“ q”引用的位置之前
#include
#include
using namespace std;
int main()
{
vector gquiz1(3, 10);
vector :: iterator it;
it = gquiz1.begin();
it = gquiz1.insert(it, 20);
gquiz1.insert(it, 2, 30);
it = gquiz1.begin();
vector gquiz2(2, 40);
gquiz1.insert(it + 2, gquiz2.begin(), gquiz2.end());
int gq [] = {50, 60, 70};
gquiz1.insert(gquiz1.begin(), gq, gq + 3);
cout << "gquiz1 contains : ";
for (it = gquiz1.begin(); it < gquiz1.end(); it++)
cout << *it << '\t';
return 0;
}
上面程序的输出是:
gquiz1 contains : 50 60 70 30 30
40 40 20 10 10 10
5.1 delete(const_iterator q)–删除由“ q”引用的元素,并向该元素返回一个迭代器,后跟已删除的元素
5.2擦除(const_iterator首先,const_iterator最后)–删除范围从第一个到最后的元素,范围中包括第一个迭代器,不包括最后一个迭代器,并向该元素返回一个迭代器,然后返回最后一个删除的元素
#include
#include
using namespace std;
int main ()
{
vector gquiz;
for (int i = 1; i <= 10; i++)
gquiz.push_back(i * 2);
// erase the 5th element
gquiz.erase(gquiz.begin() + 4);
// erase the first 5 elements:
gquiz.erase(gquiz.begin(), gquiz.begin() + 5);
cout << "gquiz contains :";
for (int i = 0; i < gquiz.size(); ++i)
cout << gquiz[i] << '\t';
return 0;
}
上面程序的输出是:
gquiz contains :14 16 18 20
6. swap(向量q,向量r)–交换’q’和’r’的内容
7. clear()–从向量中删除所有元素
#include
#include
using namespace std;
int main()
{
vector gquiz1;
vector gquiz2;
vector :: iterator i;
gquiz1.push_back(10);
gquiz1.push_back(20);
gquiz2.push_back(30);
gquiz2.push_back(40);
cout << "Before Swapping, \n"
<<"Contents of vector gquiz1 : ";
for (i = gquiz1.begin(); i != gquiz1.end(); ++i)
cout << *i << '\t';
cout << "\nContents of vector gquiz2 : ";
for (i = gquiz2.begin(); i != gquiz2.end(); ++i)
cout << *i << '\t';
swap(gquiz1, gquiz2);
cout << "\n\nAfter Swapping, \n";
cout << "Contents of vector gquiz1 : ";
for (i = gquiz1.begin(); i != gquiz1.end(); ++i)
cout << *i << '\t';
cout << "\nContents of vector gquiz2 : ";
for (i = gquiz2.begin(); i != gquiz2.end(); ++i)
cout << *i << '\t';
cout << "\n\nNow, we clear() and then add "
<< "an element 1000 to vector gquiz1 : ";
gquiz1.clear();
gquiz1.push_back(1000);
cout << gquiz1.front();
return 0;
}
上面程序的输出是:
Before Swapping,
Contents of vector gquiz1 : 10 20
Contents of vector gquiz2 : 30 40
After Swapping,
Contents of vector gquiz1 : 30 40
Contents of vector gquiz2 : 10 20
Now, we clear() and then add an element 1000 to vector gquiz1 : 1000