zoukankan      html  css  js  c++  java
  • vector模糊查询

     1 void CMy0713Dlg::OnBnClickedButton1()
     2 {
     3     vecstduent stds;
     4     CString str;
     5     str.Format("张三");
     6     CStudent *pStd1 = new CStudent(str);
     7     str.Format("李四");
     8     CStudent *pStd2 = new CStudent(str);
     9     str.Format("王五");
    10     CStudent *pStd3 = new CStudent(str);
    11     
    12     stds.push_back(pStd1);
    13     stds.push_back(pStd2);
    14     stds.push_back(pStd3);
    15     int ans =  get_stds(stds,"");
    16 
    17     
    18 }
    19 
    20 UINT CMy0713Dlg::get_stds(vecstduent &stds,char*k)
    21 {
    22     int ans = 0;//符合k的个数
    23     CStudent* temp = NULL; // 用来交换的临时数
    24     //冒泡排序
    25     for (int i = 0; i < stds.size() -1 ; i++) 
    26     {
    27         for (int j = stds.size() - 1; j > i; j--) 
    28         {
    29             
    30             if (stds[j - 1]->m_strName > stds[j]->m_strName) 
    31             {
    32                 temp = stds[j - 1];
    33                 stds[j - 1] = stds[j];
    34                 stds[j] = temp;
    35             }
    36         }
    37     }
    38     //二分搜索 返回k的第一个索引 复杂度O(log2n)
    39     auto it = stds.begin();
    40     int left = 0;
    41     int right = stds.size() -1  ;
    42     while(left < right)
    43     {
    44         int mid = (left + right) / 2;
    45         int i = 0;
    46         while(i < mid)
    47         {
    48             it ++;
    49             i++;
    50         }
    51         if((*it)->m_strName.Compare(k) >= 0)//比较字符串
    52         {
    53             right = mid;//缩进右边界
    54         }
    55         else
    56         {
    57             left = mid +  1;//缩进左边界
    58         }
    59         if(left >= right)//防止死循环
    60         {
    61             if((*it)->m_strName.Compare(k) < 0)
    62                 it ++;
    63             break;
    64         }
    65         it = stds.begin();
    66     }
    67 
    68 
    69 
    70     //从索引开始遍历 直到不再符合条件
    71     for(;it != stds.end();)
    72     {
    73         CString cstr = (*it)->m_strName;
    74         if(cstr.Find(k) == 0)//如果包含
    75         {
    76             it = stds.erase(it);
    77             m_ShowInfo.AddString(cstr);
    78             ans ++;
    79         }
    80         else //如不包含 之后的全部舍弃
    81             break;
    82     }
    83 
    84 
    85     //显示列表
    86     for(auto it = stds.begin();it!=stds.end();it++)
    87     {
    88         m_ShowInfo.AddString((*it)->m_strName);
    89     }
    90     return ans;
    91 }
  • 相关阅读:
    课程总结第十一周
    用户场景分析
    团队冲刺10
    课程总结第十周
    团队冲刺09
    梦断代码阅读笔记03
    转发和重定向的区别
    request
    servletConfig
    servlet
  • 原文地址:https://www.cnblogs.com/Jawen/p/11186868.html
Copyright © 2011-2022 走看看