std::move
Completed
Read n words into a vector a. Then vector<string> b = std::move(a);. Print the size of b, its contents joined by commas, and the size of a after a.clear().
Input
n, then n words.
Output
Three lines.
Example
Input
3 red green blue
Output
3 red,green,blue 0
Editor
C++
Hints
Moving transfers the contents instead of copying them.
A moved-from object is valid but unspecified - clear it before reuse.
Solution explanation
Move semantics make returning and passing big objects cheap.
Show solution
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> a(n);
for (auto &w : a) cin >> w;
vector<string> b = std::move(a);
a.clear();
cout << b.size() << "\n";
for (size_t i = 0; i < b.size(); i++) cout << b[i] << (i + 1 < b.size() ? "," : "\n");
cout << a.size() << "\n";
return 0;
}