我將從碼開始:如何從數組的特定值創建向量?
#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。好的一點。 –