• 28.数值算法小结


      1 #include <vector>
      2 #include <list>
      3 #include <algorithm>
      4 #include <iostream>
      5 using namespace std;
      6 
      7 void main()
      8 {
      9     //拷贝数据,与反向拷贝
     10     {
     11         vector<int> myint{ 1,2,3,4,5 };
     12         list<int> mylist{ 6,7,8,9,10 };
     13         //指定长度进行拷贝(注意,不会把容器长度增长)
     14         /*copy(myint.begin(), myint.end()-2, mylist.begin());*/
     15         //从尾部到头部进行拷贝(反向拷贝)
     16         copy_backward(myint.begin(), myint.end(), mylist.end());
     17         for (auto i : mylist)
     18         {
     19             cout << i << endl;
     20         }
     21     }
     22     
     23     //交换数据
     24     {
     25         double a = 10;
     26         double b = 20;
     27         swap(a, b);
     28         cout << a << b << endl;
     29     }
     30     
     31     //交换容器
     32     {
     33         vector<int> myint{ 1,2,3,4 };
     34         vector<int> mynewint{ 6,7,8,9 };
     35         list<int> mylist{ 1,2,3,9 };
     36         swap(myint, mynewint);//交换数据
     37         //类型不一致,不允许交换数据
     38         //swap(myint,mylist);
     39     }
     40 
     41     //交换指针
     42     {
     43         int a = 5;
     44         int b = 10;
     45         //交换指针的地址
     46         iter_swap(&a, &b);
     47         cout << a << b << endl;
     48     }
     49 
     50     //交换容器中的两个数据
     51     {
     52         vector<int> myint{ 1,2,3,4,5 };
     53 
     54         iter_swap(myint.begin(), myint.begin() + 2);
     55         for (auto i : myint)
     56         {
     57             cout << i << endl;
     58         }
     59     }
     60 
     61     //交换两个容器间的一部分
     62     {
     63         vector<int> myint1{ 1,2,3,4 };
     64         vector<int> myint2{ 6,7,8,9 };
     65         //交换部分
     66         swap_ranges(myint1.begin(), myint1.begin() + 2, myint2.begin() + 2);
     67 
     68         for (auto i : myint2)
     69         {
     70             cout << i << endl;
     71         }
     72     }
     73 
     74     //批量修改容器中的元素
     75     {
     76         vector<int> myint{ 1,2,3,4,5 };
     77         transform(myint.begin(), myint.end(), myint.begin(), [](int x) {return x * x; });
     78         for (auto i : myint)
     79         {
     80             cout << i << endl;
     81         }
     82     }
     83 
     84     //批量替换
     85     {
     86         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
     87         replace(myint.begin(), myint.end(), 3, 300);
     88         for (auto i : myint)
     89         {
     90             cout << i << endl;
     91         }
     92     }
     93 
     94     //根据条件全部替换
     95     {
     96         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
     97         replace_if(myint.begin(), myint.end(), [](int x)->bool {return x % 2; }, 0);
     98         for (auto i : myint)
     99         {
    100             cout << i << endl;
    101         }
    102     }
    103 
    104     //批量替换到副本中
    105     {
    106         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    107         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
    108         replace_copy(myint.begin(), myint.end(), myintlast.begin(), 3, 300);
    109         for (auto i : myintlast)
    110         {
    111             cout << i << endl;
    112         }
    113     }
    114 
    115     //根据条件全部替换到副本中
    116     {
    117         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    118         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
    119         replace_copy_if(myint.begin(), myint.end(), myintlast.begin(), [](int x)->bool {return x % 2; }, 0);
    120         for (auto i : myintlast)
    121         {
    122             cout << i << endl;
    123         }
    124     }
    125     
    126     //填充一个容器
    127     {
    128         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    129         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
    130         fill(myintlast.begin(), myintlast.end(), 199);
    131 
    132         fill_n(myint.begin(), 3, 100);
    133         for (auto i : myintlast)
    134         {
    135             cout << i << endl;
    136         }
    137 
    138         for (auto i : myint)
    139         {
    140             cout << i << endl;
    141         }
    142     }
    143 
    144     //删除容器中指定的数据(不是彻底删除,删除后从后往前移动)
    145     {
    146         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    147         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
    148 
    149         //it保存最后一个元素的位置
    150         auto it = remove(myint.begin(), myint.end(), 3);
    151         //此种方式会显示后面的空间的数据
    152         /*for (auto i : myint)
    153         {
    154             cout << i << endl;
    155         }*/
    156         for (auto ib = myint.begin(); ib != it; ib++)
    157         {
    158             cout << *ib << endl;
    159         }
    160     }
    161 
    162     //删除容器中满足条件的数据(不是彻底删除,删除后从后往前移动)
    163     {
    164         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    165         vector<int> myintlast{ 0,0,0,0,0,0,0,0,0 };
    166 
    167         //it保存最后一个元素的位置
    168         auto it = remove_if(myint.begin(), myint.end(), [](int x)->bool {return x % 2; });
    169         //此种方式会显示后面的空间的数据
    170         /*for (auto i : myint)
    171         {
    172         cout << i << endl;
    173         }*/
    174         for (auto ib = myint.begin(); ib != it; ib++)
    175         {
    176             cout << *ib << endl;
    177         }
    178     }
    179 
    180     //对副本进行删除
    181     {
    182         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    183         vector<int> myintlast;
    184         for (int i = 0; i < 9; i++)
    185         {
    186             myintlast.push_back(0);
    187         }
    188 
    189         remove_copy(myint.begin(), myint.end(), myintlast.begin(), 3);
    190         for (auto i : myintlast)
    191         {
    192             cout << i << endl;
    193         }
    194         
    195     }
    196 
    197     //拷贝的时候删除连续重复的
    198     {
    199         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    200         unique(myint.begin(), myint.end());
    201         for (auto i : myint)
    202         {
    203             cout << i << endl;
    204         }
    205     }
    206 
    207     //拷贝的时候删除连续重复的(对副本)
    208     {
    209         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    210         int a[9]{ 0 };
    211         unique_copy(myint.begin(), myint.end(), a);
    212         for (auto i : a)
    213         {
    214             cout << i << endl;
    215         }
    216     }
    217 
    218     //容器反转
    219     {
    220         vector<int> myint{ 1,2,3,4,5,1,2,3,4 };
    221         reverse(myint.begin(), myint.end());
    222         for (auto i : myint)
    223         {
    224             cout << i << endl;
    225         }
    226     }
    227 
    228     
    229 
    230     //区间交换到副本中
    231     {
    232         vector<int> myint{ 1,2,3,4,5,6,7,8,9 };
    233         rotate(myint.begin(), myint.begin() + 4, myint.end());
    234         for (auto i : myint)
    235         {
    236             cout << i << endl;
    237         }
    238     }
    239     
    240     //区间交换
    241     {
    242         vector<int> myint{ 1,2,3,4,5,6,7,8,9 };
    243         int a[9]{ 0 };
    244         //begin取数组的头结点
    245         rotate_copy(myint.begin(), myint.begin() + 4, myint.end(), begin(a));
    246         for (auto i : a)
    247         {
    248             cout << i << endl;
    249         }
    250     }
    251 
    252     //洗牌算法
    253     {
    254         vector<int> myint{ 1,2,3,4,5,6,7,8,9 };
    255         random_shuffle(myint.begin(), myint.end());
    256         for (auto i : myint)
    257         {
    258             cout << i << endl;
    259         }
    260     }
    261 
    262     //根据条件进行分段(例如小的在左边,大的在右边)   (相对顺序会发生变化)
    263     {
    264         vector<int> myint{ 1,9,2,8,3,7,4,6,5,10 };
    265         //it的位置是分界点
    266         auto it = partition(myint.begin(), myint.end(), [](int x)->int {return x <= 5 ? 1 : 0; });
    267         for (auto i : myint)
    268         {
    269             cout << i << endl;
    270         }
    271     }
    272 
    273     //根据条件进行分段(对副本进行操作)(例如小的在一个容器,大的在一个容器)   (相对顺序会发生变化)
    274     {
    275         vector<int> myint{ 1,9,2,8,3,7,4,6,5,10 };
    276         vector<int> myintX{ 0,0,0,0,0,0,0,0,0,0 };
    277         vector<int> myintY{ 0,0,0,0,0,0,0,0,0,0 };
    278         
    279         //两段分别拷贝到两个容器
    280         auto it = partition_copy(myint.begin(), myint.end(),myintX.begin(),myintY.begin(), [](int x)->int {return x <= 5 ? 1 : 0; });
    281         for (auto i : myintX)
    282         {
    283             cout << i << endl;
    284         }
    285 
    286         for (auto i : myintY)
    287         {
    288             cout << i << endl;
    289         }
    290     }
    291 
    292     //根据条件进行分段(例如小的在左边,大的在右边)   (相对顺序不发生变化)
    293     {
    294         vector<int> myint{ 1,9,2,8,3,7,4,6,5,10 };
    295         //it的位置是分界点
    296         auto it = stable_partition(myint.begin(), myint.end(), [](int x)->int {return x <= 5 ? 1 : 0; });
    297         for (auto i : myint)
    298         {
    299             cout << i << endl;
    300         }
    301     }
    302 
    303     //填充
    304     {
    305         vector<int> myint(10);
    306         //指定数据填充
    307         generate(myint.begin(), myint.end(), []() {return 10; });
    308         //随机数填充
    309         generate(myint.begin(), myint.end(), rand);
    310         //指定位置填充n个
    311         generate_n(myint.begin()+2, 3, rand);
    312         for (auto i : myint)
    313         {
    314             cout << i << endl;
    315         }
    316 
    317     }
    318 
    319     cin.get();
    320 }
  • 相关阅读:
    Linux的kobject机制
    利用内核模块添加系统调用
    register_sysctl_table实现内核数据交互
    设计模式17---享元模式
    设计模式16---中介者模式
    java实现PV操作
    设计模式15---桥接模式
    设计模式15---迭代器模式
    设计模式14---组合模式
    设计模式13---备忘录模式
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8647288.html
Copyright © 2020-2023  润新知