zoukankan      html  css  js  c++  java
  • STL 学习代码~

    #include<stdio.h>
    #include<numeric>
    #include<iostream>
    #include<functional>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define LL long long
    #define nmax 20
    int num[nmax];
    struct node {
    int x;
    };
    struct Add {
    int operator()(int n, node &M) const {
    return n + M.x;
    }
    };
    node Node[nmax];
    vector<int> first, second, third, four;
    int op_increase(int a) {
    return ++a;
    }
    int op_sum(int a, int b) {
    return a + b;
    }
    int main() {
    #ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
    #endif
    int i, k;
    for (i = 0; i < nmax; i++) {
    num[i] = i + 1;
    Node[i].x = i + 1;
    first.push_back((i + 1) * 10);
    }
    second.resize(first.size());
    transform(first.begin(), first.end(), second.begin(), op_increase);
    for (i = 0; i < nmax; i++) {
    cout << first[i] << " ";
    }
    cout << endl;
    for (i = 0; i < nmax; i++) {
    cout << second[i] << " ";
    }
    third.resize(first.size());
    transform(first.begin(), first.end(), second.begin(), third.begin(),
    op_sum);
    for (i = 0; i < nmax; i++) {
    cout << first[i] << " ";
    }
    cout << endl;
    for (i = 0; i < nmax; i++) {
    cout << second[i] << " ";
    }
    cout << endl;
    for (i = 0; i < nmax; i++) {
    cout << third[i] << " ";
    }
    cout << endl;
    four.resize(first.size());
    transform(first.begin(), first.end(), four.begin(),
    bind2nd(modulus<int>(), 6));
    for (i = 0; i < nmax; i++) {
    cout << four[i] << " ";
    }
    cout << endl << accumulate(four.begin(), four.end(), 0) << endl;

    printf("%d %I64d\n", accumulate(num, num + nmax,0),
    accumulate(num, num + nmax,1LL,multiplies<LL>()));
    printf("%d\n", accumulate(Node, Node + nmax, 0,Add()));
    copy(num, num + nmax,ostream_iterator<int>(cout, " "));
    cout << endl;
    vector<int> vec(num, num + nmax);
    k = count_if(vec.begin(), vec.end(), bind1st(greater<int>(), 5));
    printf("%d\n", k);
    k = count_if(vec.begin(), vec.end(), bind2nd(greater<int>(), 5));
    printf("%d\n", k);
    transform(num, num + nmax,num,bind2nd(modulus<int>(), 5));
    for (i = 0; i < nmax; i++) {
    printf("%d ", num[i]);
    }
    printf("\n%d\n", accumulate(num, num + nmax,0));
    return 0;
    }

  • 相关阅读:
    java-以月为单位,得到一年中某一个月份的范围
    计算两个时间段相差几个月(包含相差的哪些月份)
    单个进程最大线程数
    Dell PowerEdge R720内存安装原则
    Java [parms/options] range -b 100 -c 10 -i 100 -t 300 -s 180
    PhysicalDrive
    classpath和环境变量设置
    MySQL正则表达式
    MySQL模式匹配(LIKE VS REGEXP)
    ubuntu为什么没有/etc/inittab文件? 深究ubuntu的启动流程分析
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2200182.html
Copyright © 2011-2022 走看看