简体   繁体   English

在不使用boost :: counting_iterator的情况下插入一系列连续整数的最佳方法

[英]The Best way to insert a range of consecutive integers without boost::counting_iterator

What's the Best way to insert a range of consecutive integers without boost::counting_iterator.[c++] 在没有boost :: counting_iterator的情况下插入一系列连续整数的最佳方法是什么。[c ++]

    // Insert 1 to 9
    set<long> set1.insert(boost::counting_iterator<int>(1)
                          ,boost::counting_iterator<int>(10))

跨范围调用insert for循环工作得很好,并使代码易于维护。

You may use: 您可以使用:

std::set<long> s;
long l = 0;
std::generate_n(std::inserter(s, s.end()), 9, [&]{ return ++l; });

or the simple loop: 或简单的循环:

std::set<long> s;
for (long i = 1; i != 10; ++i) {
    s.insert(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM