C++学习之vector容器笔记

时间:2022-05-22 04:18:19
#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
class Student
{
public :
    Student(){cout << "Student() is dead..." <<endl;}
    ~Student(){cout << "~Student() is here..." << endl;}
    Sutdent(const Student &stu)
    {
        m_strName = stu.m_strName;
        m_fScore = stu.m_fScore;
        cout << "Student(const Student &) is here... " << endl;
    }
    Student(string name,float score)
    {
        m_strName = name;
        m_fScore = score;
        cout << "Student(aguments)..." <<endl;
    }
/**
    friend void print(const vector<Student>&vec)
    {
        vector<Student>::const_iterator iter;
        iter = vec.begin();
        for(;iter!=vec.end();iter++)
        {
            cout << *iter->stu.m_stuName << endl;
        }
    }
**/
private:
    string m_strName;
    float m_fScore;
 
};


//typedef vector<Student> VECSTU;
typedef vector<Student*> VECSTU;
 
/**自定义释放函数**/
void freeVec(VECSTU &vecStu)
{
    VECSTU::iterator iter = vecStu.begin();
    for (; iter != vecStu.end(); iter++)
    {
        delete (*iter);
    }
}
 
void print(const vector<int> &vec)
{
    vector<int>::const_iterator iter = vec.begin();//迭代器
 
    for(;iter !=vec.end();iter++)
    {
        cout << *iter <<' ';
    }
    cout << endl;
}
 
typedef vector<int> VECINT;
 
 
 
int main(void)
{
#if 0
    vector<int> vec_int;
 
    for(int i =0;i<10;i++)
    {
         //push_back:将数据放到容器里的最后
        vec_int.push_back(i+10);
        //capacity:获得容器实际存储容量的大小
        cout << vec_int.capacity() << endl;
    }
 
    //size:获得容器元素的个数
    for(int i=0;i<vec_int.size();i++)
    {
        //当容器中存在存储空间才可以使用下标访问数据
        //注意不要越界,没有做越界检查
        //cout<<vec_int[i]<<' ' << endl;
        cout << vec_int.at(i) << ' ';
    }
    cout << endl;
 
    /**
    verctor<int>是声明向量容器;
    例如 verctor<int> v,就是创建了一个名字叫v的向量容器。
    vector<int>::iterator是定义向量迭代器
    例如,vector<int>::iterator it 就可以
    for(it=v.begin();it!=v.end();it++)
    cout<<*it<<endl;
    就把里面的内容都输出了
    **/
    vector<int>::iterator iter;
 
    iter = vec_int.begin();
 
    for(;iter!=vec_int.end();iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
#endif
 
#if 0
    int iaData[6] = {11,32,15,17,26,19};
    //vector<int> vec(iaData,iaData + 6);//从数组的第一个值到数组第六个值
    vector<int> vec(10,999);//插入10个数,每个都是999
 
    for(int i = 0;i < vec.size();i++)
    {
        cout << vec[i] << ' ';
    }
   cout << endl;
 
   cout << vec.capacity() << endl;
 
   for(int i =0;i<vec.size();i++)
   {
    cout << vec[i] << ' ';
   }
   cout << endl;
 
   cout << "11111111111111111111111111111111111111" <<endl;
 
   vec.assign(2,888);
   cout << vec.capacity() << endl;
 
 
   for(int i = 0;i<vec.size();i++)
   {
    cout << vec[i] << ' ';
    }
   cout << endl;
 
   cout << "22222222222222222222222222222222222222" << endl;
 
   cout << vec.max_size() << endl;
 
   if(vec.empty())
   {
    cout << "vector is empty" << endl;
   }else
   {
   cout << "vec id not empty"<<endl;
   }
 
   vec.reserve(1024);
   cout << vec.capacity() <<endl;
 
   for(int i = 0;i<vec.size();i++)
   {
    cout << vec[i] <<' ';
   }
   cout << endl;
   vec.push_back(111);
 
   printf("3333333333333333333333333333333333\n");
   cout << vec.back() << ' ' << vec.front() <<endl;
#endif
 
 
#if 0
   //vector<int> vec_int;
    VECINT vec_int;
 
    for(int i =0;i < 10;i++)
    {
        vec_int.push_back(i+10);
    }
 
    vector<int>::iterator iter;
    iter = vec_int.begin();
 
    cout << *iter << endl;
 
    for(;iter!=vec_int.end();iter++)
    {
       cout << *iter << ' ';
    }
    cout << endl;
 
    cout << "first function test like above" <<endl;
 
    iter = vec_int.begin();
    for(;iter!=vec_int.end();iter++)
    {
        *iter +=10;
    }
 
    iter = vec_int.begin();
    for(;iter !=vec_int.end();iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
 
    cout << "second function test like above" << endl;
 
    vector<int>::const_iterator const_iter;
    const_iter = vec_int.begin();
    cout << *const_iter << endl;
    //*const_iter = 666;/error
 
    for(;const_iter != vec_int.end();const_iter++)
    {
        cout << *const_iter << ' ';
    }
    cout << endl;
 
    cout << "third function test this above" << endl;
 
    iter = vec_int.begin();
    vec_int.insert(iter,698);
    iter = vec_int.begin();
    cout << *iter << endl;
 
    cout << "fouth function test this above" <<endl;
 
 
    iter++;
    iter++;
    vec_int.insert(iter,3,899);//插入三个数
    iter = vec_int.begin();
    for(;iter != vec_int.end();iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
    cout << "ffith function test this above" <<endl;
 
    int iaData[] = {1,2,3,4,5,6,7,8,9};
    iter = vec_int.begin();
    iter++;
    vec_int.insert(iter,iaData,iaData + 8);//插入数组
    iter = vec_int.begin();
 
    for(;iter != vec_int.end();iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
 
    cout << "sixth function test is above" <<endl;
 
    cout << vec_int.capacity() <<endl;
    iter = vec_int.begin();
 
   // iter = vec_int.begin();
   /** for(;iter != vec_int.end();iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
**/
    //iter = vec_int.end();
 
    vec_int.insert(iter,2,88);
    for(;iter !=vec_int.end();iter++)
    {
        if(0 != *iter %2)
        {
            //增删数据后,所有的迭代器均失效,需要重新赋值
            //erase:返回一个迭代器,
            //该迭代器指向下一个元素或者end()
             //只是将元素删除,并不会对存储空间产生影响
            //erase()函数的功能是用来删除容器中的元素。
            /**
函数原型:
iterator erase(iterator where);
iterator erase(iterator first,iterator last);
basic_string& erase(size_type p0=0,size_type n=np);
删除某个容器里的某个元素:c.erase(T);
链表容器以list为例,执行container.erase(it)。
list<int>::iterator it;
for (it = lt.begin(); it != lt.end(); ) {
if (*it % 2 == 0)
lt.erase(it++);
else
++it;
}
            **/
             iter = vec_int.erase(iter);
        }else
        {
            iter++;
        }
    }
 
    cout << vec_int.capacity() <<endl;
    iter = vec_int.begin();
    for(;iter != vec_int.end();iter++)
    {
        cout << *iter << ' ';
    }
    cout << endl;
    print(vec_int);
 
    cout << "this test is below"<< endl;
        VECINT::iterator iter1 = vec_int.begin();
        VECINT::iterator iter2 = vec_int.begin();
        iter2++;
        iter2++;
        vec_int.erase(iter1, iter2); //[iter1, iter2);
        print(vec_int);
 
#endif
#if 0
    VECSTU stuVec;
    Student stu("zhangsan",99);
 
    for(int i=0;i<5;i++)
    {
        stuVec.push_back(stu);
    }
#endif
 
 
    VECSTU stuVec;
 
        Student *pStu = NULL;
 
        cout << "----------------------------" << endl;
 
        for (int i = 0; i < 5; i++)
        {
            pStu = new Student("zhangsan", i + 100);
            stuVec.push_back(pStu);
        }
 
        freeVec(stuVec);
 


    // cout << stuVec.at(0).m_strName <<endl;
 
 
    cout << "Hello World!" << endl;
    return 0;
}