关于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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值