简体   繁体   English

将 class 的向量拆分为 class 成员的向量

[英]Split vector of class into vector of class member

I found a problem in C++ that I cannot solve by myself and so I would like to ask you, if there is a possibility to split vector of class into vector of class member .我在 C++ 中发现了一个我自己无法解决的问题,所以我想问你,是否有可能将class的向量拆分为class 成员的向量

class CPerson
{
 public:
    string m_Name;
    string m_Surname;
}
    
class CPersonList
{
 public:
    CPersonList ( const vector<CPerson> & ref )
    {
        m_List = ref;
    }
 private:
    vector<CPerson> m_List;
}

class CAllPersonsData
{
 public:
    CPersonList "MYPROBLEM" ( ) { CPersonList listAlfa ("IDONOTKNOW"); return listAlfa; }
 private:
    class CData
    {
     public:
        CPerson m_Person;
        string m_Data;
    }

    vector<CData> m_Alldata;

"MYPROBLEM" is a method which should create and return CPersonList listAlfa. “MYPROBLEM”是一个应该创建并返回 CPersonList listAlfa 的方法。

listAlfa should be created by constructor, where I would like to pass vector created from vector . listAlfa应该由构造函数创建,我想传递从vector创建的向量 In my eyes, "IDONOTKNOW" should look like vector<CData.CPerson> .在我看来,“IDONOTKNOW”应该看起来像vector<CData.CPerson> So, there should be same data as in vector but without atributes m_Data .因此,应该有与向量中相同的数据,但没有属性m_Data

Is there any option how to do this?有什么选择如何做到这一点?

Of course I do not want to copy it, because I would like to pass it just by reference.当然我不想复制它,因为我想通过引用传递它。

Thank you so much for your tips;-)非常感谢您的提示;-)

A vector owns its content so if you want to return a std::vector<CPerson> you would have to copy the data, for example向量拥有它的内容,所以如果你想返回一个std::vector<CPerson>你必须复制数据,例如

std::vector<CPerson> people;
people.reserve(m_Alldata.size());
std::transform(m_Alldata.begin(), m_Alldata.end(), std::back_inserter(people), 
               [](CData const & data) { return data.m_person; });
return people;

If you want to return a vector of "references" to the instances in m_Alldata , you could return a std::vector<CPerson *> :如果要返回m_Alldata中实例的“引用”向量,可以返回std::vector<CPerson *>

std::vector<CPerson *> people;
people.reserve(m_Alldata.size());
std::transform(m_Alldata.begin(), m_Alldata.end(), std::back_inserter(people), 
               [](CData const & data) { return &data.m_person; });
return people;

or a std::vector<std::reference_wrapper<CPerson>> :std::vector<std::reference_wrapper<CPerson>>

std::vector<std::refernce_wrapper<CPerson>> people;
people.reserve(m_Alldata.size());
std::transform(m_Alldata.begin(), m_Alldata.end(), std::back_inserter(people), 
               [](CData const & data) { return std::ref(data.m_person); });
return people;

As an alternative to a std::vector you could return a begin/end pair of iterators that dereference to a CPerson -reference:作为std::vector的替代方案,您可以返回一对取消引用CPerson引用的开始/结束迭代器:

#include <boost/compute/iterator/transform_iterator.hpp>

using boost::compute::make_transform_iterator;

auto people_begin()
{
  return make_transform_iterator(m_Alldata.begin(), [](CData const & data) { return data.m_person; });
}

auto people_end()
{
  return make_transform_iterator(m_Alldata.end(), [](CData const & data) { return data.m_person; });
}

If you can use the ranges-library you could return a transform_view of your data:如果您可以使用范围库,则可以返回数据的 transform_view:

#include <ranges>

auto people()
{
  return std::ranges::transform_view(m_Alldata, [](CData & data) { return data.m_person; });
}

The returned object has a begin() and end() and its iterators dereference to a CPerson & .返回的 object 具有begin()end()及其迭代器对CPerson &的取消引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM