zoukankan      html  css  js  c++  java
  • C++关于容器vector的使用方法以及#ifdef #else #endif #if #ifndef 的使用

    //此处根据0还是1来判断具体使用那一段主函数
    
    #if 0
        #define WAY
    #endif
    
    #ifdef WAY
    #include <iostream>
    #include<string.h>
    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    
    #include <iomanip>
    using namespace std;
    int g_width=4;
    int main() {
    
    /*将数组赋值给vector的方法*/
        int  a[10]={1,2,3,4,5,6,7,8,9,10};
        cout <<"sizeof(a)"<<(sizeof(a)/sizeof(int))<<endl;
        vector<int> vec(a,a+(sizeof(a)/sizeof(int)));
                
        //容器的逆序排列
        reverse(vec.begin(),vec.end());
    
        //vector<int>::iterator i;
        cout.width(g_width);
        cout<<"a=";
        for(int  i=0;i<10;i++)
        {
            cout.width(g_width);
            cout <<setiosflags(ios::internal)<< a[i];
        }
        cout << endl;
        cout.width(g_width);
        //cout.fill(9);
        cout<<"vec=";
        for(int  i=0;i<10;i++)
        {
            //cout格式化输出,位宽为4个字节
            cout.width(g_width);
            cout<< vec[i];
        }
    cout << endl;
        
        return 0;
    }
    #else
    
    #include <iostream>     // std::cout
    #include <algorithm>    // std::fill
    
    using namespace std;
    
    int main () {
        int array[8]={0};                       // myvector: 0 0 0 0 0 0 0 0
    
        cout<<"=======begin======="<<"
    "; 
        for (int i = 0;i< 8;i++)
            cout << ' ' << array[i];
        cout << '
    '<<'
    ';
    
        fill (array,array+4,9);   // myvector: 5 5 5 5 0 0 0 0
        fill (array+3,array+6,9);   // myvector: 5 5 5 8 8 8 0 0
    
        cout<<"=======after fill======="<<"
    "; 
        for (int i = 0;i< 8;i++)
            cout << ' ' << array[i];
        cout << '
    '<<'
    ';
    
        /*这里的用法和memset类似,但是看来还是memset使用起来方便*/
        const int a=sizeof(array)/sizeof(int);
        fill (array,array+a,0);  
        for (int i = 0;i< 8;i++)
            cout << ' ' << array[i];
        cout << '
    '<<'
    ';
    
        return 0;
    }
    #endif
  • 相关阅读:
    HDOJ 1220 Cube
    LCIS(m*n) 最长公共上升子序列
    第二百九十七天 how can I 坚持
    第二百九十六天 how can I 坚持
    第二百九十五天 how can i 坚持
    第二百九十四天 how can I 坚持
    第二百九十三天 how can I 坚持
    第二百九十、一、二天 how can I 坚持
    第二百八十九天 how can I 坚持
    第二百八十八天 how can I坚持
  • 原文地址:https://www.cnblogs.com/Jlord/p/12739533.html
Copyright © 2011-2022 走看看