zoukankan      html  css  js  c++  java
  • 剑指offer系列7:调整数组顺序使奇数位于偶数前面

    标准库类型vector表示对象的集合,其中所有对象的类型都相同。因为它“容纳着”其他对象,所以它也被称作容器(container)。

    C++中有类模板和函数模板,vector是类模板。注意,vector是模板而非类型。

    ---C++ Primer

     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 using namespace std;
     5 class Solution {
     6 public:
     7     void reOrderArray(vector<int> &array) {
     8         vector<int> odd;
     9         vector<int> ji;
    10         for (int i = 0; i < array.size(); i++)
    11         {
    12             if (array[i] % 2 == 0)
    13             {
    14                 odd.push_back(array[i]);
    15             }
    16             else
    17             {
    18                 ji.push_back(array[i]);
    19             }
    20         }
    21         ji.insert(ji.end(),odd.begin(),odd.end());
    22         array.assign(ji.begin(), ji.end());
    23         /*
    24         for (int i = 0; i < ji.size(); i++)
    25         {
    26             cout << array[i] << endl;
    27         }
    28         */
    29     }
    30 };
    31 int main()
    32 {
    33     Solution so;
    34     vector<int> te = { 1,2,3,4,5,6,7};
    35     so.reOrderArray(te);
    36     return 0;
    37 }

    这个题要熟悉vector的各种函数,今天这道题是在别人的帮助下做的。

    vector中的各种函数很多,慢慢要一个个记住,今天学习了insert,assigh,sort,加油噢。

    有一个问题是判断这个数为偶数的时候是取模计算的,我用(array[i])&(0x1)却报错:没有与这些操作数匹配的 "&" 运算符,这个问题我也不知道是为什么。

  • 相关阅读:
    移动端 推送的那些东西
    git 常用命令
    顶部提示 先下移出来 再上移出去
    ViewPager 高度自适应
    进制转换
    Android 适配
    适配三星Galaxy S8及S8+ 屏幕比例为 18.5:9
    dpi dp px 换算关系
    资源前缀及代码分析总结
    判断是否快速点击或者滑动
  • 原文地址:https://www.cnblogs.com/neverland0718/p/10976117.html
Copyright © 2011-2022 走看看