C++ 使用C+从点向量创建int向量+;11
我有一个简单的点结构C++ 使用C+从点向量创建int向量+;11,c++,vector,c++11,C++,Vector,C++11,我有一个简单的点结构 struct mypoint { int x; int y; }; 以及mypoints的vector vector<mypoint> myvector; 使用C++11是否有办法在一行(或两行)代码中获得相同的结果?既然您使用的是C++11,您可以使用新的for语法 vector<int> newvector; for( const auto &pt : myvector) { newvector.push_b
struct mypoint
{
int x;
int y;
};
以及mypoint
s的vector
vector<mypoint> myvector;
使用C++11是否有办法在一行(或两行)代码中获得相同的结果?既然您使用的是C++11,您可以使用新的for语法
vector<int> newvector;
for( const auto &pt : myvector)
{
newvector.push_back(pt.x);
newvector.push_back(pt.y);
}
向量newvector;
用于(常数自动&pt:myvector)
{
newvector.push_back(第x部分);
newvector.push_back(第y部分);
}
由于您使用的是C++11,因此可以使用新的for语法
vector<int> newvector;
for( const auto &pt : myvector)
{
newvector.push_back(pt.x);
newvector.push_back(pt.y);
}
向量newvector;
用于(常数自动&pt:myvector)
{
newvector.push_back(第x部分);
newvector.push_back(第y部分);
}
std::vector extractIntsFromPoints(常量std::vector&pointVector)
{
std::vector-retVector;
用于(常量自动&元素:点向量)
{
retVector.push_back(元素x);
retVector.push_back(元素y);
}
返回向量;
}
在需要int向量的地方调用此函数。
我加入了基于范围的for循环,使其成为额外的C++11。std::vector extractIntsFromPoints(const std::vector&pointVector)
{
std::vector-retVector;
用于(常量自动&元素:点向量)
{
retVector.push_back(元素x);
retVector.push_back(元素y);
}
返回向量;
}
在需要int向量的地方调用此函数。
我加入了基于范围的for循环,使其成为额外的C++11。从帖子中窃取:
向量项;
std::transform(pairs.begin(),
pairs.end(),
标准:背面插入器(项目),
[](const std::pair&p){return p.first;});
从帖子中窃取:
向量项;
std::transform(pairs.begin(),
pairs.end(),
标准:背面插入器(项目),
[](const std::pair&p){return p.first;});
这里有大约4行,使用lambda:
vector<mypoint> points;
vector<int> iv;
points.push_back(mypoint(1,2));
points.push_back(mypoint(3,4));
points.push_back(mypoint(5,6));
for_each(points.cbegin(), points.cend(),
[&iv](const mypoint &pt) {
iv.push_back(pt.x);
iv.push_back(pt.y);
});
矢量点;
向量iv;
点。向后推(mypoint(1,2));
点。向后推(mypoint(3,4));
点。向后推(mypoint(5,6));
对于每个(points.cbegin(),points.cend(),
[&iv](常量mypoint和pt){
iv.推回(第x部分);
iv.推回(第y部分);
});
这里有大约4行,使用lambda:
vector<mypoint> points;
vector<int> iv;
points.push_back(mypoint(1,2));
points.push_back(mypoint(3,4));
points.push_back(mypoint(5,6));
for_each(points.cbegin(), points.cend(),
[&iv](const mypoint &pt) {
iv.push_back(pt.x);
iv.push_back(pt.y);
});
矢量点;
向量iv;
点。向后推(mypoint(1,2));
点。向后推(mypoint(3,4));
点。向后推(mypoint(5,6));
对于每个(points.cbegin(),points.cend(),
[&iv](常量mypoint和pt){
iv.推回(第x部分);
iv.推回(第y部分);
});
您可以使用std::pair
,在其中使用std::make\u pair
推送坐标,然后将std::pair
推送到向量中,例如:
mypoint a_point;
std::pair<int, int> point = std::make_pair(a_point.x, a_point.y);
vector<std::pair<int, int>> vec.push_back(point).
mypointa\u点;
std::pair point=std::make_pair(a_point.x,a_point.y);
向量向量推回(点)。
它可能体积庞大,但分为两行,可以很好地封装一个点,而不是分离每个点轴的大小并将它们放置在
std::vector
中。您可以使用std::pair
将坐标推入std::make_pair
中,然后将std::pair
推入向量,例如:
mypoint a_point;
std::pair<int, int> point = std::make_pair(a_point.x, a_point.y);
vector<std::pair<int, int>> vec.push_back(point).
mypointa\u点;
std::pair point=std::make_pair(a_point.x,a_point.y);
向量向量推回(点)。
也许体积庞大,但它分为两行,可以很好地封装一个点,而不是分离每个点轴的大小,并将它们放在
std::vector
中,正如reima已经指出的那样,如果您只想引用现有序列,将myvector.data()
转换为int*
(假设sizeof(mypoint)==2*sizeof(int)
成立)。
但是,如果您明确地想要展平序列的副本,那么最好创建一个如下的小实用程序函数:
template <typename T, typename U>
std::vector<T> flatten(std::vector<U> const& other) {
static_assert(std::is_trivially_copyable<U>::value,
"source type must be trivially copyable!");
static_assert(std::is_trivially_copy_constructible<T>::value,
"destination type must be trivially copy constructible!");
static_assert((sizeof(U) / sizeof(T)) * sizeof(T) == sizeof(U),
"sizeof(U) must be a multiple of sizeof(T)!");
return std::vector<T>(reinterpret_cast<T const*>(other.data()),
reinterpret_cast<T const*>(std::next(other.data(), other.size())));
}
template <typename U>
std::vector<typename U::value_type> flatten(std::vector<U> const& other) {
return flatten<typename U::value_type>(other);
}
请注意,此实用程序函数只不过是一个经过调整的构造函数,它使用固有的不安全的重新解释\u cast
将mypoint
指针转换为int
指针。
为了消除使用reinterpret\u cast
时出现的安全警告,flatte
函数使用了一些static\u assert
降落伞。因此,最好将所有这些隐藏在seprate函数中。
尽管如此,它还是使用了许多C++11特性,如auto
、move-construction、static\u-assert
、type-traits、std::next
和vector::data()
,这些特性几乎将您的呼叫站点代码缩减到最低限度
同样,这也是非常有效的,因为
vector
的范围构造函数只执行内存分配和调用uninitialized\u copy
,这可能会归结为对普通可复制类型的memcpy
调用。如reima所述,如果您只想引用现有的序列e、 将myvector.data()
强制转换为int*
(假设sizeof(mypoint)==2*sizeof(int)
有效)。
但是,如果您明确地想要展平序列的副本,那么最好创建一个如下的小实用程序函数:
template <typename T, typename U>
std::vector<T> flatten(std::vector<U> const& other) {
static_assert(std::is_trivially_copyable<U>::value,
"source type must be trivially copyable!");
static_assert(std::is_trivially_copy_constructible<T>::value,
"destination type must be trivially copy constructible!");
static_assert((sizeof(U) / sizeof(T)) * sizeof(T) == sizeof(U),
"sizeof(U) must be a multiple of sizeof(T)!");
return std::vector<T>(reinterpret_cast<T const*>(other.data()),
reinterpret_cast<T const*>(std::next(other.data(), other.size())));
}
template <typename U>
std::vector<typename U::value_type> flatten(std::vector<U> const& other) {
return flatten<typename U::value_type>(other);
}
请注意,此实用程序函数只不过是一个经过调整的构造函数,它使用固有的不安全的重新解释\u cast
将mypoint
指针转换为int
指针。
为了消除使用reinterpret\u cast
时出现的安全警告,flatte
函数使用了一些static\u assert
降落伞。因此,最好将所有这些隐藏在seprate函数中。
尽管如此,它还是使用了许多C++11特性,如auto
、move-construction、static\u-assert
、type-traits、std::next
和vector::data()
,这些特性几乎将您的呼叫站点代码缩减到最低限度
此外,这也是非常有效的,因为vector
的范围构造函数将只执行内存分配并调用unin
auto newvector = flatten(myvector);