ACM_小知识

直接用法

  1. lower_bound :
    返回大于或等于val的第一个元素位置
    如[2, 2, 5, 5, 5, 8, 8]中找5返回2, 找4返回2
  2. upper_bound:
    返回大于val的第一个元素位置
    如[2, 2, 5, 5, 5, 8, 8]中找5返回5, 找4返回2
  3. stringstream:
    把东西放进缓存区, 方便再读出来

    
    #include <cstdio>
    
    
    #include <iostream>
    
    
    #include <sstream>
    
    using namespace std;
    
    int main() {
        stringstream buf;
        string in;
        while(getline(cin, in)) {
            buf << in;
            string tmp;
            while(buf >> tmp) cout << tmp << endl;
        }
    }
    
  4. 删除文件 删除所有除了.cpp以外的文件 慎用 可能把系统文件之类的东西也删除了!!!!

    
    #include <bits/stdc++.h>
    
    
    #include <windows.h>
    
    
    int find_all_files(const char * lpPath)
    {
        char szFind[MAX_PATH];
        WIN32_FIND_DATA FindFileData;
        strcpy(szFind,lpPath);
        strcat(szFind,"\\*.*");
        HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
        if(INVALID_HANDLE_VALUE == hFind)
            return -1;
    
        do
        {
            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if(strcmp(FindFileData.cFileName,".")!=0 && strcmp(FindFileData.cFileName, "..")!=0)
                {
                    //发现子目录,递归之
                    char szFile[MAX_PATH] = {0};
                    strcpy(szFile,lpPath);
                    strcat(szFile,"\\");
                    strcat(szFile,FindFileData.cFileName);
                    find_all_files(szFile);
                }
            }
            else
            {
                //找到文件,处理之
                int ok = 1;
                char tmp[MAX_PATH] = {0};
                strcpy(tmp, lpPath);
                strcat(tmp, "\\");
                strcat(tmp, FindFileData.cFileName);
                int len = strlen(tmp);
                if((len >= 4) && tmp[len-1] == 'p' && tmp[len-2] == 'p' && tmp[len-3] == 'c' && tmp[len-4] == '.') ok = 0;
                if(ok) {
                    if(remove(tmp)) std::cout << "failed remove " << tmp  << std::endl;
                    else std::cout << "succeed remove " << tmp  << std::endl;
                }
    //            std::cout << lpPath << "\\" << FindFileData.cFileName << std::endl;
            }
        }while(::FindNextFile(hFind,&FindFileData));
    
        ::FindClose(hFind);
    
        return 0;
    }
    
    int main() {
        char s[MAX_PATH];
        gets(s);
        for(int i = 0; s[i]; ++i) {
            if(s[i] == '\\') {
                int len = strlen(s);
                for(int j = len; j - i; --j) s[j] = s[j-1];
                s[len+1] = '\0';
                ++i;
            }
        }
        find_all_files(s);
    }
    
  5. 计算程序的运行时间(精确到毫秒)

    
    #include <time.h>
    
    
    #include <cstdio>
    
    
    int main() {
        double s, t;
        s = clock();//得到开始时间
        int times = 1e9;
        printf("run %d times = ", times);
        for(int i = 0; i < times; ++i) ;
        t = clock();//得到结束时间
        printf("%f seconds\n", (t - s) * 1000 / CLOCKS_PER_SEC);
    }
    

lower_bound

  • 先给图
    lower_bound(有这元素)
    这里写图片描述
    lower_bound(没这个元素)
    这里写图片描述
    函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置(返回查找元素的第一个可安插位置);
    upper_bound(有这个元素)
    这里写图片描述
    upper_bound(没这个元素)
    这里写图片描述
    upper_bound(返回刚好大于这个元素的位置)
  • 实现
    假设a是一个数组
    格式: iterator res = lower_bound(a_first, a_last, val) a_first和a_last都是迭代器(当然也可以是指针) val是要找的值 那么就会得到a[res] == val并且res取最小值

    • 标准库的实现:
      这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

  • 下面是手写的实现:

这里写图片描述

upper_bound

手写的upper_bound小于改等于就可以了
这里写图片描述
标准库的不是改成等于, 不过相差不多

  • PS
    元素个数等于upper_bound - lower_boudn
    这里写图片描述
    (数组中有3个2)

stringstream

别人介绍得很好 : http://www.cppblog.com/Sandywin/archive/2007/07/13/27984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值