0

I have two vector<map<string,MyObject>> type data and I want to move or copy some of MyObject data to copy another in the same type in C++ like below:

using namespace std;

struct MyObject{
  string id;
  string name;
  string phone;
  string salary;
};

//Somewhere else have below type.
vector<map<string,MyObject>> v1;
vector<map<string,MyObject>> v2;

How can I perform the above-given operation?

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
codepro123
  • 81
  • 6
  • Do you want to copy the vector? `v2=v1`. Related: https://stackoverflow.com/a/14977748/4123703. Note: `MyObject` should follow [rule of three](https://en.cppreference.com/w/cpp/language/rule_of_three). Edit: it follows rule of zero, which is better. – Louis Go Sep 03 '20 at 08:51
  • 1
    @LouisGo it's following the rule of zero, which is superior – Caleth Sep 03 '20 at 08:55
  • @ Daniel Langr, i want some of them not all data of MyObject. – codepro123 Sep 03 '20 at 08:56
  • 1
    Elaborate on "some of them" with full code (or a bit simplified) of `MyObject`. Mostly if it follows rule of zero, copy should be handled by compiler. – Louis Go Sep 03 '20 at 08:58
  • when you copy some from v1 to v2, do you want to keep what was in v1 before? – 463035818_is_not_an_ai Sep 03 '20 at 08:58
  • @LouisGo,I meant like name,phone only or name,salary only. – codepro123 Sep 03 '20 at 08:59
  • Partially copy could be done by full copy and partial erase. You may use a lambda to erase unwanted data. If you have performance issue, elaborate on your "full" context. – Louis Go Sep 03 '20 at 09:00
  • @codepro123 I would then prefer having a member function so call `MyObject& MyObject::copyNamePhone(const MyObject& rhs)` rather than overloading the copy or move ctor. This will allow the compiler to still provide usual/ normal copy-move ctor. – Const Sep 03 '20 at 09:10
  • @Const,Yes I already have member function but what after that how can i traversed and copy or move? – codepro123 Sep 03 '20 at 09:15
  • Take a look at [`std::transform`](https://en.cppreference.com/w/cpp/algorithm/transform), if you may use C++17 or above. Or simply `v2.reserve(v1,size())` and `emplace` to prevent multiple copy. – Louis Go Sep 03 '20 at 10:27

2 Answers2

1

A simple example, I change type to std::vector<Myobject> instead of your complext vector of map.

#include <iostream>
#include <string>
#include <vector>

int main()
{
    using std::string;
    using std::vector;

    struct MyObject {
        string id;
        string name;
        string phone;
        string salary;

        MyObject copyidname() {
            MyObject obj{ id, name, "","" };
            return obj;
        }
    };
    // Simplifiled type to demonstrate copy
    vector<MyObject> v1;
    v1.push_back( MyObject{ "1","1","1","1" });
    vector<MyObject> v2;

    // Set capacity to prevent reallocation.
    v2.reserve(v1.size());
    for (auto it = v1.begin(); it != v1.end(); ++it) {
        v2.push_back(it->copyidname());
    }
}
Louis Go
  • 2,213
  • 2
  • 16
  • 29
0

Since you have not provide any constructor to MyObject, compiler will generate necessary move-copy constructors for you.

So no worries, just do

v2 = v1;

for copy

v2 = std::move(v1);

for move


For the conditional copy you can use std::copy_if algorithum from <algorithm> header.

For the conditional move (move_if) you can modify the above algorithm like shown in this post :

template <class InputIt, class OutputIt, class UnaryPredicate>
OutputIt move_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate pred)
{
    return std::copy_if(std::make_move_iterator(first), std::make_move_iterator(last),
        d_first, pred);
}
Const
  • 1,306
  • 1
  • 10
  • 26
  • ,I want only some of the parameter of MyObject structure, not all – codepro123 Sep 03 '20 at 08:58
  • 1
    @codepro123 then you do [`std::copy_if`](https://en.cppreference.com/w/cpp/algorithm/copy) with condition for conditional copy, and [`move_if` like in this post](https://stackoverflow.com/questions/55502720/why-is-there-no-stdmove-if-algorithm) – Const Sep 03 '20 at 08:59
  • @Const Does `std::copy_if` allow conditional class member copy? Maybe I misunderstood it. – Louis Go Sep 03 '20 at 09:03
  • @LouisGo It will copy the entire class member, not some of them. If OP needs only some members in the struct to be copied, he needs to implement those constructors as per. – Const Sep 03 '20 at 09:06