因此,您可以使用可以包含 strings 和 ints 的对向量。
此示例程序允许您逐个节点输入对,并将其存储在对的向量中(string 和 int):
Live sample
#include <iostream>
#include <vector>
int main()
{
int num;
std::string str;
std::vector<std::pair<std::string, int>> nodes; //container
std::cout << "Enter string and number" << std::endl;
while(std::cin >> str)
{
if(str == "exit") //type exit to leave the input cycle
break;
std::cin >> num;
nodes.push_back(std::make_pair(str, num));
}
for (const auto& p : nodes) // print the products
{
std::cout << p.first << " " << p.second << std::endl;
}
}
您现在可以将其纳入您需要做的事情,因为您对问题的描述无法让我准确理解您如何将员工与产品和名称联系起来。
编辑
因此,根据您的评论,我添加了一个新的解决方案来维护std::pair,它使用起来非常简单,而且我相信没有人会抱怨它,所以您需要一个具有对(名称、值向量)的容器。
我将姓名和值的输入分开,因为您的员工有多个姓名,因此更难管理输入。
没有多少员工和产品,您可以根据需要添加尽可能多的产品价值。
Live sample
#include <iostream>
#include <vector>
#include <sstream>
int main() {
double temp_num;
std::string str, name;
std::vector<std::pair<std::string, std::vector<double>>> nodes; //container
std::vector<double> values;
while(true){
std::cout << "Enter employee name ('exit' to leave): ";
getline(std::cin, str); //employee name
if (str == "exit") {
break;
}
name = str;
std::cout << "Enter values: ";
getline(std::cin, str); //values
std::stringstream ss(str);
while(ss >> temp_num)
values.push_back(temp_num);
nodes.push_back(std::make_pair(name, values));
}
std::cout << std::endl;
for (const auto& p : nodes) // print names and the products
{
std::cout <<"Name - " << p.first << ": ";
for (const auto& vals : p.second) {
std::cout <<"$"<< vals << " ";
}
std::cout << std::endl;
}
}