如何打印我在地图中使用的数组?

How to print array which I use in a map?

这是我的代码:

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;
        }
    }
}

我将数组发送到此处的地图:

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 代表我的数组,但我无法将其打印到屏幕上。我能做什么?

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

假设你的地图是

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

然后使用迭代器MyMap::iterator。 (假设您的 class 中有 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 << " ";
}

, you could use 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 << " ";
}

但是,如果您可以访问 or later compiler use structured binding for key-value pair, along with a 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?

您还可以为您的类型创建自己的流插入运算符 (<<) Array:

#include <algorithm>
#include <array>
#include <iterator>
#include <iostream>

typedef std::array<int, 6> Array;

std::ostream& operator<<(std::ostream& o, const Array& arr)
{
    std::copy(arr.cbegin(), arr.cend(), std::ostream_iterator<int>(o, " "));
    return o;
}

那么你应该可以在没有循环的情况下执行以下操作:

std::cout << last->second.second;

会自动调用上面的运算符

示例:

int main()
{
    Array dayHours{ 0,1,0,0,0,0 };
    std::cout << dayHours;
    //prints 0 1 0 0 0 0
}