小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

流和STL迭代器

 孤步 2012-07-24

在vc++中程序中用了srandom()和random(),頭文件為stdlib.h,但編譯出現(xiàn)錯誤error C3861: “srandom”: 找不到標識符。
  原因是現(xiàn)在vc++編譯器的庫函數(shù)中沒有randomize()和random(),分別用srand()和rand()代替了。
  #include <time.h> //定義關(guān)于時間的函數(shù)  
  一般在用到time(NULL)(當前時間)函數(shù)時需要包含此頭文件  
  #include <stdlib.h> //定義雜項函數(shù)及內(nèi)存分配函數(shù)  
  一般在用到rand()和srand()函數(shù)時需要包含此頭文件  

  函數(shù)名: random 功 能: 隨機數(shù)發(fā)生器,也就是產(chǎn)生一個隨機數(shù)  
  用 法: int random(int num);  
  產(chǎn)生的隨機數(shù)范圍為0~num-1。  

  函數(shù)名: randomize  
  功 能: 初始化隨機數(shù)發(fā)生器,相當于撥隨機種子  
  用 法: void randomize(void);


/************************************************************************/

/*

對于迭代器,有另一種方法使用流和標準函數(shù)。理解的要點是將輸入/輸出流作為容器看待。

因此,任何接受迭代器參數(shù)的算法都可以和流一起工作。

 

  函數(shù)Display()顯示了如何使用一個輸出流迭代器。下面的語句將容器中的值傳輸?shù)?/span>cout輸出流對象中:

 

  copy(v.begin(), v.end(),

  ostream_iterator<int>(cout, "\t"));

第三個參數(shù)實例化了ostream_iterator<int>類型,并將它作為copy()函數(shù)的輸出目標迭代器對象?!?/span>\t”字符串是作為分隔符。

                                                                     */

/************************************************************************/

#include <iostream>

#include <stdlib.h>    // Need random(), srandom()

#include <time.h>      // Need time()

#include <algorithm>   // Need sort(), copy()

#include <vector>      // Need vector

 

using namespace std;

 

void Display(vector<int>& v, const char* s);

 

int main()

{

       // Seed the random number generator

       srand(time(NULL));

       // Construct vector and fill with random integer values

       vector<int> collection(10);

       for (int i = 0; i < 10; i++)

        collection[i] = rand() % 10000;

      

       // Display, sort, and redisplay

       Display(collection, "Before sorting");

       sort(collection.begin(), collection.end());

       Display(collection, "After sorting");

       return 0;

}

 

// Display label s and contents of integer vector v

void Display(vector<int>& v, const char* s)

{

       cout << endl << s << endl;

       copy(v.begin(), v.end(),ostream_iterator<int>(cout, "  "));

       cout << endl;

}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多