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 .
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.
listAlfa should be created by constructor, where I would like to pass vector created from vector . In my eyes, "IDONOTKNOW" should look like vector<CData.CPerson> . So, there should be same data as in vector but without atributes 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> 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 *>
:
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::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:
#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:
#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 &
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.