//vector::at
访问vector中的数据
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
int num = vec.at(vec.size() - 1);
num = 5;
//vector::back访问vector中最后一个元素
auto back = vec.back();//back = 5;
//vector::begin获取vector开始元素的迭代器(iterator)
auto begin = vec.begin();
//vector::capacity获取vector中的capacity
auto capacity = vec.capacity();
//vector::cbegin从开始获取const_iterator
auto cbegin = vec.cbegin();
//vector::cend
//Return const_iterator to end
//Returns a const_iterator pointing to the past-the-end element in the container.
//A const_iterator is an iterator that points to const content. This iterator can be increased and decreased (unless it is itself also const), just like the iterator returned by vector::end,
but it cannot be used to modify the contents it points to, even if the vector object is not itself const.
//If the container is empty, this function returns the same as vector::cbegin.
//The value returned shall not be dereferenced.
auto cend = vec.cend();
// vector::clear
//Clear content
//Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
vec.clear();
//vector::crbegin( 反向迭代 )
//Return const_reverse_iterator to reverse beginning
//Returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning).
vec.crbegin();
//vector::crend( 反向迭代 )
//Return const_reverse_iterator to reverse end
//Returns a const_reverse_iterator pointing to the theoretical element preceding the first element in the container (which is considered its reverse end).
vec.crend();
//vector::data
//Access data
//Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
auto *p = vec.data();
*p = 10;
p++;*p = 20;p[1] = 100;
//vector::emplace
//Construct and insert element
//The container is extended by inserting a new element at position. This new element is constructed in place using args as the arguments for its construction.
auto it = vec.emplace ( vec.begin(), 100 );
vec.emplace ( it, 200 );
vec.emplace ( vec.end(), 300 );
//vertor::emplace_back
//Construct and insert element at the end
//Inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor.
vec.emplace_back(100);
vec.emplace_back(1000);
//vector::empty
//Test whether vector is empty
//Returns whether the vector is empty (i.e. whether its size is 0).
while (!vec.empty())
{
std::cout<<("vector is not empty")<<std::endl;
break;
}
//vector::end
//Test whether vector is empty
//Returns whether the vector is empty (i.e. whether its size is 0).
vec.end();
//vector::erase
//Erase elements
//Removes from the vector either a single element (position) or a range of elements ([first,last)).
//This effectively reduces the container size by the number of elements removed, which are destroyed.
vec.erase(vec.begin()+1);
vec.erase(vec.begin(),vec.begin()+3);
//vector::front
//Access first element
//Returns a reference to the first element in the vector.
//Unlike member vector::begin, which returns an iterator to this same element, this function returns a direct reference.
//Calling this function on an empty container causes undefined behavior.
vec.front();
//vector::get_allocator
//Get allocator
//Returns a copy of the allocator object associated with the vector.
std::vector<int> myvector;
int * p;
unsigned int i;
// allocate an array with space for 5 elements using vector‘s allocator:
p = myvector.get_allocator().allocate(5);
// construct values in-place on the array:
for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],i);
std::cout << "The allocated array contains:";
for (i=0; i<5; i++) std::cout << ‘ ‘ << p[i];
std::cout << ‘\n‘;
// destroy and deallocate:
for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
myvector.get_allocator().deallocate(p,5);
//vector::insert
//Insert elements
//The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.
std::vector<int> myvector (3,100);
std::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();
std::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);
std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ‘ ‘ << *it;
std::cout << ‘\n‘;
//vector::max_size
//Return maximum size
//Returns the maximum number of elements that the vector can hold.
std::vector<int> myvector;
// set some content in the vector:
for (int i=0; i<100; i++) myvector.push_back(i);
std::cout << "size: " << myvector.size() << "\n";
std::cout << "capacity: " << myvector.capacity() << "\n";
std::cout << "max_size: " << myvector.max_size() << "\n";
//vector::operator=
//Assign new contents to the container, replacing its current contents, and modifying its size accordingly.
std::vector<int> v1(3,0);
std::vector<int> v2(5.0);
v2 = v1;
std::cout<<v2.size()<<‘\n‘;