板凳——————————————————c++(60)

这段代码展示了C++中的几种算法实现,如打印序列、查找、相邻元素比较等,并通过示例进行了演示,包括`print`、`find`、`find_if`、`adjacent_find`、`find_first_of`、`search`、`search_n`、`min_element`、`max_element`、`replace_copy`、`replace_if`等。此外,还涉及到了字符串的比较和字符生成等操作。
摘要由CSDN通过智能技术生成

//Thinking in c++ p 677    
//2020年06月19日 10时42分18秒
//PrintSequance.h
//#pragma once
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>
template<typename Iter>
void print(Iter first, Iter last, const char *nm = "",
               const char * sep = "\n",
               std::ostream & os = std::cout)
{
    if(nm != 0 && *nm != '\n')
       os << nm << ": " << sep;
    typedef typename
          std::iterator_traits<Iter>::value_type T;
          std::copy(first, last,
               std::ostream_iterator<T>(std::cout, sep));
        os << std::endl;
}

struct PlusOne{
    bool operator()(int i, int j) {return j == i + 1;}
};

class MulMoreThan{
         int value;
    public:
         MulMoreThan(int val) : value(val) { }
         bool operator() (int v, int m) { return v * m > value; }
};

#include <cstring>
#include <set>
#include <cstdlib>

class SkipGen{
         int i;
         int skp;
    public:
         SkipGen(int start = 0, int skip = 1)
            : i(start), skp(skip) {}
         int operator()(){
             int r = i;
             i += skp;
             return r;
         }
};

class URandGen{
        std::set<int> used;
        int limit;
    public:
        URandGen(int lim) : limit(lim){}
        int operator()(){
            while(true){
                int i = int(std::rand()) % limit;
                if(used.find(i) == used.end()){
                    used.insert(i);
                    return i;
                }
            }
        }
};

class CharGen{
         static const char* source;
         static const int len;
     public:
         char operator()(){
              return source[std::rand() % len];
         }
};

const char* CharGen::source = "ABCDEFGHIJK"
  "LMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const int CharGen::len = std::strlen(source);

struct IsUpper {
  bool operator()(char c) { return isupper(c); }
};

int main(){
    int a[] {1, 2, 3, 4, 5, 6, 6, 7, 7, 7, 8,
             8, 8, 8,  11, 11, 11, 11, 11};
    const int ASZ = sizeof a / sizeof *a;
    std::vector<int> v(a, a + ASZ);
    print(v.begin(), v.end(), "v", " ");
    std::vector<int>::iterator it = find(v.begin(), v.end(), 4);
    std::cout << "find: " << *it << std::endl;
    it = std::find_if(v.begin(), v.end(),
         std::bind2nd(std::greater<int>(), 8));
    std::cout << "find_if: " << *it << std::endl;
    it = std::adjacent_find(v.begin(), v.end());
    while(it != v.end()) {
        std::cout << "adjacent_find: " << *it
             << ", " << *(it + 1) << std::endl;
             it = adjacent_find(it + 1, v.end());
    }
    it = adjacent_find(v.begin(), v.end(), PlusOne());
    while(it != v.end()) {
        std::cout << "adjacent_find PlusOne: " << *it
         << ", " << *(it + 1) << std::endl;
         it = adjacent_find(it + 1, v.end(), PlusOne());
    }
    int b[] {8, 11};
    const int BSZ = sizeof b / sizeof *b;
    print(b, b + BSZ, "b", " ");
    it = std::find_first_of(v.begin(), v.end(), b, b + BSZ);
    print(it, it + BSZ, "find_first_of", " ");
    it = std::find_first_of(v.begin(), v.end(),
         b, b + BSZ, PlusOne());
    print(it, it + BSZ, "find_first_of PlusOne", " ");
    it = std::search(v.begin(), v.end(), b, b + BSZ);
    print(it, it + BSZ, "search", " ");
    int c[] {5, 6, 7};
    const int CSZ = sizeof c / sizeof *c ;
    print(c, c + CSZ, "c", " ");
    it = search(v.begin(), v.end(), c, c + CSZ, PlusOne());
    print(it, it + CSZ, "search PlusOne", " ");
    int d[] {11, 11, 11};
    const int DSZ = sizeof d / sizeof *d;
    print(d, d + DSZ, "d", " ");
    it = std::find_end(v.begin(), v.end(), d, d + DSZ);
    print(it, v.end(), "find_end", " " );
    int e[] {9, 9};
    print(e, e + 2, "e", " ");
    it = find_end(v.begin(), v.end(), e, e + 2, PlusOne());
    print(it, v.end(), "find_end PlusOne", " ");
    it = std::search_n(v.begin(), v.end(), 3, 7);
    print(it, it + 3, "search_n 3, 7", " ");
    it = search_n(v.begin(), v.end(),
         6, 15, MulMoreThan(100));
    print(it, it + 6,
          "search_n 6, 15, MulMoreThan(100)", " ");
    std::cout << "min_element: "
              << *min_element(v.begin(), v.end()) << std::endl;
    std::cout << "max_element: "
              << *max_element(v.begin(), v.end()) << std::endl;
    std::vector<int> v2;
    std::replace_copy(v.begin(), v.end(),
              std::back_inserter(v2), 8, 47);
    print(v2.begin(), v2.end(), "replace_copy 8 -> 47", " ");
    std::replace_if(v.begin(), v.end(),
              std::bind2nd(std::greater_equal<int>(), 7), -1);
    print(v.begin(), v.end(), "replace_if >= 7 -> -1", " ");
    
    //Thinking in c++ p 681
    std::string s1("This is a test");
      std::string s2("This is a Test");//唯一区别 T
      std::cout << "s1: " << s1 << std::endl << "s2: " << s2 << std::endl;
      std::cout << "compare s1 & s1: "
           << std::equal(s1.begin(), s1.end(), s1.begin()) << std::endl;
      std::cout << "compare s1 & s2: "
           << std::equal(s1.begin(), s1.end(), s2.begin()) << std::endl;
      std::cout << "lexicographical_compare s1 & s1: "
           << std::lexicographical_compare(s1.begin(), s1.end(),
          s1.begin(), s1.end()) <<  std::endl;
      std::cout << "lexicographical_compare s1 & s2: "
           << std::lexicographical_compare(s1.begin(), s1.end(),
          s2.begin(), s2.end()) << std::endl;
          //c++ 操作平台, 大写字母字符 "领先于" 小写字母字符
      std::cout << "lexicographical_compare s2 & s1: "
           << std::lexicographical_compare(s2.begin(), s2.end(),
          s1.begin(), s1.end()) << std::endl;
      std::cout << "lexicographical_compare shortened "
          "s1 & full-length s2: " << std::endl;
    //s1内容复制到s3, 每次循环减去s1末尾的一个字符, 直到测试结果返回true
    //实际, s3中依次减到大写T, 之前两序列完全相同, 下面停止了. s3 < s1, s3 "领先于" s1      
      std::string s3(s1);
      while(s3.length() != 0) {
        bool result = std::lexicographical_compare(
          s3.begin(), s3.end(), s2.begin(),s2.end());
        std::cout << s3 << std::endl << s2 << ", result = "
         << result << std::endl;
        if(result == true) break;
        s3 = s3.substr(0, s3.length() - 1);
    }
    
    std::pair<std::string::iterator, std::string::iterator > p =
        std::mismatch(s1.begin(), s1.end(), s2.begin());
        print(p.first, s1.end(), "p.first", "");
        print(p.second, s2.end(), "p.second", "");
        
        
      std::string v3;
      v3.resize(25);
      std::generate(v3.begin(), v3.end(), CharGen());
      print(v3.begin(), v3.end(), "v3 original", "");
      // Create a set of the characters in v:
      std::string us(v3.begin(), v3.end());
      std::sort(us.begin(), us.end());
      std::string::iterator it1 = us.begin(), cit = v3.end(),
        uend = unique(us.begin(), us.end());
      // Step through and remove everything:
      while(it1 != uend) {
        cit = std::remove(v3.begin(), cit, *it1);
        print(v3.begin(), v3.end(), "Complete v3", "");
        print(v3.begin(), cit, "Pseudo v3 ", " ");
        std::cout << "Removed element:\t" << *it1
         << "\nPsuedo Last Element:\t"
         << *cit << std::endl << std::endl;
        ++it1;
      }
      std::generate(v3.begin(), v3.end(), CharGen());
      print(v3.begin(), v3.end(), "v3 ", "");
      cit = std::remove_if(v3.begin(), v3.end(), IsUpper());
      print(v3.begin(), cit, "v3 after remove_if IsUpper", " ");
      // Copying versions are not shown for remove()
      // and remove_if().
      std::sort(v3.begin(), cit);
      print(v3.begin(), cit, "sorted", " ");
      std::string v4;
      v4.resize(cit - v3.begin());
      std::unique_copy(v3.begin(), cit, v4.begin());
      print(v4.begin(), v4.end(), "unique_copy", " ");
      // Same behavior:
      /*
      c12.cpp:4114:59: 错误:对‘unique(std::vector<int>::iterator, std::__cxx11::basic_string<char>::iterat         or&, std::equal_to<char>)’的调用没有匹配的函数
 4114 |    cit = std::unique(v.begin(), cit, std::equal_to<char>());

      */
//      cit = std::unique(v.begin(), cit, std::equal_to<char>());
//      print(v3.begin(), cit, "unique equal_to<char>", " ");

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c12 c12.cpp
wannian07@wannian07-PC:~$ ./c12
v:  1 2 3 4 5 6 6 7 7 7 8 8 8 8 11 11 11 11 11
find: 4
find_if: 11
adjacent_find: 6, 6
adjacent_find: 7, 7
adjacent_find: 7, 7
adjacent_find: 8, 8
adjacent_find: 8, 8
adjacent_find: 8, 8
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find PlusOne: 1, 2
adjacent_find PlusOne: 2, 3
adjacent_find PlusOne: 3, 4
adjacent_find PlusOne: 4, 5
adjacent_find PlusOne: 5, 6
adjacent_find PlusOne: 6, 7
adjacent_find PlusOne: 7, 8
b:  8 11
find_first_of:  8 8
find_first_of PlusOne:  7 7
search:  8 11
c:  5 6 7
search PlusOne:  4 5 6
d:  11 11 11
find_end:  11 11 11
e:  9 9
find_end PlusOne:  8 8 11 11 11 11 11
search_n 3, 7:  7 7 7
search_n 6, 15, MulMoreThan(100):  7 7 7 8 8 8
min_element: 1
max_element: 11
replace_copy 8 -> 47:  1 2 3 4 5 6 6 7 7 7 47 47 47 47 11 11 11 11 11
replace_if >= 7 -> -1:  1 2 3 4 5 6 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1


s1: This is a test
s2: This is a Test
compare s1 & s1: 1
compare s1 & s2: 0
lexicographical_compare s1 & s1: 0
lexicographical_compare s1 & s2: 0
lexicographical_compare s2 & s1: 1
lexicographical_compare shortened s1 & full-length s2:
This is a test
This is a Test, result = 0
This is a tes
This is a Test, result = 0
This is a te
This is a Test, result = 0
This is a t
This is a Test, result = 0
This is a
This is a Test, result = 1

p.first: test
p.second: Test


v3 original: nWlrBbmQBhCDarzOwKkYHIDdq
Complete v3: nWlrbmQhCDarzOwKkYHIDdqdq
Pseudo v3 :  n W l r b m Q h C D a r z O w K k Y H I D d q
Removed element:    B
Psuedo Last Element:    d

Complete v3: nWlrbmQhDarzOwKkYHIDdqqdq
Pseudo v3 :  n W l r b m Q h D a r z O w K k Y H I D d q
Removed element:    C
Psuedo Last Element:    q

Complete v3: nWlrbmQharzOwKkYHIdqdqqdq
Pseudo v3 :  n W l r b m Q h a r z O w K k Y H I d q
Removed element:    D
Psuedo Last Element:    d

Complete v3: nWlrbmQharzOwKkYIdqqdqqdq
Pseudo v3 :  n W l r b m Q h a r z O w K k Y I d q
Removed element:    H
Psuedo Last Element:    q

Complete v3: nWlrbmQharzOwKkYdqqqdqqdq
Pseudo v3 :  n W l r b m Q h a r z O w K k Y d q
Removed element:    I
Psuedo Last Element:    q

Complete v3: nWlrbmQharzOwkYdqqqqdqqdq
Pseudo v3 :  n W l r b m Q h a r z O w k Y d q
Removed element:    K
Psuedo Last Element:    q

Complete v3: nWlrbmQharzwkYdqqqqqdqqdq
Pseudo v3 :  n W l r b m Q h a r z w k Y d q
Removed element:    O
Psuedo Last Element:    q

Complete v3: nWlrbmharzwkYdqqqqqqdqqdq
Pseudo v3 :  n W l r b m h a r z w k Y d q
Removed element:    Q
Psuedo Last Element:    q

Complete v3: nlrbmharzwkYdqqqqqqqdqqdq
Pseudo v3 :  n l r b m h a r z w k Y d q
Removed element:    W
Psuedo Last Element:    q

Complete v3: nlrbmharzwkdqqqqqqqqdqqdq
Pseudo v3 :  n l r b m h a r z w k d q
Removed element:    Y
Psuedo Last Element:    q

Complete v3: nlrbmhrzwkdqqqqqqqqqdqqdq
Pseudo v3 :  n l r b m h r z w k d q
Removed element:    a
Psuedo Last Element:    q

Complete v3: nlrmhrzwkdqqqqqqqqqqdqqdq
Pseudo v3 :  n l r m h r z w k d q
Removed element:    b
Psuedo Last Element:    q

Complete v3: nlrmhrzwkqqqqqqqqqqqdqqdq
Pseudo v3 :  n l r m h r z w k q
Removed element:    d
Psuedo Last Element:    q

Complete v3: nlrmrzwkqqqqqqqqqqqqdqqdq
Pseudo v3 :  n l r m r z w k q
Removed element:    h
Psuedo Last Element:    q

Complete v3: nlrmrzwqqqqqqqqqqqqqdqqdq
Pseudo v3 :  n l r m r z w q
Removed element:    k
Psuedo Last Element:    q

Complete v3: nrmrzwqqqqqqqqqqqqqqdqqdq
Pseudo v3 :  n r m r z w q
Removed element:    l
Psuedo Last Element:    q

Complete v3: nrrzwqqqqqqqqqqqqqqqdqqdq
Pseudo v3 :  n r r z w q
Removed element:    m
Psuedo Last Element:    q

Complete v3: rrzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 :  r r z w q
Removed element:    n
Psuedo Last Element:    q

Complete v3: rrzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 :  r r z w
Removed element:    q
Psuedo Last Element:    q

Complete v3: zwzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 :  z w
Removed element:    r
Psuedo Last Element:    z

Complete v3: zwzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 :  z
Removed element:    w
Psuedo Last Element:    w

Complete v3: zwzwqqqqqqqqqqqqqqqqdqqdq
Pseudo v3 :  
Removed element:    z
Psuedo Last Element:    z

v3 : SCDXrJmOWFrxsjyBldbEFSArC
v3 after remove_if IsUpper:  r m r x s j y l d b r
sorted:  b d j l m r r r s x y
unique_copy:  b d j l m r s x y   

*/   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值