Ввод данных в map
У меня есть код который выводит заранее заданные возраст, рост и вес. Как переобразовать это что бы можно было вводить выше указанные данные?
#include <iostream>
#include <map>
#include <iomanip>
#include <string>
using namespace std;
class People {
public:
int age, height, weight;
People(int age, int height, int weight) {
this->age = age;
this->height = height;
this->weight = weight;
}
};
pair<string, int> *f() {
return new pair<string, int>("Vlad, ", 16);
}
int main() {
map<string, People> obj = map<string, People>();
obj.insert(pair<string, People>("Name", People(16, 180, 80))); // age, height, weight
for (pair<string, People> p : obj) {
cout << "Name: " << p.first << " Age: " << p.second.age << " Height: " << p.second.height << " Weight: " << p.second.weight << "\n\n";
}
cout << f()->first << " " << f()->second << endl;
system("pause");
return 0;
}
Ответы (1 шт):
Автор решения: AR Hovsepyan
→ Ссылка
Можно так:
//...
#include <iterator>
using Pair = std::pair<std::string, People>;
using In = std::istream_iterator<Pair>;
std::istream& operator>>(std::istream& input, const Pair& p)
{
auto [a, b] = p;
input >> a >> b.age >> b.height >> b.weight;
return input;
}
int main() {
map<string, People> obj;
//...
obj.insert(In(std::cin), In());
//...
return 0;
}
Можно и по разному...