C++ 需要帮助(二维数组)
如何编写一个2d数组,让用户为每个国家的金牌、银牌和铜牌输入不同的值?我知道这很基本,但我真的需要帮助,因为我上周才开始C++。C++ 需要帮助(二维数组),c++,C++,如何编写一个2d数组,让用户为每个国家的金牌、银牌和铜牌输入不同的值?我知道这很基本,但我真的需要帮助,因为我上周才开始C++。 Country Gold Silver Bronze Canada 0 3 0 Italy 0 0 1 Germany 0 0 1 Japan 1 0 0 Kazakhstan
Country Gold Silver Bronze
Canada 0 3 0
Italy 0 0 1
Germany 0 0 1
Japan 1 0 0
Kazakhstan 0 0 1
Russia 3 1 1
South Korea 0 1 0
United States 1 0 1
对于任何特定的国家,都可能有一个数组来表示保存奖牌计数的行。一行中的每一列都与金、银或青铜相关联 暂时忽略国家,您可以执行以下操作:
// Hold all the medal values
std::vector<std::array<int, 3>> medal_table;
// Skip title line
std::string line;
std::getline(std::cin, line);
std::string country;
int gold, silver, bronze;
while (std::cin
>> country
>> gold
>> silver
>> bronze) {
std::array<int, 3> row{gold, silver, bronze};
medal_table.push_back(row);
}
现在,奖牌表
中的行与国家表
中的行之间存在对应关系
这种方法的缺点是,如果你想知道某个特定国家的奖牌数量,你必须先扫描国家表
,才能找到该国家。在country\u表
中找到的索引然后用于indemail\u表
,以查找奖牌数量
std::map<std::string, std::array<int, 3>> medal_map;
如果您可以从严格的二维数组改为允许二维索引的数组,则可以直接将国家名称映射到表示奖牌数量的行
std::map<std::string, std::array<int, 3>> medal_map;
现在,要查找特定国家的奖牌数量,只需按国家名称为奖牌地图编制索引。另一种使用
std::tuple
的方法是创建std::tuple
,然后将每个tuple添加到std::vector
由于您可以在国家名称中包含空格,因此使用getline()
读取国家名称,然后使用普通的iostream读取黄金、白银和青铜将更简单。从值创建std::tuple
后,使用std::vector
的.push_back()
成员函数将tuple添加到向量中
要输出值,您可以使用std::tim
将每个std::tuple
分离为单独的值,然后输出,例如
#include <iostream>
#include <iomanip>
#include <vector>
#include <tuple>
#include <limits>
int main (void) {
std::vector<std::tuple<std::string, int, int, int>> medals{}; /* vector tuples */
std::string country{}; /* string for country input */
int g, s, b; /* integers for gold, silver, bronze input */
while (1) { /* loop continually */
std::cout << "enter country: "; /* prompt and read country */
if (!getline (std::cin, country) || !country.length())
break;
std::cout << " gold silver bronze: "; /* prompt and read gold, silver, bronze */
if (!(std::cin >> g >> s >> b)) {
std::cerr << "error: invalid integer input.\n";
break;
} /* remove up to \n from stdin */
std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
medals.push_back (std::make_tuple (country, g, s, b)); /* add tuple to vector */
}
std::cout << "\nCountry Gold Silver Bronze\n"; /* output heading */
for (const auto& t : medals) { /* loop outputting formatted values */
std::tie (country, g, s, b) = t;
std::cout << std::left << std::setw(17) << country << std::right <<
std::setw(2) << g << std::setw(6) << s << std::setw(7) << b << '\n';
}
}
有很多方法可以做到这一点,这只是一种方法。仔细检查一下,如果有其他问题,请告诉我。您可以尝试使用
std::map
,将std:string
作为键,将std:tuple
作为值。或者将特定结构与4个字段一起使用。这取决于你将如何使用它。在最后一种情况下使用1D数组或使用std::string
和std::array
创建struct
,然后创建std::vector
?欢迎使用堆栈溢出中的几个示例。请尽快阅读该页面,并访问描述和(MCVE)的链接。提供必要的详细信息,包括您的MCVE、编译器警告和相关错误(如果有),这里的每个人都可以帮助您解决问题。另请参阅示例,您可以更具体地了解您希望用户能够做什么吗?漂亮的元组示例!谢谢,答案的转换有点讽刺。我的注释是字符串数组,但后来我得到了一个元组<代码>:)
medal_map[country] = row;
#include <iostream>
#include <iomanip>
#include <vector>
#include <tuple>
#include <limits>
int main (void) {
std::vector<std::tuple<std::string, int, int, int>> medals{}; /* vector tuples */
std::string country{}; /* string for country input */
int g, s, b; /* integers for gold, silver, bronze input */
while (1) { /* loop continually */
std::cout << "enter country: "; /* prompt and read country */
if (!getline (std::cin, country) || !country.length())
break;
std::cout << " gold silver bronze: "; /* prompt and read gold, silver, bronze */
if (!(std::cin >> g >> s >> b)) {
std::cerr << "error: invalid integer input.\n";
break;
} /* remove up to \n from stdin */
std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
medals.push_back (std::make_tuple (country, g, s, b)); /* add tuple to vector */
}
std::cout << "\nCountry Gold Silver Bronze\n"; /* output heading */
for (const auto& t : medals) { /* loop outputting formatted values */
std::tie (country, g, s, b) = t;
std::cout << std::left << std::setw(17) << country << std::right <<
std::setw(2) << g << std::setw(6) << s << std::setw(7) << b << '\n';
}
}
$ ./bin/tuple_metals
enter country: Canada
gold silver bronze: 0 3 0
enter country: Italy
gold silver bronze: 0 0 1
enter country: Germany
gold silver bronze: 0 0 1
enter country: Japan
gold silver bronze: 1 0 0
enter country: Kazakhstan
gold silver bronze: 0 0 1
enter country: Russia
gold silver bronze: 3 1 1
enter country: South Korea
gold silver bronze: 0 1 0
enter country: United States
gold silver bronze: 1 0 1
enter country:
Country Gold Silver Bronze
Canada 0 3 0
Italy 0 0 1
Germany 0 0 1
Japan 1 0 0
Kazakhstan 0 0 1
Russia 3 1 1
South Korea 0 1 0
United States 1 0 1