C++ 如何从数组中的特定值创建向量?
我将从代码开始:C++ 如何从数组中的特定值创建向量?,c++,arrays,c++11,vector,constructor,C++,Arrays,C++11,Vector,Constructor,我将从代码开始: #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).co
#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;
}
#包括
#包括
使用名称空间std;
结构A
{
内色;
A(int p_f):字段(p_f){}
};
int main()
{
A la[4]={A(3),A(5),A(2),A(1)};
std::vector lv={begin(la).color,end(la).color};//我想从数组la的特定值创建向量
对于(std::vector::iterator it=fifth.begin();it!=fifth.end();++it)std::cout这应该可以工作
std::vector<int> lv;
std::transform(std::begin(la), std::end(la), std::back_inserter(lv), [](const A& a){
return a.color;
});
在这种情况下,您可以使用bind
:
std::transform(std::begin(la), std::end(la), std::back_inserter(lv), std::bind(&A::getColor, std::placeholders::_1));
或者您也可以使用std::mem_fn
to稍微短一点的方法(感谢@Piotr S.):
或者您可以使用std::mem\u fn
访问数据成员。在这种情况下,您甚至不需要实现getter方法:
std::transform(std::begin(la), std::end(la), std::back_inserter(lv), std::mem_fn(&A::color));
以下可能会有所帮助:
namespace detail
{
using std::begin;
using std::end;
template <typename Container, typename F>
auto RetrieveTransformation(const Container& c, F f)
-> std::vector<std::decay_t<decltype(f(*begin(c)))>>
{
// if `F` return `const T&`, we want `std::vector<T>`,
// so we remove reference and cv qualifier with `decay_t`.
//
// That handles additionally the case of lambda
// The return type of lambda [](const std::string&s) { return s;}
// - is `const std::string` for msvc
// - is `std::string` for for gcc
// (Note that the return type rules have changed:
// see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1048)
using F_Ret = std::decay_t<decltype(f(*begin(c)))>;
std::vector<F_Ret> res;
res.reserve(std::distance(begin(c), end(c)));
for (const auto& e : c)
{
res.push_back(f(e));
}
return res;
}
}
template <typename Container, typename F>
auto RetrieveTransformation(const Container& c, F f)
-> decltype(detail::RetrieveTransformation(c, f))
{
return detail::RetrieveTransformation(c, f);
}
名称空间详细信息
{
使用std::begin;
使用std::end;
模板
自动检索转换(常量容器和c、F)
->向量
{
//如果`F`return`const T&`,则需要`std::vector`,
//因此,我们删除了带有“decation\u t”的引用和cv限定符。
//
//这就额外处理了lambda的情况
//lambda[](const std::string&s){return s;}的返回类型
//-是msvc的'const std::string'
//-对于gcc是'std::string'
//(请注意,返回类型规则已更改:
//看http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1048)
使用F_Ret=std::decation_t;
std::向量res;
res.reserve(标准::距离(开始(c),结束(c));
用于(常数自动和电气:c)
{
res.push_back(f(e));
}
返回res;
}
}
模板
自动检索转换(常量容器和c、F)
->decltype(详细信息:检索转换(c,f))
{
返回细节::RetrieveTransformation(c,f);
}
然后将其用作
std::vector<int> lv = RetrieveTransformation(la, std::mem_fn(&A::getColor));
// or
// auto lv = RetrieveTransformation(la, [](const A&a){return a.color;});
std::vector lv=RetrieveTransformation(la,std::mem_fn(&A::getColor));
//或
//自动lv=检索转换(la,[](常量A&A){return A.color;});
std::mem\u fn(&A::getColor)
代替bind
也可以实现这一功能,它也适用于数据成员
namespace detail
{
using std::begin;
using std::end;
template <typename Container, typename F>
auto RetrieveTransformation(const Container& c, F f)
-> std::vector<std::decay_t<decltype(f(*begin(c)))>>
{
// if `F` return `const T&`, we want `std::vector<T>`,
// so we remove reference and cv qualifier with `decay_t`.
//
// That handles additionally the case of lambda
// The return type of lambda [](const std::string&s) { return s;}
// - is `const std::string` for msvc
// - is `std::string` for for gcc
// (Note that the return type rules have changed:
// see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1048)
using F_Ret = std::decay_t<decltype(f(*begin(c)))>;
std::vector<F_Ret> res;
res.reserve(std::distance(begin(c), end(c)));
for (const auto& e : c)
{
res.push_back(f(e));
}
return res;
}
}
template <typename Container, typename F>
auto RetrieveTransformation(const Container& c, F f)
-> decltype(detail::RetrieveTransformation(c, f))
{
return detail::RetrieveTransformation(c, f);
}
std::vector<int> lv = RetrieveTransformation(la, std::mem_fn(&A::getColor));
// or
// auto lv = RetrieveTransformation(la, [](const A&a){return a.color;});