C++ vector 功能简单介绍

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	/*
	   class vector index 0 1 2... 索引从0 1 2...
	   member type
	      value_type
		  allocator_type
		  reference
		  const_reference
		  pointer
		  const_pointer
		  iterator
		  const_iterator
		  reverse_iterator
		  const_reverse_iterator
		  difference_type
		  size_type

	  Allocator:
	      get_allocator
	  Non_member function overloads
	      relational operators
		  swap
	*/

	/* 初始化 
	Member functions:
	(constructor)
	(destructor)
	operator=
	  1. empty container constructor(default constructor)
	  2. fill constructor
	  3. range constructor
	  4. copy constructor(and copying with allocator)
	  5. initializer list constructor
	*/
	//constructors used in the same order as described above
	vector<int> first;
	vector<int> second(4, 100);
	vector<int> third(second.begin(), second.end());
	vector<int> fourth(third);

	// the iterator constructor can also be used to construct from arrays;
	int myints[] = { 16, 2, 77,29 };
	vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
	vector<int> sixth(myints, myints + 4);
	
	/*
		Iterator:
		   begin
		   end
		   rbegin
		   rend
		   cbegin
		   cend
		   crbegin
		   crend
	*/
	// begin end
	for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
		cout << ' ' << *it;
	cout << endl;
	// rbegin rend 反向迭代器
	for (vector<int>::reverse_iterator rit = fifth.rbegin(); rit != fifth.rend(); ++rit)
		cout << ' ' << *rit;
	cout << endl;
	// crbegin crend
	for (auto rit = fifth.rbegin(); rit != fifth.rend(); ++rit)
		cout << ' ' << *rit;
	/*
	  Capacity:
	    size     
	    max_size
	    resize
	    capacity
	    empty
	    reserve
	    shrink_to_fit
	*/
	vector<int> v;
	//std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;
	v.reserve(10);
	//std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;
	v.resize(10);
	v.push_back(0);
	//std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;

	/*
		Element access:
		     operator[]
		     at
		     front
		     back
		     data   返回 一个指向第一个元素的指针
	*/
	vector<int> vec;
	for (int i = 0; i < 10; i++)
		vec.push_back(i);
	int *pi = vec.data();
	//cout << vec.at(0) << ' ' << vec[0] << ' ' << vec.front() << ' ' << vec.back() << ' ' << *pi << ' ' << pi[2] << endl;
	
	/*
	   Modifiers:
	      assign      赋值
	      push_back   add the element at the end
	      pop_back    delete the last element
	      insert      插入一个值或者一个数组或者一个vector
	      erase       删除某一个位置上的 或者 一段
	      swap
	      clear
	      emplace
	      emplace_back
	*/
	// vec 0 1 2 3 4 5 6 7 8 9
	vec.pop_back(); // 0 1 2 3 4 5 6 7 8 
	for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
		cout << ' ' << *it;    

	vector<int>::iterator it;
	it = vec.begin();
	it = vec.insert(it, 200); //200 0 1 2 3 4 5 6 7 8 

	vec.insert(it, 2, 300);   // 300 300 200 0 1 2 3 4 5 6 7 8
	
	// " it " no longer valid, get a new one:
	int arr[] = { 501, 502, 503 };
	vec.insert(vec.begin(), arr, arr + 3);  // 501 502 503 300 300 200 0 1 2 3 4 5 6 7 8

	vector<int> myvec = { 10, 20, 30 };

	auto it0 = myvec.emplace(myvec.begin() + 1, 100); //10 100 20 30
	myvec.emplace(it0, 200);  // 10 200 100 20 30
	for (auto& x : myvec)  // 厉害的语法
		cout << ' ' << x;

	vec.swap(myvec);
	for (auto& x : myvec)  // 厉害的语法
		cout << ' ' << x;

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值