gpt4 book ai didi

c++ - 如何打印我在 map 中使用的数组?

转载 作者:行者123 更新时间:2023-12-01 15:08:19 28 4
gpt4 key购买 nike

这是我的代码:

typedef array<int,6> Array;
Array dayHours{0,0,0,0,0,0};

我在这里使用这些数据:
void Schedule::studentSchedule()
{
string c, s;
int courseNum;
list<string>::iterator studentLoc;
map<pair<string, int>, pair<string, Array> >::iterator location;

cout << "Enter the student name" << endl;
cin >> s;
cout << "Enter how many course you want?" << endl;
cin >> courseNum;
wrongCourse:
cout << "Enter the course names you want" << endl;
for (int i = 0; i < courseNum; ++i)
{
cin >> c;
auto predicate = [&](auto& course) { return compareName(course, c); };
studentLoc = find(getStudentList().begin(), getStudentList().end(), s);
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
map<pair<string, int>, pair<string, Array> >::iterator it;
cout << "Student:\t\t" << "Course:\t\t" << "Course Day:\t\t" << "Course Hours:" << endl;
if (studentLoc != getStudentList().end() && location != getMatchMap().end())
{
getCourseScList().insert({ make_pair(s,c),make_pair(getDay1()[i],getDayHours()) });
}
else
{
cout << "The course you're writing isn't available.Please enter existed courses!" << endl;
goto wrongCourse;
}
}
}

我在这里将数组发送到 map :
if (studentLoc != getStudentList().end() && location != getMatchMap().end())
{
getCourseScList().insert({ make_pair(s,c),make_pair(getDay1()[i],getDayHours())});
}

问题是如何到达数组元素:
map< pair<string, string>, pair<string, Array> >::iterator last;
for (last = getCourseScList().begin(); last != getCourseScList().end(); ++last)
{
cout << (last->first).first << "\t\t\t\t"
<< (last->first).second
<< "\t\t" << (last->second).first
<< (last->second).second << endl;
}
(last->second).second代表我的数组,但是我无法将其打印到屏幕上。我能做什么?

最佳答案

没有为 operator<< 定义std::array<T>,因此,您需要遍历数组中的元素以进行打印。

假设您的 map 是

using Array = std::array<int, 6> ;
using MyMap = std::map<std::pair<std::string, std::string>, std::pair<std::string, Array>>;

然后使用迭代器 MyMap::iterator 。 (假设您的类中有 MyMap& getCourseScList(); getter重载。对于 MyMap::const_iterator,您应该有 const MyMap& getCourseScList() const;重载)
#include <array>
#include <map>

for (MyMap::iterator last = getCourseScList().begin(); // preferably cbegin() and cend(), as the entries will not be modified
last != getCourseScList().end(); ++last)
{
// print the key-pair (i.e. std::pair<std::string, std::string>)
std::cout << last->first.first << "\t\t\t\t" << last->first.second << "\t\t";
// print the first of value-pair (i.e. string of std::pair<std::string, Array>)
std::cout << last->second.first;
// now to print the array `Array`
for (const int element : last->second.second)
std::cout << element << " ";
}

中,您可以使用 range-based for -loop,而不是基于迭代器的迭代器。
for (const auto& entry : getCourseScList())
{
std::cout << entry.first.first << " " << entry.first.second << "\n";
std::cout << entry.second.first << " ";
for (const int element : entry.second.second)
std::cout << element << " ";
}

但是,如果您可以访问 或更高版本的编译器,请使用 structured binding作为键值对以及 range-based for -loop使其更加直观。
for (auto const&[key, value]: getCourseScList())
// ^^^^^^^^^^^^ -->structured binding
{
std::cout << key.first << " " << key.second << "\n";
std::cout << value.first << " ";
for (const int element : value.second)
std::cout << element << " ";
}

作为旁注,请记住以下几点:
  • What is wrong with using goto?
  • Why is "using namespace std;" considered bad practice?
  • 关于c++ - 如何打印我在 map 中使用的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59517865/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com