When it comes to JavaScript, Array.prototype.map is a powerful tool for creating a new array by applying a function to each element. Consider the following example:
const elements = [{ text: 'hi1' }, { text: 'hi2' }, { text: 'hihi3'}];
const map = elements.map(element => element.text);
console.log(map); // Returns ["hi1", "hi2", "hi3"]
Now, in C++, suppose we have a vector of custom class vector<A>
:
#include <iostream>
#include <vector>
using namespace std;
class A {
public:
std::string text;
A(std::string text) {
this->text = text;
}
};
int main() {
vector<A> elements;
for(int i = 0 ; i < 3 ; i++) {
// Creating the Text String
string input = "hi";
string index = to_string(i);
input += index;
// Inserting Element into Vector
A element(input);
elements.push_back(element);
}
// Here lies the challenge,
// Is there a dynamic function that can be used to return Object's variable to a new array?
// The desired result could be either a vector of A's text or an array of A's text.
}
Is there any method available to achieve this task and return the values as described?