我将从码开始:如何从数组的特定值创建向量?
#include <iostream>
#include <vector>
using namespace std;
struct A
{
int color;
A(int p_f) : field(p_f) {}
};
int main()
{
A la[4] = {A(3),A(5),A(2),A(1)};
std::vector<int> lv = {begin(la).color, end(la).color};//I would like to create vector from specific value from array la
for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) std::cout << ' ' << *it;
return 0;
}
一般来说,我想创建从阵列特定值的向量。 正如你所看到的,la是一个数组,我想创建一个不包含整个la数组但只包含颜色的矢量。 vector(int)不是矢量(A),哪个矢量{3,5,2,1},所以不是A,而只是int的颜色。它也可以在C++ 11中使用。谢谢。
' std :: mem_fn(&A :: getColor)'代替'bind'也可以做到这一点,它也适用于数据成员 –
@P iotrS。好的一点。 –