C++ vector generated via rand() and print with fixed width

#include <iostream>
#include <uuid/uuid.h>
#include <sstream>
#include <ctime>
#include <unistd.h>
#include <ostream>
#include <fstream>
#include <istream>
#include <random>
#include <vector>

using namespace std;

static char *dtValue = (char *)malloc(20);
static char *uuidValue = (char *)malloc(40);
char *getUuid3()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
    return uuidValue;
} 

char *getTimeNow()
{
    time_t rawTime = time(NULL);
    struct tm tmInfo = *localtime(&rawTime);
    strftime(dtValue, 20, "%Y%m%d%H%M%S", &tmInfo);
    return dtValue;
}


void vector8();

int main()
{
    vector8();
    return 0;
}

void vector8()
{
    srand(time(NULL));
    int len = 100;
    vector<int> vec;
    for (int i = 0; i < len; i++)
    {
        vec.push_back(rand());
    }

    cout << "Original order:" << endl;
    vector<int>::iterator itr = vec.begin();
    int colWidth = 0;
    while (itr != vec.end())
    {
        if (++colWidth >= 8)
        {
            colWidth = 0;
            cout << endl;
        }
        printf("%-10dt", *itr++); 
    }

    for (int i = 0; i < len; i++)
    {
        for (int j = i + 1; j < len; j++)
        {
            if (vec[i] > vec[j])
            {
                int temp = vec[i];
                vec[i] = vec[j];
                vec[j] = temp;
            }
        }
    }

    cout << "nnSort ascendingly:" << endl;
    vector<int>::iterator itr2 = vec.begin();
    colWidth = 0;
    while (itr2 != vec.end())
    {
        if (++colWidth >= 8)
        {
            colWidth = 0;
            cout << endl;
        }
        printf("%-10dt", *itr2++); 
    }
    cout << "nnNow finished in vector8() and now is " << getTimeNow() << endl;
    free(uuidValue);
    free(dtValue);
}

Compile

g++ -g -std=c++2a -I. h1.cpp -o h1 -luuid

C++ vector generated via rand() and print with fixed width

 

 

#include <iostream>#include <uuid/uuid.h>#include <sstream>#include <ctime>#include <unistd.h>#include <ostream>#include <fstream>#include <istream>#include <random>#include <vector>

原文链接: https://www.cnblogs.com/Fred1987/p/15739917.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++ vector generated via rand() and print with fixed width

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401657

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:20
下一篇 2023年4月19日 上午9:20

相关推荐