C++ vector,map,queue,stack,list,forward_list,array

1.The common function

#include <iostream>
#include <uuid/uuid.h>
#include <ctime>
#include <unistd.h>
#include <string.h>
#include <ostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <list>
#include <forward_list>
#include <stack>
#include <array>
using namespace std; static char *dtValue=(char*)malloc(20); static char *uuidValue=(char*)malloc(40); static int num=0; char *getTimeNow() { time_t rawTime=time(NULL); struct tm tmInfo=*localtime(&rawTime); strftime(dtValue,20,"%Y%m%d%H%M%S",&tmInfo); return dtValue; } char *getUuid3() { uuid_t newUUID; uuid_generate(newUUID); uuid_unparse(newUUID,uuidValue); return uuidValue; }

struct BookStruct
{
int BookId;
string BookName;
string BookTitle;
};

 

2.vector

void vector7()
{
    vector<string> vec;
    for(int i=0;i<100;i++)
    {
        for(int j=0;j<10000;j++)
        {
            vec.push_back(getUuid3());
        }
        cout<<"I="<<i<<",and now is "<<getTimeNow()<<endl;
    }

    vector<string>::iterator itr=vec.begin();
    while(itr!=vec.end())
    {
        cout<<*itr<<endl;
        itr++;
    }
    cout<<"Finished in vector7() and now is "<<getTimeNow()<<endl;
}

3.map

void map8()
{
    map<int,string> mp;
    for(int i=0;i<100;i++)
    {
        for(int j=0;j<10000;j++)
        {
            mp.insert(pair<int,string>(++num,getUuid3()));
        }
    }

    map<int,string>::iterator itr=mp.begin();
    while(itr!=mp.end())
    {
        cout<<"Id="<<itr->first<<",value="<<itr->second<<endl;
        itr++;
    }
    cout<<"Finished in map8() and now is "<<getTimeNow()<<endl;
    free(uuidValue);
    free(dtValue);
}

4.queue

void queue11()
{
    int len=10;
    queue<string> qe;
    for(int i=0;i<len;i++)
    {
        string str=getUuid3();
        qe.push(str);
        cout<<"In push I="<<i<<",value="<<str<<endl;
    }

    for(int i=0;i<len;i++)
    {
        cout<<"In pop i="<<i<<",value="<<qe.front()<<endl;
        qe.pop();
    }
    cout<<"In queue11() and now is "<<getTimeNow()<<endl;
    free(uuidValue);
    free(dtValue);
}

5.list

void list13()
{
    list<struct BookStruct> lst;
    int len=10;
    srand(time(nullptr));
    struct BookStruct bs;
    for(int i=0;i<len;i++)
    { 
        bs.BookId=rand()%1000000000;
        bs.BookName=getUuid3();
        bs.BookTitle=getUuid3();
        lst.push_back(bs);
    }

    for(int i=0;i<len;i++)
    {        
        bs=lst.front();
        cout<<"Index="<<i<<",Id="<<bs.BookId<<",Name="<<bs.BookName<<",Title="<<bs.BookTitle<<endl;
        lst.pop_front();
    }
    cout<<"Finished in list13() and now is "<<getTimeNow()<<endl;
    free(uuidValue);
    free(dtValue);
}

6.forward_list

void forwardList14()
{
    forward_list<string> fList;
    int len=10;
    int i=0;
    for(i=0;i<len;i++)
    {
        string str=getUuid3();
        fList.push_front(str); 
        cout<<"In forward_list  push_front i="<<i<<",value="<<str<<endl;
    }

    for(i=0;i<len;i++)
    {
        cout<<"In forward_list pop_front,Index="<<i<<",value="<<fList.front()<<endl;
        fList.pop_front();
    }
    cout<<"Finished in forwardList14() and now is "<<getTimeNow()<<endl;
    free(uuidValue);
    free(dtValue);
}

7.stack

void stack15()
{
    stack<struct BookStruct> stk;
    int len=10;
    int i;
    struct BookStruct bs;
    srand(time(nullptr));
    for(i=0;i<len;i++)
    {
        bs.BookId=rand()%1000000000;
        bs.BookName=getUuid3();
        bs.BookTitle=getUuid3();
        stk.push(bs);
        cout<<"In push index="<<i<<",Id="<<bs.BookId<<",Name="<<bs.BookName<<",Title="<<bs.BookTitle<<endl;
    }

    for(i=0;i<len;i++)
    {
        bs=stk.top();
        cout<<"In top Index="<<i<<",Id="<<bs.BookId<<",Name="<<bs.BookName<<",Title="<<bs.BookTitle<<endl;
        stk.pop();
    }
    cout<<"Finished in stack15() and now is "<<getTimeNow()<<endl;
}

9.Array

void array16()
{
    size_t len=10;
    array<string,10> ary;
    int i=0;
    for(i=0;i<len;i++)
    {
        string str=getUuid3();
        ary[i]=str;
        cout<<"Fill,Index="<<i<<",value="<<str<<endl;
    }

    array<string,10>::iterator itr=ary.begin();
    while(itr!=ary.end())
    {
        cout<<"Print Index="<<i<<",value="<<*itr<<endl;
        itr++; 
    }
    cout<<"Finished in array16() and now is "<<getTimeNow()<<endl;
    free(uuidValue);
    free(dtValue);
}
原文地址:https://www.cnblogs.com/Fred1987/p/15731960.html