DEV Community

海前 王
海前 王

Posted on

stl

//#include <iostream> 
//#include <vector> 
//#include <stdint.h> 
//#include <ctime> 
//
//int main()
//{
//    const uint32_t loop = 100000000000000;
//    std::vector<int32_t> vec;
//    clock_t timeStart = 0;
//    for (uint32_t i = 0; i < loop; ++i)
//    {
//        vec.push_back(i);
//    }
//    // test time use 
//    // 1.by index 
//    timeStart = clock();
//    uint64_t sum1 = 0;
//    for (uint32_t i = 0; i < vec.size(); ++i)
//    {
//        sum1 += vec[i];
//    }
//    std::cout << sum1 << " " << clock() - timeStart << "clock" << std::endl;
//
//    // 2.by iterator 
//    timeStart = clock();
//    uint64_t sum2 = 0;
//    for (std::vector<int32_t>::const_iterator it = vec.begin(); it != vec.end(); ++it)
//    {
//        sum2 += *it;
//    }
//    std::cout << sum2 << " " << clock() - timeStart << "clock" << std::endl;
//    // 3.by auto iterator 
//    uint64_t sum3 = 0;
//    timeStart = clock();
//    for (auto it = vec.begin(); it != vec.end(); ++it)
//    {
//        sum3 += *it;
//    }
//    std::cout << sum3 << " " << clock() - timeStart << "clock" << std::endl;
//
//    // 4.by for range 
//    uint64_t sum4 = 0;
//    timeStart = clock();
//    for (const auto& it : vec)
//    {
//        sum4 += it;
//    }
//    std::cout << sum4 << " " << clock() - timeStart << "clock" << std::endl;
//
//    return 0;
//}
//
#include<iostream>

//import std;
import <vector>;
int main()
{
    std::cout << "Import the STL library for best performance\n";
    std::vector<int> v{ 5, 5, 5 };

}

//#include <vector>
//#include <iostream>
//using namespace std;
//int main()
//{
//  vector<int> v{ 1,2,3,4 };
//  v.clear();
//  for(auto i:v)
//  {
//      cout << i;
//  }
//  cout << v.size()<<endl;
//  cout << v.capacity()<<endl;
//  v.shrink_to_fit();
//  cout << v.capacity();
//}
//
//#include <map>
//#include <iostream>
//#include <string>
//using namespace std;
//int main()
//{
//  map<string, int> a;
//
//  a.insert(make_pair("jack", 18));
//  a.insert(make_pair("tom", 38));
//  a.insert(make_pair("chen", 28));
//  a.insert(make_pair("fen", 13));
//
//
//  auto it = a.begin();
//  cout << (*it).first;
//  cout << a.at("fen");
//  a.erase(a.begin());
//  a.insert(make_pair("admin", 8));
//  cout << (*it).first;
//
//
//}
#include <iostream>
#include <vector>
using namespace std;

void fun(vector<int>::iterator it)
{
    cout << *it;
}

int main()
{
    vector<int> a {1,2,3,4,5,6,7};
    vector<int>::iterator it = a.begin();
    it++;
    it += 1;
    vector<int>::iterator it1 = it;
    it1++;
    cout << *it1;
    fun(it1);
    cout<<it - a.begin();
    cout << distance(a.begin(), it);

}

#include <iostream>  
#include <vector>  
#include <list>  
#include <ctime>  

using namespace std;

class Message
{
};

int main()
{
    vector<Message*> vt;
    list<Message*> lt;

    Message* msg = new Message();
    time_t start = time(NULL);
    for (int i = 0; i < 10000; ++i) {
        vt.clear();
        for (int j = 0; j < 100000; ++j) {
            vt.push_back(msg);
        }
    }

    time_t end = time(NULL);
    cout << "vector spend time " << end - start << endl;

    start = time(NULL);
    for (int i = 0; i < 10000; ++i) {
        lt.clear();
        for (int j = 0; j < 100000; ++j) {
            lt.push_back(msg);
        }
    }

    end = time(NULL);
    cout << "list spend time " << end - start << endl;
    delete msg;
    msg = NULL;

    start = time(NULL);
    for (int i = 0; i < 10000; ++i) {
        typeof(vt.begin()) it = vt.begin();
        while (it != vt.end()) {
            msg = *it;
            ++it;
        }
    }

    end = time(NULL);
    cout << "vector spend time " << end - start << endl;

    start = time(NULL);
    for (int i = 0; i < 10000; ++i) {
        typeof(lt.begin()) it = lt.begin();
        while (it != lt.end()) {
            msg = *it;
            ++it;
        }
    }

    end = time(NULL);
    cout << "list spend time " << end - start << endl;

    return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <afxtempl.h>

int main()
{
    std::vector<std::string> linesVector;

    std::ifstream file("D:\\使用说明.txt");
    if (file.is_open())
    {
        std::string line;
        while (std::getline(file, line))
        {
            linesVector.push_back(line);
        }
        file.close();
    }

    CArray<CString> linesArray;
    for (const auto& line : linesVector)
    {
        linesArray.Add(CString(line.c_str()));
    }

    // 访问CArray对象中的行内容
    for (int i = 0; i < linesArray.GetSize(); i++)
    {
        CString line = linesArray.GetAt(i);
        std::string li_string = (std::string)li_string;
        OutputDebugString(line);
    }

    return 0;
}

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

using namespace std;
#define MAX_L 16 //最大层数  
#define prob 0.5 //有i+1级指针的结点占有i级指针的结点的比例

//结点结构
typedef struct node
{
    int key;//键值
    struct node* next[1];//多层链表结点
} Node;

//跳跃表结构
typedef struct skiplist
{
    int level;//最大层数
    Node* head;//表头结点
} Skiplist;

//创建结点
Node* create_node(int level, int key)
{
    Node* p = (Node*)malloc(sizeof(Node) + level * sizeof(Node*));//分配对应层次的结点
    if (!p)
        return NULL;
    p->key = key;
    return p;
}

//创建跳跃表
Skiplist* create_sl()
{
    Skiplist* sl = (Skiplist*)malloc(sizeof(Skiplist));
    if (!sl)
        return NULL;
    sl->level = 0;//初始化跳跃表层数为0层
    Node* h = create_node(MAX_L - 1, 0);//创建跳跃表头结点
    if (!h)
    {
        free(sl);
        return NULL;
    }
    sl->head = h;
    for (int i = 0; i < MAX_L; i++)
        h->next[i] = NULL;//初始化数组
    return sl;
}

double random()//生成0~1的随机数
{
    double q = rand() / (double)RAND_MAX;
    return q;
}

static int level_rand()//生成新结点的级数
{
    int level = 1;
    while (random() <= prob)
        level++;
    return (level <= MAX_L) ? level : MAX_L;
}

//向跳跃表中插入元素
bool insert(Skiplist* sl, int key)
{
    //step1:查找到在每层待插入位置,更新update数组
    Node* update[MAX_L];
    Node* q = NULL, * p = sl->head;
    //找到每一层插入前的一个结点,放在update数组中
    for (int i = sl->level - 1; i >= 0; i--)
    {
        while ((q = p->next[i]) && q->key < key)
            p = q;
        update[i] = p;
    }
    if (q && q->key == key)//如果插入的结点已经存在
    {
        return true;
    }
    /**************************************/
    //step2:随机产生一个层数
    int level = level_rand();//随机产生新结点的层数
    //如果新生成的结点层数比原跳跃表大
    if (level > sl->level)
    {
        for (int i = sl->level; i < level; i++)
        {
            update[i] = sl->head;//多出的层数讲跳跃表头结点放入update数组
        }
        sl->level = level;//更新跳跃表的层数
    }
    /****************************************/
    //step3:从高层至下插入
    q = create_node(level, key);//创建新结点
    if (!q)
        return false;
    //根据update数组在每一层中插入新结点
    for (int i = level - 1; i >= 0; i--)
    {
        q->next[i] = update[i]->next[i];
        update[i]->next[i] = q;
    }
    return true;
}

//删除跳跃表中的元素
bool erase(Skiplist* sl, int key)
{
    Node* update[MAX_L];
    Node* q = NULL, * p = sl->head;
    //找到每一层待删除前的一个结点,放在update数组中
    for (int i = sl->level - 1; i >= 0; i--)
    {
        while ((q = p->next[i]) && q->key < key)
            p = q;
        update[i] = p;
    }
    //判断q是否为待删除的结点
    if (!q || (q && q->key != key))
        return false;
    //从最高层开始逐层删除结点
    for (int i = sl->level - 1; i >= 0; i--)
    {
        if (update[i]->next[i] == q)
        {
            update[i]->next[i] = q->next[i];
            //如果删除了最高层唯一的结点,则层数减一
            if (sl->head->next[i] == NULL)
                sl->level--;
        }
    }
    free(q);
    return true;
}

//查找跳跃表中元素
Node* search(Skiplist* sl, int key)
{
    Node* q = NULL, * p = sl->head;
    for (int i = sl->level - 1; i >= 0; i--)
    {
        while ((q = p->next[i]) && q->key < key)
            p = q;
        if (q && q->key == key)
            return q;
    }
    return NULL;
}

//从最高层开始逐层打印
void print(Skiplist* sl)
{
    Node* q;
    for (int i = sl->level - 1; i >= 0; i--)
    {
        q = sl->head->next[i];
        printf("level %d:\n", i + 1);
        while (q)
        {
            printf("key:%d\t", q->key);
            q = q->next[i];
        }
        printf("\n");
    }
}

//释放跳跃表
void free_sl(Skiplist* sl)
{
    if (!sl)
        return;
    Node* q = sl->head;
    Node* next;
    while (q)
    {
        next = q->next[0];
        free(q);
        q = next;
    }
    free(sl);
}

int main()
{
    srand((int)time(0));//随机数种子
    Skiplist* sl = create_sl();
    for (int i = 1; i < 20; i++)
    {
        insert(sl, i);
    }
    print(sl);
    cout << "******************************" << endl;
    for (int i = 11; i < 20; i++)
    {
        if (!erase(sl, i))
            printf("No!\n");
    }
    print(sl);
    free_sl(sl);
    return 0;
}

//#include "pch.h"
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
    float  a = 0;
    cout<<atan(3.0 / a);
    float c = 1 / a;
    cout << c;
    int b = 0;
    if(a==b)
    {
        cout << "hello";
    }
    else
    {
        cout << "bad";
    }
}


/**class TestCallback {
public:
    int num = 5;
    static int num2;
    void call(void(*callback)(int));
    static void callBack2(int num);
};
int TestCallback::num2 = 6;//静态变量必须初始化

void callBack1(int num)
{
    cout << "------start func1------" << endl;
    cout << "func1的num = " << num << endl;
    cout << "------end func1------" << endl;
}

void TestCallback::call(void(*callback)(int))
{
    cout << "------调用回调------" << endl;
    callback(this->num);
    cout << "------调用结束" << endl;
    cout << endl;
}

void TestCallback::callBack2(int num)
{
    cout << "------start func2------" << endl;
    cout << "func2的num = " << num << endl;
    cout << "func2访问类的静态成员变量num2 = " << TestCallback::num2 << endl;
    cout << "------end func2------" << endl;
}

int main()
{
    TestCallback t;
    t.call(callBack1);
    t.call(TestCallback::callBack2);
}*/

#pragma once
#ifndef LINK_STACK_H
#define LINK_STACK_H
namespace xmj{
#define MAX_SIZE 20
    /*
    ******作业思路
    ******采用双向链表
    *****栈底指向头节点,栈顶指向入栈的节点。出栈:栈顶指针利用节点反方向指针折返
    */
    typedef int DateType;
    typedef struct DateLink {
        //DateType* date;
        DateType date;
        DateLink* front;
        DateLink* next;
        DateLink();
        /*DateLink(const DateLink& other);
        const DateLink& operator=(const DateLink& other);
        ~DateLink();*/
    }DLhead;
    typedef DateLink* DateLink_ptr;
    typedef struct LinkStack {
        size_t length;
        DateLink_ptr top;
        DateLink_ptr base;
        LinkStack();
    }LS;
    bool initLinkStack(LinkStack& linkStack, DLhead& head);
    bool pushLinkStack(LinkStack& linkStack, DateLink& dateNode, DLhead& head);
    bool popLinkStack(LinkStack& linkStack, DateLink& dateNode);
    size_t LinkStackCount(LinkStack& linkStack);
    bool deleteLinkStack(LinkStack& linkStack);
}

#endif // !LINK_STACK_H

#include"LinkStack.h"
#include<string.h>
namespace xmj{
    LinkStack::LinkStack() {
        length = 0;
        top = nullptr; base = nullptr;
    }
    DateLink::DateLink() {
        //date = new DateType();
        date = 0;
        front = nullptr;
        next = nullptr;
    }
    //DateLink::DateLink(const DateLink& other) {
    //  date = new DateType();
    //  memcpy(date, other.date, sizeof(DateType));
    //  next = nullptr;
    //}
    //const DateLink& DateLink::operator=(const DateLink& other) {
    //  date = new DateType();
    //  memcpy(date, other.date, sizeof(DateType));
    //  next = nullptr; return other;
    //}
    //DateLink::~DateLink() { delete date; }
    bool pushLinkStack(LinkStack& linkStack, DateLink& dateNode, DLhead& head) {
        if (linkStack.length == MAX_SIZE)return false;      
        if (head.next == nullptr&&linkStack.base== linkStack.top) {
            head.next = &dateNode; dateNode.front = &head;
            linkStack.top = &dateNode; linkStack.length++;
            return true; 
        }
        linkStack.top->next = &dateNode; dateNode.front = linkStack.top;
        linkStack.top = &dateNode; linkStack.length++;
        return true;
    }

    bool popLinkStack(LinkStack& linkStack, DateLink& dateNode) {
        if (linkStack.length == 0)return false; 
         DateLink* tp = linkStack.top;
         dateNode = *tp;
         linkStack.top = tp->front;
         linkStack.top->next = nullptr; delete tp;
         linkStack.length--;
        return true;
    }

    size_t LinkStackCount(LinkStack& linkStack) {
        return size_t(linkStack.length);
    }

    bool deleteLinkStack(LinkStack& linkStack) {
        if (linkStack.length==0)return false;
        while ((linkStack.length)>0) {
            DateLink* tp = linkStack.top;           
            linkStack.top = tp->front;
            linkStack.top->next = nullptr; delete tp;
            linkStack.length--;
        }
        linkStack.base=nullptr; linkStack.top = linkStack.base;
        return true;
    }   
    bool initLinkStack(LinkStack& linkStack, DLhead& head) {
        if (&linkStack == nullptr || !&head)return false;
        linkStack.base = &head;
        linkStack.top = linkStack.base;
        return true;
    }
}

#pragma once
#ifndef SQ_STACK2_H
#define SQ_STACK2_H

#define MAX_SIZE 20
typedef int DateType;
typedef struct SQ_STACK2 {
    size_t top;
    DateType* base;
    SQ_STACK2();
}SQ_S2;
bool initSqStack2(SQ_STACK2& sqStack, size_t maxSize);
bool pushSqStack2(SQ_STACK2& sqStack, DateType date);
bool popSqStack2(SQ_STACK2& sqStack, DateType& popDate);
size_t sqStackCount2(SQ_STACK2& sqStack);
bool deleteSqStack2(SQ_STACK2& sqStack);
#endif // !SQ_STACK2_H

#include"SqStack2.h"
SQ_STACK2::SQ_STACK2() {
    top = 0; base = nullptr;
}

bool initSqStack2(SQ_STACK2& sqStack, size_t maxSize) {
    if (&sqStack == nullptr || maxSize < 1)return false;
    sqStack.base = new DateType[maxSize]();
    return true;
}

bool pushSqStack2(SQ_STACK2& sqStack, DateType date) {
    if (!&sqStack || sqStack.top == MAX_SIZE || !sqStack.base)return false;
    *(sqStack.base+ sqStack.top) = date; sqStack.top++;
    return true;
}

bool popSqStack2(SQ_STACK2& sqStack, DateType& popDate) {
    if (!&sqStack || sqStack.top == 0 || !sqStack.base)return false;
    popDate = *(sqStack.base + --sqStack.top);
    return true;
}

size_t sqStackCount2(SQ_STACK2& sqStack) {
    return size_t(sqStack.top);
}

bool deleteSqStack2(SQ_STACK2& sqStack) {
    if (!&sqStack)return false;
    sqStack.top = 0;
    delete sqStack.base;
    return true;
}




Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay