1 vector简介
vector是序列式容器的一种。所谓序列式容器,其中的元素都可序,但未必有序。vector的数据安排和操作方式与array非常 相似。两者唯一的差别是空间的灵活性。array是静态空间,一开始就需要设定大小,有时不够用,有时候又浪费(旱的旱死,涝的涝死啊)。vector就不同了,吃多少拿多少。
2 vector实例化
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> first; // empty vector of ints
vector<int> second(4, 100); // four ints with value 100
vector<int> third(second.begin(), second.end()); // iterating through second
vector<int> fourth(third); // a copy of third
int myints[] = { 16,2,77,29 };
vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
cout << "first :";
for (auto it = first.begin(); it != first.end(); ++it)
{
cout << ' ' << *it;
}
cout << '\n';
cout << "second :";
for (auto it = second.begin(); it != second.end(); ++it)
{
cout << ' ' << *it;
}
cout << '\n';
cout << "third :";
for (auto it = third.begin(); it != third.end(); ++it)
{
cout << ' ' << *it;
}
cout << '\n';
cout << "fourth :";
for (auto it = fourth.begin(); it != fourth.end(); ++it)
{
cout << ' ' << *it;
}
cout << '\n';
cout << "fifth :";
for (auto it = fifth.begin(); it != fifth.end(); ++it)
{
cout << ' ' << *it;
}
cout << '\n';
system("pause");
return 0;
}
first :
second : 100 100 100 100
third : 100 100 100 100
fourth : 100 100 100 100
fifth : 16 2 77 29
请按任意键继续. . .
3 vector遍历
vector::begin() //返回迭代器指向第一个元素
vector::end() //指向最后一个元素后面的那个元素(past-the-end超过结尾)
vector::cbegin() //返回const_iterator指向第一个元素
vector::cend() //返回const_iterator指向最后一个后面的那个元素
vector::rbegin() //反向迭代器,指向最后一个元素
vector::rend() //反向迭代器,指向第一个元素之前的那个元素
vector::crbegin() //return const_reverse_iterator to reverse beginning
vector::crend() //返回const_reverse_iterator指向第一个元素之前的那个元素
vector::at(int n) //返回n位置元素的引用
vector::front() //返回第一个元素的引用
vector::back() //返回最后元素的引用
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> test(10);//test.size = 10
cout << "test.size: " << test.size() << endl; //result :10
for (auto i = 0; i < 10; i++)
{
test.at(i) = i;
}
cout << "original test: ";
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\ncbegin&cend test: ";
for (auto it = test.cbegin(); it != test.cend(); it++)
{
cout << " " << *it;
}
cout << "\ncrbegin&crend test: ";
for (auto it = test.crbegin(); it != test.crend(); it++)
{
cout << " " << *it;
}
cout << "\nreverse test: ";
for (auto it = test.rbegin(); it != test.rend(); it++)
{
cout << " " << *it;
}
cout << "\n";
cout << "test.front: " << test.front() << " test.back: " << test.back() << endl;
system("pause");
return 0;
}
test.size: 10
original test: 0 1 2 3 4 5 6 7 8 9cbegin&cend test: 0 1 2 3 4 5 6 7 8 9
crbegin&crend test: 9 8 7 6 5 4 3 2 1 0
reverse test: 9 8 7 6 5 4 3 2 1 0
test.front: 0 test.back: 9
请按任意键继续. . .
4 插入删除
// vector::insert(iterator it, const T& x) :向量中迭代器指向元素前增加一个元素x
//vector::insert(iterator it, int n, const T& x) :向量中迭代器指向元素前增加n个相同的元素x
//vector::insert(iterator it, const_iterator first, const_iterator last) :
//向量中迭代器指向元素前插入另一个相同类型向量的[first, last)间的数据
//vector::push_back(const T& x):最后位置插入元素x
//vector::emplace_back(T x); 最后位置插入元素x
//vector::emplace(const_iterator position, T x);在position插入元素x,返回指向x元素位置的迭代器
//vector::erase(const_iterator position);删除position位置的元素
//vector::erase(const_iterator first, const_iterator last);删除[first,last)的元素
//vector::poop_back();删除最后一个元素
//vector::clear();删除所有元素
//vector::empty();true if the container size is 0, false otherwise.
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
vector<int> test(3, 100);
auto it = test.begin();
it = test.insert(it, 200); //在it指向位置前插入元素200
test.insert(it, 2, 300); //在it指向位置前插入2个300
cout << "test : ";
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\ninsert vector: ";
vector<int> a(3, 400);
it = test.end();
test.insert(it - 2, a.begin(), a.end());
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\ntest.at(5):" << test.at(5);
cout << "\ninsert array: ";
int array[] = { 5,6,7,8 };
test.insert(test.begin() + 2, array, array + 4);
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\ninsert 1024: ";
test.push_back(1024);
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\ninsert 2048: ";
test.emplace_back(2048);
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\nemplace operating.......\n";
auto it1 = test.emplace(test.begin() + 1, 1);//在第二个位置插入1,返回指向第二个位置的迭代器
test.emplace(it1, 2);//在第二个位置插入2,第二个元素后面的都往后移动一个位置,于是元素1自然变成第三个元素啦
test.emplace(test.end(), 3);//最后一个元素的后一个位置插入3
for (auto it2 : test)
{
cout << " " << it2;
}
cout << "\nlet's erase some elements.......\n";
cout << "erase [7,倒数第3个): \n";
test.erase(test.begin() + 6, test.end() - 3);//删除第7个元素到倒数第3个元素(倒数第3保留)
for (auto it2 : test)
{
cout << " " << it2;
}
cout << "\n erase the first element: \n";
test.erase(test.begin());//删除第1个元素
for (auto it2 : test)
{
cout << " " << it2;
}
cout << "\npop_back() : \n";
test.pop_back();
for (auto it2 : test)
{
cout << " " << it2;
}
cout << "\ndarkness falls %&*#@¥%……¥……\n";
test.clear();
if (test.empty())
cout << "empty container test\n";
else
cout << "not empty\n";
cout << "\n";
system("pause");
return 0;
}
5 容量大小
#include <iostream>
#include <vector>
using namespace std;
//vector::size() //返回元素的个数
//vector::resize(size_type n) //更改容器大小为n,如果n小于size(),则截取前n个元素即可
//vector::resize(size_type n,const value_type& val )
//vector::capacity() //容量大小,永远大于等于size()
//vector::maxsize() //容器可以容纳的最大元素个数
int main()
{
vector<int> test;
for (int i = 1; i<10; i++) test.push_back(i);
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\n";
test.resize(5);
cout << "size: " << test.size() << endl;
cout << "size = 5: ";
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
test.resize(8,100);
cout << "\nsize: " << test.size();
cout << "size = 8: ";
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
test.resize(12);
cout << "\nsize: " << test.size() << endl;
std::cout << "test contains:";
for (int i = 0; i<test.size(); i++)
std::cout << ' ' << test[i];
std::cout << '\n';
cout << "test.capacity: " << test.capacity() << " test.maxsize(): " << test.maxsize() << endl;
system("pause");
return 0;
}
1 2 3 4 5 6 7 8 9
size: 5
size = 5: 1 2 3 4 5
size: 8size = 8: 1 2 3 4 5 100 100 100
size: 12
test contains: 1 2 3 4 5 100 100 100 0 0 0 0
test.capacity: 13 test.maxsize(): 1073741823
请按任意键继续. . .
6 vector::asign()
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
vector<int> test;
vector<int> test1;
test.assign(8, 10);//5个int型数据10
cout << "test: ";
for (auto it : test)
{
cout << " " << it;
}
test1.assign(test.begin() + 1, test.begin() + 5);//分配从第二个到第六个共4个元素
cout << "\ntest1.size: " << test1.size();
cout << "\ntest1: ";
for (auto it : test1)
{
cout << " " << it;
}
cout << "\n";
system("pause");
return 0;
}
7 vector::data()
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
//vector::data()Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
//返回一个指向内存数组的直接指针,该内存数组由vector内部用于存储其所属元素。
int main()
{
vector<int> test(10);
int* p = test.data();
*p = 10;//第一个数据等于10
++p;
*p = 20; //第二个为20
p[3] = 100; //第5(2 + 3)个为100
p[6] = 1024;//第8(2 + 6)个位1024
++p;
*p = 2048;//第3个位2048
for (auto it : test)
{
cout << " " << it;
}
cout << "\n";
system("pause");
return 0;
}
8 vector::getallocator()
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
//vector::get_allocator() Returns a copy of the allocator object associated with the vector.
//返回与vector关联的分配器对象的副本。
int main()
{
vector<int> test;
int *p;
p = test.get_allocator().allocate(5);
for (auto i = 0; i < 5; i++)
{
test.get_allocator().construct(&p[i], i);
}
cout << "The allocated array contains:";
for (auto i = 0; i < 5; i++)
{
cout << ' ' << p[i];
}
cout << '\n';
// destroy and deallocate:
for (auto i = 0; i < 5; i++)
{
test.get_allocator().destroy(&p[i]);
}
test.get_allocator().deallocate(p, 5);
system("pause");
return 0;
}
9 operator=
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
//operator = 将新内容分配给容器,替换其当前内容,并相应地修改其大小
int main()
{
vector<int> test(3,1);//3个1
vector<int> test1(5,1);//5个1
cout << "test: ";
for (auto it : test)
{
cout << " " << it;
}
cout << "\ntest1: ";
for (auto it : test1)
{
cout << " " << it;
}
test1 = test; //test1变为3个1
test = vector<int>();//test变为空
cout << "\nafter operator = ..........\n";
cout << "test: ";
for (auto it : test)
{
cout << " " << it;
}
cout << "\ntest1: ";
for (auto it : test1)
{
cout << " " << it;
}
system("pause");
return 0;
}
10 &&operator[]
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
//operator[] 返回对向量容器中位置n处元素的引用。
int main()
{
vector<int> test(10);
for (auto i = 0; i < 10; i++)
{
test[i] = i;
}
cout << "original test:";
for (auto it : test)
{
cout << " " << it;
}
for (auto i = 0; i < 5; i++)
{
test[i] = test[9 - i];
}
cout << "\ntest:";
for (auto it : test)
{
cout << " " << it;
}
system("pause");
return 0;
}
11 vector::shrink_to_fit()
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
//vector::shrink_to_fit() requests the container to reduce its capacity to fit its size.
int main()
{
vector<int> test(10);
cout << "1.test's capacity; " << test.capacity() << endl;
test.resize(6);
cout << "2.test's capacity; " << test.capacity() << endl;
test.shrink_to_fit();
cout << "3.test's capacity; " << test.capacity() << endl;
system("pause");
return 0;
}
12 vector::swap()
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
//vector::swap(vector& x)
int main()
{
vector<int> test(5, 1);
vector<int> test1(8, 10);
cout << "test : ";
for (auto it : test)
{
cout << " " << it;
}
cout << "\ntest1 : ";
for (auto it : test1)
{
cout << " " << it;
}
test.swap(test1);
cout << "\nswap:............ \n";
cout << "test : ";
for (auto it : test)
{
cout << " " << it;
}
cout << "\ntest1 : ";
for (auto it : test1)
{
cout << " " << it;
}
cout << "\n";
system("pause");
return 0;
}