vector

//vector::assign 构造vector
std::vector<int> v1;
std::vector<int> v2;
v2.assign(v1.begin(),v1.end());//复制构造v1


//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';
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小竹浮生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值