首页 > 编程语言 >面试最常问的数组转树,树转数组 c++ web框架paozhu实现

面试最常问的数组转树,树转数组 c++ web框架paozhu实现

时间:2023-04-24 18:55:40浏览次数:48  
标签:std web 框架 int unsigned c++ 数组 targetdata sourcedata

刚毕业同学,找工作常被问 二维数组转树,树转二维数组 需要支持无限层级实现,如果你了解这个语言那么实现起来还要一番思考

c++ web框架 paozhu使用 需要实现数据库表数据到前台菜单实现,就是这种功能 二维数组转树,树转二维数组 保存时候树二维数组,展示时候树树状。

这个技术难点在于无限递归,这个树程序基本原理

现在看看c++怎么实现的,无限递归,家肯定是考虑到结构体 嵌套自己定义。

struct department_outjson_t
{
  unsigned int id          = 0;
  unsigned int parentid    = 0;
  bool isopen              = true;
  bool _is_use             = false;
  std::string title;
  std::string desc;
  std::vector<department_outjson_t> children;

};
department_outjson_t 就是结构体 嵌套定义
我们看看怎么实现的,先上代码,使用了模板

template <typename deps_json_type>
concept ishas_isuse_member = requires(deps_json_type m) { m._is_use; };

template <typename deps_json_type>
  requires ishas_isuse_member<deps_json_type>
void array_to_tree(std::vector<deps_json_type> &targetdata,
                   std::vector<deps_json_type> &sourcedata)
{

  if (targetdata.size() == 0)
  {
    for (unsigned int i = 0; i < sourcedata.size(); i++)
    {
      if (sourcedata[i].parentid == 0)
      {
        targetdata.push_back(sourcedata[i]);
        sourcedata[i]._is_use = true;
      }
    }
  }

  for (unsigned int i = 0; i < sourcedata.size(); i++)
  {
    if (sourcedata[i]._is_use == false)
    {
      for (unsigned int j = 0; j < targetdata.size(); j++)
      {
        if (targetdata[j].id == sourcedata[i].parentid)
        {
          targetdata[j].children.push_back(sourcedata[i]);
          sourcedata[i]._is_use = true;
          array_to_tree(targetdata[j].children, sourcedata);
        }
      }
    }
  }
}

 

就是这样可以实现了,注意是使用了扫描,怕影响性能,我用一个变量标注,当然也可以用保存地址方式。

 

顺便把 树转二维数组也实现了

template <typename deps_json_type>
void tree_to_array(std::vector<deps_json_type> &targetdata,
                   std::vector<deps_json_type> &sourcedata,
                   unsigned int parendid = 0)
{

  for (unsigned int i = 0; i < sourcedata.size(); i++)
  {
    if (sourcedata[i].parentid == parendid)
    {
      if (sourcedata[i].children.size() > 0)
      {
        deps_json_type temp = sourcedata[i];
        temp.children.clear();
        targetdata.push_back(temp);
        tree_to_array(targetdata, sourcedata[i].children, sourcedata[i].id);
      }
      else
      {
        targetdata.push_back(sourcedata[i]);
      }
    }
  }
}

 

这样给前端api多爽,前端也不用自己处理,每次提交整个树过来就可以了,看完是不是明白了,主要是控制id 和 parentid

更多paozhu有趣特性 以后一一介绍。

✅ 1. 自带json编解码不用第三方库,标准json支持
✅ 2. 支持多域名网站
✅ 3. 支持多域名ssl 服务端
✅ 4. 支持http/1.1、http/2协议
✅ 5. 支持websocket服务端
✅ 6. 框架自带websocket推送,支持定时推送到webscoket客户端
✅ 7. 支持同步httpclient get post
✅ 8. 框架自带ORM,使用链接池方式,目前支持mysql
✅ 9. 框架自带线程池,和用户代码运行的线程池
✅10. 框架使用asio自带的协程
✅11. 框架特色是I/O 使用协程池 用户代码运行使用线程池,类似GO那种调度,只是针对http请求调度
✅12. 框架支持普通文件gzip、br,并支持缓存到磁盘,下次不用cpu再压缩
✅13. 框架解析URL和POST,解析结果类似PHP GET POST方式获取内容
✅14. 自带sendmail类库
✅15. 生成二维码(qrcode),需要gd、qrencode库
✅16. 插件化编程,热动态更新,使用动态库方式
✅17. 框架内置通用数据缓存模块,ORM结果缓存,提高并发能力
✅18. 框架controller目录注解功能,方便添加URL路由映射,降低入门心智
✅19. 结构和类注解JSON功能,使用json_encode json_decode操作

 

https://github.com/hggq/paozhu

 

标签:std,web,框架,int,unsigned,c++,数组,targetdata,sourcedata
From: https://www.cnblogs.com/paozhu/p/17350533.html

相关文章

  • 初学者代码训练Day7(c/c++)
    兔子产子问题要求 流程图  代码1#include<iostream>2usingnamespacestd;34intmain()5{inta=1,b=1,sum=0,y;6printf("%d\n%d\n",a,b);7for(y=3;y<=30;y++)8{sum=a+b;9printf("%d\n",sum);10a=b;1......
  • 【c&c++】[Error] iostream.h: No such file or directory的解决办法
    直接上错误代码实例#include<iostream.h>intmain(){print('hello,world\n')return0;}编译通不过,直接出错 这是C语言转C++的两条经典错误C++中是没有iostream.h这个东西的(或者一般不会这么使用),正确用法是:#include<iostream>用了iostream还不......
  • 【c&c++】VScode报错error: ‘::main‘ must return ‘int‘ void main()
    在运行指针时终端出现error:‘::main’mustreturn‘int’voidmain()错误。源代码如下:#include<stdio.h>voidmain(){inta,*p,b,c,d,e;a=100;p=&a;/*(*&a)先进行&a运算,得a的地址,再进行*运算,即变量a的值*/b=*&a;printf("a=%d\n",a);......
  • 【c&c++】C++ 关于编译出现“undefined reference to `std::cout‘“的问题
    1、问题概述        在使用gcc编译c++代码时会出现undefinedreferenceto`std::cout',如编译如下代码:#include<iostream>usingnamespacestd;intmain(){cout<<"Helloworld!";return0;}然而,gcc下编译出现的问题是: 2、解决方法使用g++编译,g++......
  • 开心档之C++ 类 & 对象
    C++类&对象C++在C语言的基础上增加了面向对象编程,C++支持面向对象程序设计。类是C++的核心特性,通常被称为用户定义的类型。类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。类中的数据和方法称为类的成员。函数在一个类中被称为类的成员。C++类定义定......
  • C++数据结构(队列)
    队列是先进先出的线性表顺序队列顺序存储,使用一段连续的内存空间去依次存储队列中的数据代码实现:#include<iostream>#defineMaxSize10template<typenameT>classSeqQueue{public:SeqQueue();~SeqQueue();public:boolEnQueue(constT&e);b......
  • kubectl 命令 --save-config 将部署信息添加到注解,防止deploy或webhook通过注释添加
    1、--save-config为什么需要使用kubctlapply保存配置?kubectl apply<file.yaml>--save-config创建或更新部署,并将部署另存为元数据。文件上说--save-config[=false]:如果为true,则当前对象的配置将保存在其注释中。当您将来要对此对象执行kubectlapply时,这非常有用。为什么......
  • LeetCode 周赛 342(2023/04/23)容斥原理、计数排序、滑动窗口、子数组 GCB
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。前天刚举办2023年力扣杯个人SOLO赛,昨天周赛就出了一场Easy-Easy-Medium-Medium的水场,不得不说LeetCode是懂礼数的......
  • 【C/C++】 可变参数函数
    #include<stdio.h>#include<stdarg.h>/***按自定义格式符解析数据*/voidprocess(constchar*fmt,va_listargs){for(;*fmt;fmt++){if(*fmt=='%'){continue;}switch(*fmt){ca......
  • WebSphere Message Broker -JavaCompute组件的使用
      IBMWebSphereMessageBrokerJavaCompute节点的使用. importjava.util.List;importcom.ibm.broker.javacompute.MbJavaComputeNode;importcom.ibm.broker.plugin.*;publicclassSub_FFN_JavaComputeextendsMbJavaComputeNode{ privatefinalArticleCreator......