0

Here is my code:

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

I am using this data in here:

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

I am sending the array to the map here:

if (studentLoc != getStudentList().end() && location != getMatchMap().end())
{
    getCourseScList().insert({ make_pair(s,c),make_pair(getDay1()[i],getDayHours())});
}

The question is how can I reach the array element:

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

The (last->second).second is representing my array but I can not print this to screen. What can I do?

1
  • "How to print array which I use in a map?" The same way that one which is not in map. I meant, you might reduce your sample to array<int,6> Array; Array dayHours{0,0,0,0,0,0}; std::cout << dayHours; // Error: How to print dayHours? Commented Dec 29, 2019 at 8:52

2 Answers 2

3

There is no operator<< defined for std::array<T>, Therefore, you need to iterate through the elements in the array to print it.

Let say your map is

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

Then using iterators MyMap::iterator. (Assuming you have MyMap& getCourseScList(); getter overload in your class. In case of MyMap::const_iterator, you should be having const MyMap& getCourseScList() const; overload)

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

In , you could use range-based for-loop, instead of the iterator based one.

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

However, if you have access to the or later compiler use structured binding for key-value pair, along with a range-based for-loop to make it more intuitive.

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

As a side note, please keep in mind the followings:

Sign up to request clarification or add additional context in comments.

1 Comment

@sonelektrikci Your welcome. Check out the update for the alternative in C++17.
2

You can also create your own stream insertion operator (<<) for your type 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;
}

Then you should be able to do the following without the loop:

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

which will call the operator above automatically.

Example:

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

2 Comments

I tried overloaded operator but I can't use it in my class.
@sonelektrikci You should define the operator << outside your class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.