C++标准库Vector & Iterator用法

http://blog.163.com/hbu_lijian/blog/static/12612915320120511849791/

//程序1
#include <iostream>
#include <vector>

//using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;

//vector's demo class
class VectorDemo
{
private:
    int num;
    vector<int> ivec;
    vector<string> svec;

public:
    VectorDemo()
    {

    }
    VectorDemo(int temp)
    {
        num = temp;
    }

    void VectorEssentialUse(void);
    void IteratorEssentialUse(void);
};

void VectorDemo::VectorEssentialUse()
{
    int i = 0;

    for(i = 0; i < 3; i++)
    {
        ivec.push_back(i+1);
    }

    for(i = 0; i < 3; i++)
    {
        cout << ivec[i] << endl;
    }

    cout << "ivec_length = " << ivec.size() << endl;
    ivec.clear();
    cout << "ivec_length = " << ivec.size() << endl;
    cout << "svec_length = " << svec.size() << endl;
    for(i = 0; i < 3; i++)
    {
        svec.push_back("aaa");
    }
    cout << "svec_length = " << svec.size() << endl;
    for(i = 0; i < 3; i++)
    {
        cout << svec[i] << endl;
        svec.pop_back();
        cout << "svec_length = " << svec.size() << endl;
    }

    vector<int> iv1(10);
    vector<int> iv2(10,100);
    vector<int> iv3(iv2);
    for(i = 0; i < 10; i++)
    {
        iv1[i] = i;
        cout << iv1[i] << "          " << iv2[i] << "          " << iv3[i] << endl;
    }

    return;
}

void VectorDemo::IteratorEssentialUse()
{
    int i = 0;

    for(i = 0; i < 3; i++)
    {
        ivec.push_back(i+1);
    }

    for(vector<int>::size_type i = 0; i != ivec.size(); i++)//essential loop
    {
        cout << ivec[i] << endl;
    }

    for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++)//iterator loop
    {
        cout << *iter << endl;
        *iter = 100000000;//iterator can change the value.
        cout << *iter << endl;
    }

    return;
}

int main(int argc, char *argv[])
{
    VectorDemo *p = new VectorDemo();
    p->IteratorEssentialUse();
    cout << "Application Run Successfully!" << endl;
    return 0;
}

//程序2
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::cin;
using std::cerr;
using std::string;
using std::vector;

int main(int argc, char *argv[])
{
    vector<int> myvector(3, 100);
    vector<int>::iterator it;

    for(it = myvector.begin(); it != myvector.end(); it++)
    {
        cout << *it << endl;
    }
    cout << endl;

    it = myvector.begin();
    it = myvector.insert(it, 200);

    for(it = myvector.begin(); it != myvector.end(); it++)
    {
        cout << *it << endl;
    }
    cout << endl;

    myvector.insert(it, 2, 300);

    for(it = myvector.begin(); it != myvector.end(); it++)
    {
        cout << *it << endl;
    }

    // "it" no longer valid, get a new one:
    it = myvector.begin();

    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);

    cout << "myvector contains:";
    for (it = myvector.begin(); it < myvector.end(); it++)
        cout << " " << *it;
    cout << endl;

    cout << "Application Run Successfully!" << endl;
    return 0;
}

小结:
1.#include <vector> 并且 using std::vector;
2.所有的标准库容器类都定义了相应的iterator类型
3.定义一个vector
vector <int> vect;
vector <int> vect1(12);//12个int类型元素,每个元素的初始值均为0
vector <int> vect2(12,9);//12个int,初试值均为9
4.定义迭代器iterator
vector <int>::iterator iter=vt.begin();//起始地址
vector <int>::iterator iter_end=vt.end();//结束地址,两个地址都是指针类型
5.vector的一些基本方法(size(),insert(),push_back(),pop_back(),begin(),end())
6.const_iterator,只能用于读取容器内元素值,不能用于改变容器内元素值
7.迭代器基本操作:iter++,++iter,iter- -,--iter,iter+n,iter,-n,iter1-iter2
8.vector对象可用数组进行初始化
const size_t arr_size = 6;
int int_arr[arr_size] = {0,1,2,3,4,5};
//ivec has 6 elements:each a copy of the corresponding element in int_arr
vector<int> ivec(int_arr,int_arr + arr_size);
or
vector<int> ivec(int_arr + 1, int_arr +4);//copy 3 elements:int_arr[1],int_arr[2],int_arr[3]

你可能感兴趣的:(C++标准库Vector & Iterator用法)