如何将Boost UUID随机生成器输出传递给main

时间:2014-11-20 08:52:06

标签: c++ boost

我正在尝试了解Boost UUID的工作原理。我已经制作了以下代码

#include <boost/uuid/uuid.hpp>            
#include <boost/uuid/uuid_generators.hpp> 
#include <boost/uuid/uuid_io.hpp> 
#include <iostream>

using namespace std;
using namespace boost;
using namespace uuids;

int main() {

    for (int i = 0; i < 40; ++i){
        uuid uuid = random_generator()();
        cout <<i+1<<"     "<<"\t"<<uuid << endl;
    }
    system("pause");

    return 0;
}

在此代码中,我使用UUID代码生成40行。我想将这些行放在一个函数中,并通过我的主类int main()

调用它们

任何想法如何做到这一点?

1 个答案:

答案 0 :(得分:1)

将它们生成标准库容器,例如, std::vector<uuid>

std::vector<uuid> foo() {
    std::vector<uuid> r;
    std::generate_n(back_inserter(r), 40, random_generator());
    return r;
}

<强> Live On Coliru

#include <boost/uuid/uuid.hpp>            
#include <boost/uuid/uuid_generators.hpp> 
#include <boost/uuid/uuid_io.hpp> 
#include <iostream>

using namespace std;
using namespace boost;
using namespace uuids;

std::vector<uuid> foo() {
    std::vector<uuid> r;
    std::generate_n(back_inserter(r), 40, random_generator());
    return r;
}


int main() {
    for(auto& uuid : foo())
        cout << uuid << endl;
}