【问题标题】:Generating multiple files of shuffled integers for a given range c++为给定范围c ++生成多个混洗整数文件
【发布时间】:2016-03-27 14:36:12
【问题描述】:

我正在尝试为特定范围(例如:1000、10000 等)生成多个混洗整数文件(例如 100)。这是我到目前为止尝试过的代码

// random_shuffle example
#include <iostream>     // std::cout
#include <algorithm>    // std::random_shuffle
#include <vector>       // std::vector
#include <ctime>        // std::time
#include <cstdlib>      // std::rand, std::srand
#include <fstream>

// random generator function:
int myrandom (int i) { return std::rand()%i;}

int main(){
   std::srand ( unsigned ( std::time(0) ) );
   std::vector<int> myvector2;
// For creating 10 different shuffled files
   for(int j=0;j<10;j++){
// set some values:
      for (int i=1; i<=10000; ++i) myvector2.push_back(i);

// using built-in random generator:
      std::random_shuffle ( myvector2.begin(), myvector2.end() );

// using myrandom:
      std::random_shuffle ( myvector2.begin(), myvector2.end(), myrandom);

// print out content:
       std::cout << "Data Shuffled:";
// put content in out file
      std::ofstream f2("out.txt");
       for (std::vector<int>::iterator it=myvector2.begin(); it!=myvector2.end(); ++it)
       f2<< *it <<'\n';
     }
return 0;
}

文件“out.txt”被覆盖,我最终只得到一个文件。如何创建多个文件,每个文件中都有不同的洗牌整数集?

谢谢

【问题讨论】:

  • 将随机数写入文件是否重要?如果没有,您将需要减少代码以更接近问题的核心。另外,请把代码完整,以便其他人可以拿来编译。并对其进行格式化,以便其他人可以阅读和理解它。就目前而言,您的问题是题外话。
  • @UlrichEckhardt 。你能帮我理解为什么我的问题听起来离题吗?
  • @NivethaMahalakshmi 你应该阅读“How to create a Minimal, Complete, and Verifiable example”的帮助部分。

标签: c++ random file-io


【解决方案1】:

为了提供一个有教育意义的例子,这可能是我的做法。

评论是内联的。此代码使用了一些 c++14 特性。

请注意,整个操作是用标准算法表示的。这是使代码可移植、可维护、正确并为编译器提供最佳优化机会的最佳方式。

#include<random>
#include<fstream>
#include<vector>
#include<utility>
#include<iterator>
#include<algorithm>

// generate a vector of all integers in the set Is...
template<int...Is>
auto initial_vector(std::integer_sequence<int, Is...>)
{
    return std::vector<int>({ Is... });
}

// defering to template expansion function allows the compiler to populate the vector from a compile-time
// computed data table. Totally unnecessary, but I find it pleasing.
auto initial_vector()
{
    return initial_vector(std::make_integer_sequence<int, 10000>());
}

int main()
{
    auto eng = std::default_random_engine(std::random_device{}());

    // make our initial vector
    auto data = initial_vector();

    // for each data set...
    for (int i = 0 ; i < 100 ; ++i)
    {
        // shuffe the data...
        std::shuffle(std::begin(data), std::end(data), eng);

        // create a filename from th index of the data set
        auto strm = std::ofstream("data" + std::to_string(i) + ".txt");

        // copy the data-set to the file, encoded as newline-delimited strings representing decimal integers
        std::copy(std::begin(data), std::end(data), std::ostream_iterator<int>(strm, "\n"));
    }
}

【讨论】:

  • 这真的很有帮助。谢谢。
【解决方案2】:

您每次都使用相同的文件名 std::ofstream f2("out.txt");

每次尝试使用不同的名称,例如 out1.text、out2.txt、...

你可以在for循环中得到这个表单j

std::ostringstream stringStream;
stringStream << "out"<<j<<".txt";
std::string fileName= stringStream.str();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多