Please pardon my lack of clarity on this topic. I am trying to create functions for inserting a big class into a vector. In this example, I use vector of ints as the big class.
#include <vector>
#include <iostream>
using namespace std;
vector<vector<int>> vectorOfVectors;
void fn(const vector<int> &v) {
vectorOfVectors.push_back(v);
}
void fn(vector<int> &&v) {
vectorOfVectors.push_back(std::move(v));
}
int main() {
vector<int> a({1});
const vector<int> b({2});
fn(std::move(a));
fn(b);
cout<<b[0];
}
Obviously, I want no copying to be done when possible. My questions:
- Does this code do the right thing?
- Is there a better way of doing this?
- For the same approach to work with custom classes, do I need to define move constructors?
Aucun commentaire:
Enregistrer un commentaire