打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
流和STL迭代器
userphoto

2012.07.24

关注

在vc++中程序中用了srandom()和random(),头文件为stdlib.h,但编译出现错误error C3861: “srandom”: 找不到标识符。
  原因是现在vc++编译器的库函数中没有randomize()和random(),分别用srand()和rand()代替了。
  #include <time.h> //定义关于时间的函数  
  一般在用到time(NULL)(当前时间)函数时需要包含此头文件  
  #include <stdlib.h> //定义杂项函数及内存分配函数  
  一般在用到rand()和srand()函数时需要包含此头文件  

  函数名: random 功 能: 随机数发生器,也就是产生一个随机数  
  用 法: int random(int num);  
  产生的随机数范围为0~num-1。  

  函数名: randomize  
  功 能: 初始化随机数发生器,相当于拨随机种子  
  用 法: void randomize(void);


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

/*

对于迭代器,有另一种方法使用流和标准函数。理解的要点是将输入/输出流作为容器看待。

因此,任何接受迭代器参数的算法都可以和流一起工作。

 

  函数Display()显示了如何使用一个输出流迭代器。下面的语句将容器中的值传输到cout输出流对象中:

 

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

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

第三个参数实例化了ostream_iterator<int>类型,并将它作为copy()函数的输出目标迭代器对象。“\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, constchar*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 labels and contents of integer vector v

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

{

       cout << endl<< s<< endl;

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

       cout << endl;

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
STL中的常用的vector,map,set,Sort用法 - c/c++程序设计 - j...
STL之vector的使用
Vector向量知识
C++STL之迭代器
C++容器类常用泛型函数
c++中容器总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服