English 中文(简体)
定点: 定点: 定点数
原标题:Initialization of std::vector<unsigned int> with a list of consecutive unsigned integers

我想使用一种特殊方法,以启动<代码>std:vector<unsign int>,这在《C++》一书中被描述为参考(Ulrich Breymann的德国书《Der C++ Programmer》)。 该书是关于STL系列的一节,特别提及list vectordeque。 在该节中,他写道,有两种类型的特别建筑商,即如果是<代码>。 X 指这种类型,

X(n, t) // creates a sequence with n copies of t
X(i, j) // creates a sequence from the elements of the interval [i, j)

我想利用第二版<代码>在t上签字,即:

std::vector<unsigned int> l(1U, 10U);

采用<代码>{1,2,...,9}的初始清单。 然而,我得到的是一种病媒,有一张<条码>,在t上签字,价值10:> 第二个变量是否存在,如果是,我如何迫使它被称作?

最佳回答

Reread the paragraphs near there describing what each of the parameters are. Specifically, it should mention that i and j are not values, but iterators. This constructor is very commonly used to make copies of other types of containers. If you want to get a sequence of values, the Boost library provides a counting iterator, that does exactly what you want.

std::vector<unsigned int> numbers(
     boost::counting_iterator<unsigned int>(0U),
     boost::counting_iterator<unsigned int>(10U));
问题回答

至少可以采取三种方式。 布赖恩早些时候提到过一个问题。

//method 1
generate(v.begin(), v.end(), [] { static int i {1}; return i++; });     

如果你使用C++11,你也可以使用:

//method 2
iota(v.begin(), v.end(), 1);

Or instead you can initialize your container with 1s and then do a partial sum on that. I don t think anybody will use this third method anyway :)

//method 3
vector<int> v(n, 1);                                                     
partial_sum(v.begin(), v.end(), v.begin()); 

采取非禁忌的方式,用自封的推器来做到这一点。

#include <vector>
#include <iostream>
#include <algorithm>

static int NUM_ITEMS = 10;

class gen_range {
    public:
        gen_range(int i) { idx = i; }
        int operator()() { return (idx++); };

    int idx;
};

int main() {

    std::vector<int> x(NUM_ITEMS);
    std::generate_n(x.begin(), NUM_ITEMS, gen_range(0));

    for (int i=0; i < x.size(); i++) {
        std::cout << x[i] << std::endl;
    }
}

No, that variant does not exist. The second constructor initializes a vector from two iterators that point into another sequence.

这里是“双引擎”构造者采取行动的例子:

int fill_data[4] = { 1, 2, 3, 4 };
std::vector<int> v(fill_data, fill_data + 4);

C++11:

std::vector<int> idxs (n);

std::generate_n (idxs.begin (), n, [] { static int i {1}; return i++; });




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签