Is there anyway to loop through an index in a boost::multi_index and perform a replace?
#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
using namespace boost::multi_index;
using namespace std;
struct name_record
{
public:
name_record(string given_name_,string family_name_,string other_name_)
{
given_name=given_name_;
family_name=family_name_;
other_name=other_name_;
}
string given_name;
string family_name;
string other_name;
string get_name() const { return given_name + " " + family_name + " " + other_name; }
void setnew(string chg)
{
given_name = given_name + chg;
family_name = family_name + chg;
}
};
struct NameIndex{};
typedef multi_index_container<
name_record,
indexed_by<
ordered_non_unique<
tag<NameIndex>,
composite_key
<
name_record,
BOOST_MULTI_INDEX_MEMBER(name_record,string, name_record::given_name),
BOOST_MULTI_INDEX_MEMBER(name_record,string, name_record::family_name)
>
>
>
> name_record_set;
typedef boost::multi_index::index<name_record_set,NameIndex>::type::iterator IteratorType;
typedef boost::multi_index::index<name_record_set,NameIndex>::type NameIndexType;
void printContainer(name_record_set & ns)
{
cout << endl << "PrintContainer" << endl << "-------------" << endl;
IteratorType it1 = ns.begin();
IteratorType it2 = ns.end ();
while (it1 != it2)
{
cout<<it1->get_name()<<endl;
it1++;
}
cout << "--------------" << endl << endl;
}
void modifyContainer(name_record_set & ns)
{
cout << endl << "ModifyContainer" << endl << "-------------" << endl;
IteratorType it3;
IteratorType it4;
NameIndexType & idx1 = ns.get<NameIndex>();
IteratorType it1 = idx1.begin();
IteratorType it2 = idx1.end();
while (it1 != it2)
{
cout<<it1->get_name()<<endl;
name_record nr = *it1;
nr.setnew("_CHG");
bool res = idx1.replace(it1,nr);
cout << "result is: " << res << endl;
it1++;
}
cout << "--------------" << endl << endl;
}
int main()
{
name_record_set ns;
ns.insert( name_record("Joe","Smith","ENTRY1") );
ns.insert( name_record("Robert","Brown","ENTRY2") );
ns.insert( name_record("Robert","Nightingale","ENTRY3") );
ns.insert( name_record("Marc","Tuxedo","ENTRY4") );
printContainer (ns);
modifyContainer (ns);
printContainer (ns);
return 0;
}
PrintContainer
-------------
Joe Smith ENTRY1
Marc Tuxedo ENTRY4
Robert Brown ENTRY2
Robert Nightingale ENTRY3
--------------
ModifyContainer
-------------
Joe Smith ENTRY1
result is: 1
Marc Tuxedo ENTRY4
result is: 1
Robert Brown ENTRY2
result is: 1
--------------
PrintContainer
-------------
Joe_CHG Smith_CHG ENTRY1
Marc_CHG Tuxedo_CHG ENTRY4
Robert Nightingale ENTRY3
Robert_CHG Brown_CHG ENTRY2
--------------
while (it1 != it2)
{
cout<<it1->get_name()<<endl;
name_record nr = *it1;
nr.setnew("_CHG");
bool res = idx1.replace(it1,nr);
cout << "result is: " << res << endl;
it1++;
}
The problem with this code is that, once you've replaced an element x, this is automatically reordered to its new position, so that it1, which keeps pointing to x, "jumps" through the sequence and might skip or revisit elements.
The solution is to add a level of indirection and collect iterators to all the elements before beginning the replacements:
#include <vector>
#include <boost/iterator/counting_iterator.hpp>
void modifyContainer(name_record_set & ns)
{
cout << endl << "ModifyContainer" << endl << "-------------" << endl;
NameIndexType & idx1 = ns.get<NameIndex>();
std::vector<IteratorType> v(
boost::make_counting_iterator(idx1.begin()),
boost::make_counting_iterator(idx1.end()));
std::vector<IteratorType>::iterator it1=v.begin(),
it2=v.end();
while (it1 != it2)
{
cout<<(*it1)->get_name()<<endl;
name_record nr = **it1;
nr.setnew("_CHG");
bool res = idx1.replace(*it1,nr);
cout << "result is: " << res << endl;
it1++;
}
cout << "--------------" << endl << endl;
}
In case you didn't use it before, the make_counting_iterator bit is just a compact way to populate the vector with iterators to all the elements of ns.
Related
I'm trying to make a name, and then give a list of courses this person has taken. Here is my code currently:
#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;
int main()
{
multimap<string,string> students;
students.insert(make_pair("john","cs1"));
students.insert(make_pair("john","cs2"));
for (auto itr = students.begin(); itr != students.end(); ++itr)
{
cout << itr->first << '\t' << itr->second << '\n';
}
}
This prints out:
John CS1
John CS2
If I wanted to make it print out the first value and then print out all second values, how would I do this? Do I need to use a list as the second value or something? Or is this doable with just multimaps? For example:
John CS1 CS2
You can use equal_range to achieve what you wanted, see:
std::string current_key = "";
for (auto itr = students.begin(); itr != students.end(); ++itr)
{
if(current_key == itr->first) {
continue;
} else {
current_key = itr->first;
}
const auto result = students.equal_range(itr->first);
cout << itr->first << ": ";
for(auto it = result.first; it != result.second; ++it) {
cout << it->second << " ";
}
cout << endl;
}
See working example here: https://ideone.com/RqCFOk
You'll may eventually end up creating a class to model a student, in the meantime, you could just iterate over the multimap container and check if the current key is the same as the previous:
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
using students_t = std::multimap<std::string, std::string>;
void print_students_list(students_t const& students)
{
// Start printing the name of the first student and the first course (the first key-value
// pair) if the container is not empty.
auto student{ students.cbegin() };
if ( student == students.cend() )
return;
std::cout << std::setw(12) << std::left << student->first
<< std::setw(8) << student->second;
// Then iterate over the rest of the container printing the name (after a newline)
// only if it's different from the previous one.
for ( auto prev{ student++ }; student != students.cend(); prev = student, ++student )
{
if ( student->first != prev->first )
std::cout << '\n' << std::setw(12) << student->first;
std::cout << std::setw(8) << student->second;
}
std::cout << '\n';
}
int main()
{
students_t students {
{"john", "cs1"},
{"john", "cs2"},
{"Karen", "cs2"},
{"Karen", "nc1"},
{"Karen", "nc2"},
{"Bob", "nc1"}
};
print_students_list(students);
}
I'm using CGAL (v. 4.14-2) to compute the visibility region (polygon) within a simple polygon from one of its vertices, using the Simple_polygon_visibility_2 class.
On a particular combination of polygon/vertex, I'm getting the following assertion error:
terminate called after throwing an instance of 'CGAL::Assertion_exception'
what(): CGAL ERROR: assertion violation!
Expr: k+1<vertices.size()
File: /usr/include/CGAL/Simple_polygon_visibility_2.h
Line: 678
This occurs in the method scan_edges of the class Simple_polygon_visibility_2, it seems like there should be some intersection with the given segment/ray, but none was found.
When I run the same code with the class Triangular_expansion_visibility_2, it seems to work, giving the output:
Regularized visibility region of q has 8 edges:
[7968 492 -> 7938 428]
[7884 408 -> 7968 492]
[8040 428 -> 7884 408]
[8090.99 428 -> 8040 428]
[8105.55 458.865 -> 8090.99 428]
[8090 456 -> 8105.55 458.865]
[7968 446 -> 8090 456]
[7938 428 -> 7968 446]
Here's a minimal working example:
#include <CGAL/Arr_naive_point_location.h>
#include <CGAL/Arr_segment_traits_2.h>
#include <CGAL/Arrangement_2.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Simple_polygon_visibility_2.h>
#include <CGAL/Triangular_expansion_visibility_2.h>
#include <fstream>
#include <iostream>
#include <string>
#include <list>
#include <vector>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef Kernel::Segment_2 Segment_2;
typedef CGAL::Polygon_2<Kernel, std::list<Point_2>> Polygon_2;
typedef CGAL::Arr_segment_traits_2<Kernel> Traits_2;
typedef CGAL::Arrangement_2<Traits_2> Arrangement_2;
typedef Arrangement_2::Face_handle Face_handle;
typedef Arrangement_2::Vertex_handle ArrVertex_handle;
typedef Arrangement_2::Edge_const_iterator Edge_const_iterator;
typedef Arrangement_2::Ccb_halfedge_circulator Ccb_halfedge_circulator;
using namespace std;
vector<Point_2> readInput() {
vector<Point_2> Points;
ifstream File;
File.open("polygon");
string Line;
long int Idx, XCoord, YCoord;
while (getline(File, Line)) {
istringstream Iss(Line);
Iss >> Idx >> XCoord >> YCoord;
Points.push_back({XCoord, YCoord});
}
return Points;
}
int main() {
// create environment
std::vector<Segment_2> segments;
Arrangement_2 env;
Segment_2 CurrSegment;
Polygon_2 AuxPoly;
auto Points = readInput();
auto QueryPt = Points[0];
ArrVertex_handle CurrPtHandle =
env.insert_in_face_interior(QueryPt, env.unbounded_face());
AuxPoly.push_back(Points[0]);
ArrVertex_handle NextPtHandle = CurrPtHandle;
ArrVertex_handle QueryPtHandle = CurrPtHandle;
for (auto i = 1; i < Points.size(); ++i) {
NextPtHandle = env.insert_in_face_interior(Points[i], env.unbounded_face());
CurrSegment = Segment_2(CurrPtHandle->point(), NextPtHandle->point());
env.insert_at_vertices(CurrSegment, CurrPtHandle, NextPtHandle);
CurrPtHandle = NextPtHandle;
AuxPoly.push_back(Points[i]);
}
assert(AuxPoly.is_simple());
CurrSegment = Segment_2(CurrPtHandle->point(), QueryPtHandle->point());
auto HalfEHandle =
env.insert_at_vertices(CurrSegment, CurrPtHandle, QueryPtHandle);
if (HalfEHandle->face()->is_unbounded()) {
// there are exactly two incident halfedges in the query point
auto FirstHalfE = HalfEHandle->target()->incident_halfedges();
HalfEHandle = (FirstHalfE != HalfEHandle) ? FirstHalfE : next(FirstHalfE, 1);
}
// compute non regularized visibility area
// Define visibiliy object type that computes regularized visibility area
typedef CGAL::Simple_polygon_visibility_2<Arrangement_2, CGAL::Tag_true> RSPV;
// typedef CGAL::Triangular_expansion_visibility_2<Arrangement_2, CGAL::Tag_true> RSPV;
Arrangement_2 regular_output;
RSPV regular_visibility(env);
regular_visibility.compute_visibility(QueryPtHandle->point(), HalfEHandle, regular_output);
std::cout << "Regularized visibility region of q has "
<< regular_output.number_of_edges() << " edges:" << std::endl;
for (Edge_const_iterator eit = regular_output.edges_begin();
eit != regular_output.edges_end(); ++eit)
std::cout << "[" << eit->source()->point() << " -> "
<< eit->target()->point() << "]" << std::endl;
return 0;
}
an auxiliary file arr_print.h to print the arrangement:
#ifndef _PRINT_ARR_H_
#define _PRINT_ARR_H_
#include <iostream>
//-----------------------------------------------------------------------------
// Print all neighboring vertices to a given arrangement vertex.
//
template<class Arrangement>
void print_neighboring_vertices (typename Arrangement::Vertex_const_handle v)
{
if (v->is_isolated())
{
std::cout << "The vertex (" << v->point() << ") is isolated" << std::endl;
return;
}
typename Arrangement::Halfedge_around_vertex_const_circulator first, curr;
typename Arrangement::Vertex_const_handle u;
std::cout << "The neighbors of the vertex (" << v->point() << ") are:";
first = curr = v->incident_halfedges();
do
{
// Note that the current halfedge is (u -> v):
u = curr->source();
std::cout << " (" << u->point() << ")";
++curr;
} while (curr != first);
std::cout << std::endl;
return;
}
//-----------------------------------------------------------------------------
// Print all vertices (points) and edges (curves) along a connected component
// boundary.
//
template<class Arrangement>
void print_ccb (typename Arrangement::Ccb_halfedge_const_circulator circ)
{
typename Arrangement::Ccb_halfedge_const_circulator curr = circ;
typename Arrangement::Halfedge_const_handle he;
std::cout << "(" << curr->source()->point() << ")";
do
{
he = curr;
std::cout << " [" << he->curve() << "] "
<< "(" << he->target()->point() << ")";
++curr;
} while (curr != circ);
std::cout << std::endl;
return;
}
//-----------------------------------------------------------------------------
// Print the boundary description of an arrangement face.
//
template<class Arrangement>
void print_face (typename Arrangement::Face_const_handle f)
{
// Print the outer boundary.
if (f->is_unbounded())
{
std::cout << "Unbounded face. " << std::endl;
}
else
{
std::cout << "Outer boundary: ";
print_ccb<Arrangement> (f->outer_ccb());
}
// Print the boundary of each of the holes.
typename Arrangement::Hole_const_iterator hole;
int index = 1;
for (hole = f->holes_begin(); hole != f->holes_end(); ++hole, ++index)
{
std::cout << " Hole #" << index << ": ";
print_ccb<Arrangement> (*hole);
}
// Print the isolated vertices.
typename Arrangement::Isolated_vertex_const_iterator iv;
for (iv = f->isolated_vertices_begin(), index = 1;
iv != f->isolated_vertices_end(); ++iv, ++index)
{
std::cout << " Isolated vertex #" << index << ": "
<< "(" << iv->point() << ")" << std::endl;
}
return;
}
//-----------------------------------------------------------------------------
// Print the given arrangement.
//
template<class Arrangement>
void print_arrangement (const Arrangement& arr)
{
// CGAL_precondition (arr.is_valid());
// Print the arrangement vertices.
typename Arrangement::Vertex_const_iterator vit;
std::cout << arr.number_of_vertices() << " vertices:" << std::endl;
for (vit = arr.vertices_begin(); vit != arr.vertices_end(); ++vit)
{
std::cout << "(" << vit->point() << ")";
if (vit->is_isolated())
std::cout << " - Isolated." << std::endl;
else
std::cout << " - degree " << vit->degree() << std::endl;
}
// Print the arrangement edges.
typename Arrangement::Edge_const_iterator eit;
std::cout << arr.number_of_edges() << " edges:" << std::endl;
for (eit = arr.edges_begin(); eit != arr.edges_end(); ++eit)
std::cout << "[" << eit->curve() << "]" << std::endl;
// Print the arrangement faces.
typename Arrangement::Face_const_iterator fit;
std::cout << arr.number_of_faces() << " faces:" << std::endl;
for (fit = arr.faces_begin(); fit != arr.faces_end(); ++fit)
print_face<Arrangement> (fit);
return;
}
#endif
The input polygon:
input polygon
The input file has in each line the polygon vertex id, followed by its x- and-coordinates. The query point is the one with id 1716.
Could anyone help me with this issue?
Thank you all in advance.
I have tried below code snippets to print map of map values but I could not able to access second map values.
#include <iostream>
#include <iterator>
#include <map>
#include <string>
using namespace std;
int main()
{
map< string, std::map<std::string, int> > someStorage;
//First key values
someStorage["key1"]["This Is Layer one"] = 100;
someStorage["Key1"]["This Is Layer Two"] = 120;
//second key, values
someStorage["key2"]["This Is Layer one"] = 110;
someStorage["key2"]["This Is Layer Two"] = 110;
map< string, std::map<std::string, int> > ::iterator itr;
cout << "\nThe map is : \n";
for (itr = someStorage.begin(); itr != someStorage.end(); ++itr)
{
cout << '\t' << itr->first;
//<< '\t' << (itr->second).first << '\n' <==problematic part
//<< '\t' << (itr->second).second << '\n'; <==problematic part
}
cout << endl;
return 0;
}
How to print/access these values and how do I differentiate "This Is Layer
one" for "key1" and "key2". Because I can see that it is getting
overwritten if we assign key2 value, key1 has same. Why?
Also I am expecting below key value pairs
Key1 => {This Is Layer one, 100}
{This Is Layer Two, 120}
Key2 =>{This Is Layer one, 110}
{This Is Layer Two, 110}
.
In addition to the other answers here, you can use structured binding (since c++17) to simplify this:
for (auto const& [key, val] : someStorage) { // val = second map
for (auto const& [k, v] : val) { // k = first, v = second
cout << key << ' ' << k << ' ' << v << '\n';
}
}
You need a second, inner loop to traverse the nested std::map. Like this:
for (auto itr = someStorage.cbegin(); itr != someStorage.cend(); ++itr)
{
cout << itr->first << " => ";
for (auto innerItr = itr->second.cbegin(); innerItr != itr->second.cend(); ++innerItr)
{
cout << innerItr->first << " : " << innerItr->second << " ";
}
cout << "\n";
}
Note that for the desired output, you need to capitalize the keys such that they are "Key1" and "Key2" (this is currently a typo in your question). Note further that I changed to begin/end member functions to cbegin/cend, as the loop doesn't modify the container.
You'll need to iterate over your inner map as well, something like:
for (auto itr1 = someStorage.begin(); itr1 != someStorage.end(); ++itr1)
{
cout << '\t' << itr->first << ":\n";
for (auto itr2 = itr1->second.begin(); itr2 != itr1->second.end(); ++itr2)
{
cout << "\t\t" << itr2->first << '\n';
cout << "\t\t" << itr2->second << '\n';
}
}
Thank you for the output. My Gcc version did not support auto iterate
for (itr1 = someStorage.begin(); itr1 != someStorage.end(); ++itr1)
{
cout << '\t' << itr1->first << ":\n";
std::map<std::string, int> ::iterator itr2;
for (itr2 = itr1->second.begin(); itr2 != itr1->second.end(); ++itr2)
{
cout << "\t\t" << itr2->first << '\n';
cout << "\t\t" << itr2->second << '\n';
}
}
First part of the question is that I am trying to use boost::bimap, but from the documentation it is unclear to me how to define a bidirectional multimap.
The second part of the question is that I need it to be a map in one direction and a multimap in the other direction, can this be done using boost::bimap ?
Has anyone experience of this or can point me to the correct page ?
All is in documentation... http://www.boost.org/doc/libs/1_51_0/libs/bimap/doc/html/boost_bimap/the_tutorial/discovering_the_bimap_framework.html
typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;
Example.
#include <iostream>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>
namespace bimaps = boost::bimaps;
int main()
{
typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;
typedef bimap_t::value_type value_type;
bimap_t bimap;
bimap.insert(value_type(1, 1));
bimap.insert(value_type(1, 2));
auto& left = bimap.left;
auto it = left.find(1);
std::cout << "LEFT" << std::endl;
for (; it != left.end(); ++it)
{
std::cout << it->first << " " << it->second << std::endl;
}
auto& right = bimap.right;
auto r_it = right.find(2);
std::cout << "RIGHT" << std::endl;
for (; r_it != right.end(); ++r_it)
{
std::cout << r_it->first << " " << r_it->second << std::endl;
}
}
The answer from ForEverR is partially correct for the first part of your question (how to define a bidirectional multimap?).
For the second part (accessing a bimap which is a map in one direction and a multimap in the other direction) It is not correct.
A correct way to access would be [ http://rextester.com/BXBDHN12336 ]:
//bimap operations
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>
int main()
{
typedef boost::bimap<boost::bimaps::multiset_of<int>, boost::bimaps::set_of<int>> bimap_t;
typedef bimap_t::value_type value_type;
bimap_t bimap;
bimap.insert(value_type(1, 1));
bimap.insert(value_type(10, 50));
bimap.insert(value_type(1, 2));
bimap.insert(value_type(9, 15));
typedef bimap_t::left_const_iterator l_itr_t;
typedef std::pair<l_itr_t,l_itr_t> l_itr_range_t;
l_itr_range_t ii = bimap.left.equal_range(1);
std::cout << "LEFT" << std::endl;
for(l_itr_t it = ii.first; it != ii.second; ++it)
{
std::cout << "Key = " << it->first << " Value = " << it->second << std::endl;
}
std::cout << "RIGHT" << std::endl;
std::cout << "Key = " << 1 << " Value = " << bimap.right.at(1) << std::endl;
}
stdout:
LEFT
Key = 1 Value = 1
Key = 1 Value = 2
RIGHT
Key = 1 Value = 1
The example from ForEverR 'seems' to work because of the order of the data inserted but check out the results when you insert another couple at the end bimap.insert(value_type(9, 15));:
#include <iostream>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>
namespace bimaps = boost::bimaps;
int main()
{
typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;
typedef bimap_t::value_type value_type;
bimap_t bimap;
bimap.insert(value_type(1, 1));
bimap.insert(value_type(1, 2));
bimap.insert(value_type(9, 15));
auto& left = bimap.left;
auto it = left.find(1);
std::cout << "LEFT" << std::endl;
for (; it != left.end(); ++it)
{
std::cout << it->first << " " << it->second << std::endl;
}
auto& right = bimap.right;
auto r_it = right.find(2);
std::cout << "RIGHT" << std::endl;
for (; r_it != right.end(); ++r_it)
{
std::cout << r_it->first << " " << r_it->second << std::endl;
}
}
stdout:
LEFT
1 1
1 2
9 15
RIGHT
2 1
15 9
I have a map declared as follows:
map < string , list < string > > mapex ; list< string > li;
How can I display the items stored in the above map on the console?
Update (Back to the future): with C++11 range-based for loops –
std::map<Key, Value> m { ... /* initialize it */ ... };
for (const auto &p : m) {
std::cout << "m[" << p.first << "] = " << p.second << '\n';
}
Well it depends on how you want to display them, but you can always iterate them easily:
typedef map<string, list<string>>::const_iterator MapIterator;
for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++)
{
cout << "Key: " << iter->first << endl << "Values:" << endl;
typedef list<string>::const_iterator ListIterator;
for (ListIterator list_iter = iter->second.begin(); list_iter != iter->second.end(); list_iter++)
cout << " " << *list_iter << endl;
}
I'd try the following
void dump_list(const std::list<string>& l) {
for ( std::list<string>::const_iterator it = l.begin(); l != l.end(); l++ ) {
cout << *l << endl;
}
}
void dump_map(const std::map<string, std::list<string>>& map) {
for ( std::map<string,std::list<string>>::const_iterator it = map.begin(); it != map.end(); it++) {
cout << "Key: " << it->first << endl;
cout << "Values" << endl;
dump_list(it->second);
}
I'm a little off topic here...
I guess you want to dump the map content for debugging. I like to mention that the next gdb release (version 7.0) will have a built in python interpreter which will be used by the gcc libstdc++ to provide stl pretty printers. Here is an example for your case
#include <map>
#include <map>
#include <list>
#include <string>
using namespace std;
int main()
{
typedef map<string, list<string> > map_type;
map_type mymap;
list<string> mylist;
mylist.push_back("item 1");
mylist.push_back("item 2");
mymap["foo"] = mylist;
mymap["bar"] = mylist;
return 0; // stopped here
}
which results in
(gdb) print mymap
$1 = std::map with 2 elements = {
["bar"] = std::list = {
[0] = "item 1",
[1] = "item 2"
},
["foo"] = std::list = {
[0] = "item 1",
[1] = "item 2"
}
}
Yay!
Another form, using <algorithm>:
void printPair(const pair<string, list<string> > &p)
{
cout << "Key: " << p.first << endl;
copy(p.second.begin(), p.second.end(), ostream_iterator<string>(cout, "\n"));
}
for_each(mapex.begin(), mapex.end(), printPair);
Test program:
#include <iostream>
#include <map>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
void printPair(const pair<string, list<string> > &p)
{
cout << "Key: " << p.first << endl;
copy(p.second.begin(), p.second.end(), ostream_iterator<string>(cout, "\n"));
}
int main()
{
map<string, list<string> > mapex;
list<string> mylist1;
mylist1.push_back("item 1");
mylist1.push_back("item 2");
mapex["foo"] = mylist1;
list<string> mylist2;
mylist2.push_back("item 3");
mylist2.push_back("item 4");
mylist2.push_back("item 5");
mapex["bar"] = mylist2;
for_each(mapex.begin(), mapex.end(), printPair);
}
You can write a quite generic overloaded function, which is good for two purposes:
It works with any map.
It allows for using <<.
The function is
template<class key_t, class value_t>
ostream& operator<<(ostream& os, const map<key_t, value_t>& m) {
for (typename map<key_t, value_t>::const_iterator it = m.begin();
it != m.end(); it++) {
os << "Key: " << it->first << ", Value: " << it->second;
}
return os;
}
cout << will work with any map for which << is defined for typenames key_t and value_t. In your case, this is not defined for value_t (= list<string>), so you also have to define it.
In a similar spirit, you can use
template<class T>
ostream& operator<<(ostream& os, const list<T>& l) {
for (typename list<T>::const_iterator it = l.begin(); it != l.end(); it++) {
os << "\"" << *it << "\", ";
}
return os;
}
So, you may:
Add these two functions.
Add the prototypes where needed.
Use using namespace std; (or add std:: as needed).
Use, e.g.,
cout << mapex << endl;
cout << li << endl;
Remember that if there is any other viable candidate for the <<s just defined (which I take there is not, otherwise you would likely not ask this question), it may take precedence over the present ones.
If you can use C++11 features, then I think range-based for loops as proposed in The Paramagnetic Croissant's answer provide the most readable option. However, if C++17 is available to you, then you can combine those loops with structured bindings to further increase readability, because you no longer need to use the first and second members. For your specific use case, my solution would look as follows:
std::map<std::string, std::list<std::string>> mapex;
mapex["a"] = { "1", "2", "3", "4" };
mapex["b"] = { "5", "6", "7" };
for (const auto &[k, v] : mapex) {
std::cout << "m[" << k.c_str() << "] =";
for (const auto &s : v)
std::cout << " " << s.c_str();
std::cout << std::endl;
}
Output:
m[a] = 1 2 3 4
m[b] = 5 6 7
Code on Coliru