Vector Demo

/*
 * vectorDemo.cpp
 *
 *  Created on: Jul 17, 2014
 *      Author: lichfeng
 */
#include<vector>
#include <iostream.h>
#include <algorithm>
#include"vectorDemo.H"

using namespace std;

void vectorDemo() {
	/**************************************************************************/
	/*                    constructors functions                              */
	/**************************************************************************/
	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
	// the iterator constructor can also be used to construct from arrays:
	int myints[] = { 16, 2, 77, 29, 8, 3, 24 };
	vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));

	cout << "The contents of fifth are:";
	for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';

	/**************************************************************************/
	/* iterator erase (iterator position);                                    */
	/* iterator erase (iterator first, iterator last);                        */
	/* erase(): 在vector中移除一个元素(通过position),或者移除一个区间([first, last))内的元素*/
	/*          该函数返回值是所删除的元素的下一个元素。                                    */
	/* Note: 由于vector是以数组形式组织的,所以删除中间元素将导致vector中其他元素向前移动。这对vector  */
	/*       而言是低效的操作,应减少使用。但此操作对list和deque等链表组织的容器很高效.              */
	/**************************************************************************/
	//vector<int>::iterator ii;
	fifth.erase(fifth.begin() + 1);
	cout << "After erase by position, the contents of fifth are:";
	for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';

	fifth.erase(fifth.begin(), fifth.begin() + 2);
	cout << "After erase by range, the contents of fifth are:";
	for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';

	/***************************************************************************/
	/* capacity(): 返回当前vector已分配的存储空间大小(按照所存元素类型),该值大于等于size()                          */
	/* size():     返回当前vector所实际存储的元素数目                                                                                                                             */
	/* max_size(): 该vector可以分配的最大内存空间(操作系统限制的)                                                                                             */
	/* push_back():在vector末尾增加一个元素,将导致size数加1。当且仅当size数大于当前容量时,                         */
	/*             引起vector内存重新分配(按内存非配器算法增加vector容量                                                                      */
	/* pop_back(): 移除vector的最后一个元素,引起size减少1                              */
	/* begin():    返回指向vector第一个元素的iterator                                */
	/* end():      返回指向vector最后一个元素的下一个元素的iterator, 不能取值。                                                     */
	/* cbegin():   返回指向vector第一个元素的const iterator,不允许修改vector数据                                   */
	/* cend():     返回指向vector最后一个元素的下一个元素的const iterator, 不能取值。                               */
	/* at():       返回at(position)中position的引用.                              */
	/* front():    返回vector第一個元素的引用.                                       */
	/* back():     返回vector中最后一个元素的引用.                                    */
	/* clear():    移除vector中所有的元素,将导致vector size为0.(capacity非强制变化).     */
	/**************************************************************************/
	vector<int> vIntTest;
	cout << "0:The size is " << vIntTest.size() << endl;
	cout << "0:The capacity is " << vIntTest.capacity() << endl;
	cout << "0:The max capacity is " << vIntTest.max_size() << endl;

	for (int i = 0; i < 10; i++)
		vIntTest.push_back(i);
	cout << "1:The size is " << vIntTest.size() << endl;
	cout << "1:The capacity is " << vIntTest.capacity() << endl;

	vIntTest.pop_back();
	cout << "2:The size is " << vIntTest.size() << endl;
	cout << "2:The capacity is " << vIntTest.capacity() << endl;

	vIntTest.insert(vIntTest.end(), 10, 100);
	cout << "3:The size is " << vIntTest.size() << endl;
	cout << "3:The capacity is " << vIntTest.capacity() << endl;

	vIntTest.at(0) = 100;
	vIntTest.at(1) = 200;
	cout << "After set by at():";
	for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';

	vIntTest.back() = 1000;
	cout << "After set by back():";
	for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';
	/***************************************************************************/
	/* rbegin():   返回指向vector最后一个元素的迭代器,对翻转迭代器加操作将导致其向前移动.            */
	/* empty():    判斷vector是否為空并返回,空返回true,非空返回false                      */
	/* resize():   調整容器的size以包含n個元素,移除多餘的元素或增大內存空間                                                                            */
	/* reserve():  要求vector capacity至少包含n個元素,如果n大于當前capacity,vector容量將增加  */
	/***************************************************************************/
	vector<int> reserveVec(5);
	vector<int>::reverse_iterator ri;

	int i=0;
	for(ri=reserveVec.rbegin(); ri!=reserveVec.rend(); ri++)
	{
	    *ri = ++i;
	}

	cout<<"reserveVec contains: ";
	for(vector<int>::iterator it= reserveVec.begin(); it != reserveVec.end(); it++)
		cout<<"*it ";
	cout<<endl;

	/***************************************************************************/
	/* data():     返回vector首元素的内部C类型指针,通过该指针完成所有的操作.                    */
	/* empty():    判斷vector是否為空并返回,空返回true,非空返回false                      */
	/* resize():   調整容器的size以包含n個元素,移除多餘的元素或增大內存空間                                                                            */
	/* reserve():  要求vector capacity至少包含n個元素,如果n大于當前capacity,vector容量將增加  */
	/***************************************************************************/

	//data()
    /*vector<int> myvector(5);

	int* p = myvector.data();

	*p = 10;
	++p;
	*p = 20;
	p[2] = 100;

	cout << "myvector contains:";
	for (unsigned i = 0; i < myvector.size(); ++i)
		cout << ' ' << myvector[i];
	cout << '\n';*/

	// reserve()
	vector<int>::size_type sz;

	vector<int> foo;
	sz = foo.capacity();
	std::cout << "making foo grow:\n";
	for (int i = 0; i < 100; ++i) {
		foo.push_back(i);
		if (sz != foo.capacity()) {
			sz = foo.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}

	vector<int> bar;
	sz = bar.capacity();
	bar.reserve(100);   // this is the only difference with foo above
	cout << "making bar grow:\n";
	for (int i = 0; i < 100; ++i) {
		bar.push_back(i);
		if (sz != bar.capacity()) {
			sz = bar.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}

	//resize
	vector<int> myvector1;

	// set some initial content:
	for (int i = 1; i < 10; i++)
		myvector1.push_back(i);

	myvector1.resize(5);
	myvector1.resize(8, 100);
	myvector1.resize(12);

	cout << "myvector contains:";
	for (int i = 0; i < myvector1.size(); i++)
		cout << ' ' << myvector1[i];
	cout << '\n';
	//myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0

}
posted on 2014-07-18 17:48  Stephen_init  阅读(302)  评论(0编辑  收藏  举报