Reputation: 539
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?
Upvotes: 0
Views: 271
Reputation: 6494
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
}
Upvotes: 2
Reputation: 32847
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 c++11, 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 c++17 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:
Upvotes: 3