關於vector的一段自用程序--初級

由於最近在看STL,關於string的操作,不例舉了,今天把自學寫的vector放上來,大家一起學習。

#include "iostream"
#include "vector"
#include "string"
#include "algorithm"
using namespace std;


int main(void)
{
        string str[]={"C","C++","Linux"};
        string line;
        //create the vector
        vector<string> v1;
        vector<string> cpv1;
        vector<int> v2(10);
        vector<int> v3(10,0);
        vector<string> v4(str+0,str+3);
        //begin traversing the vector
        vector<string>::iterator sIt=v4.begin();//string iterator
        while(sIt!=v4.end())                    //read the v4
                cout<<*sIt++<<" ";
        cout<<endl;
        vector<string> v5(v4);                  //the v5 is copy v4
        for(int i=0;i<3;i++)
                cout<<v5[i]<<" ";
        cout<<endl;
        //
        vector<int>::iterator iIt=v3.begin();
        while(iIt!=v3.end())
                cout<<*iIt++<<" ";
        cout<<endl;
        while(getline(cin,line))
        {
                if(line.empty())
                break;
                v1.push_back(line);//add the line end in the v1
                //v1.push_front(line);  //add the line begin in the v1
                //v1.insert(p,line);    //add the line before p
                //v1.insert(p,n,line);  //add n line before p
        }
        sIt=v1.begin();//string iterator
        while(sIt!=v1.end())                    //read the v1
                cout<<*sIt++<<" ";
        cout<<endl;
        cpv1=v1;




        //use insert at v1
        sIt=v1.begin();
        string spo("newElement");
        v1.insert(v1.begin(),spo);      //must use the v1.begin(),don't use the sIt
        cout<<"insert begin:";
        while(sIt!=v1.end())
                cout<<*sIt++<<" ";
        cout<<endl;
        v1=cpv1;


        //
        sIt=v1.begin()+2;
        v1.insert(sIt,spo);             //here can use the v1.begin()=2 and sIt
        sIt=v1.begin();
        cout<<"insert begin+2:";
        while(sIt!=v1.end())
                cout<<*sIt++<<" ";
        cout<<endl;
        v1=cpv1;


        //
        sIt=v1.end()-1;                 //-1 is one before the end,but don't +1
        v1.insert(sIt,3,"COPY");
        sIt=v1.begin();                 //must again assign the sIt,becase the begin is change
        cout<<"insert 3 COPY before endl:";
        while(sIt!=v1.end())
                cout<<*sIt++<<" ";
        cout<<endl;
        v1=cpv1;


        //


        string sar[4]={"Test1","Test2","Test3","Test4"};
        v1.insert(v1.end(),sar,sar+4);
        sIt=v1.begin();
        cout<<"insert all of sar before end:";
        while(sIt!=v1.end())
                cout<<*sIt++<<" ";
        cout<<endl;
        v1=cpv1;


        //
        sIt=v1.begin();
        v1.insert(v1.begin(),sar,sar+2);
        cout<<"insert same of sar before end:";
        while(sIt!=v1.end())
                cout<<*sIt++<<" ";
        cout<<endl;
        v1=cpv1;
        //


        //using the const_iterator traversing read the vector
        //use the const_iterator because we won't change the elements
        vector<string>::const_iterator iread = v1.begin();
        while(iread!=v1.end())
                cout<<*iread++<<" ";
        cout<<endl;


        //use the clear
        v1.clear();
        iread=v1.begin();
        v1.push_back("Hello");
        while(iread!=v1.end())
                cout<<*iread++<<" ";
       //cout<<v1.size()<<endl;
        cout<<endl;
        return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章