Map contains value as a list + how to print in C ++

I have a map containing strings as keys and lists of file names as values. ex: Map(firstDir, list(file1,file2,file3))

I know that using the following code, I can print a value that has a String

 { cout << "Key: " << pos->first << endl; cout << "Value:" << pos->second << endl; } 

But if pos->second contains a list, how to display?

+4
source share
7 answers

operator << overload for list

 std::ostream& operator << (std::ostream& out, std::list<ListElemType> const& lst) { for(std::list<ListElemType>::iterator it = lst.begin(); it != lst.end(); ++it) { if(it != lst.begin()) out << /*your delimiter*/; out << *it; } return out; } 

now you can do what you want

 cout << "Key: " << pos->first << endl << "Value:" << pos->second << endl; 
+7
source

How to use Boost.Foreach?

 #include <boost/foreach.hpp> { cout << "Key: " << pos->first << endl; cout << "Values:" << endl; BOOST_FOREACH(std::string const& file, pos->second) { cout << "\t" << file << endl; } } 
+2
source

The first thing you will need to decide is how do you want the list to be displayed? Perhaps separated by commas or each entry on a new line? Then you can overload the stream output statement for line lists:

 std::ostream & operator<<(std::ostream & stream, const std::list<std::string> & object) { std::copy(object.begin(), object.end(), std::ostream_iterator<std::string>(std::cout, ", ") } 

This statement will be called every time you write am std::list<std::string> to any output stream, and it will print the list values ​​separated by commas.

+2
source

Use a library that overloads stream inserts for containers, such as my example :

 void example(MapType const &m) { using namespace kniht::container_inserters; // must be enabled in this scope MapType::const_iterator x = m.begin(); cout << *x << '\n'; // can print the pair directly cout << "Key: " << x->first << '\n'; // or format it yourself cout << "Value: " << x->second << '\n'; // output for a list: [a, b, c] } 

You can extract the used functions from my header or just copy it elsewere (it is standalone, but has other utilities).

+2
source

Since you are using STL, the easiest way to print such a structure is as follows:

 #include <iostream> #include <map> #include <list> #include <string> using namespace std; int main() { map<string, list<string>> sample; ... //fill the map for (auto itr : sample){ cout << itr.first << ":\t" << endl; for (auto innerItr : sample.second) cout << innerItr << " "; cout << endl; } } 
+1
source

What you want to do is create a template that can output any print sequence, that is, a sequence of printable elements. You should also be able to customize how you start, end, and limit the sequence.

Create your own wrapper class. Now, since this is your shell, you can overload the <<operator happily on it without interfering with the external namespace.

You can use boost :: range in your constructor, or templated being / end sequence or templated collection.

Also take your separators as parameters. You can have reasonable defaults if you want,

Finally, when you want to print one, you simply do something like:

 std::cout << MySequenceFormatter( seq.begin(), seq.end(), delim, startofSeq, endOfSeq ) << std::endl; 

When displaying cards, you can use boost :: transform_iterator between them to convert each std :: pair when repeating a sequence. Then you can also have your own format. In fact, you can do this for any sequence in which you want to use your own method to print the elements themselves.

0
source

Strait version (-:

 #include <map> #include <list> #include <string> #include <iostream> #include <sstream> // only for generating testdata typedef std::list<std::string> TStringList; typedef std::map<std::string, TStringList> TStringListMap; TStringListMap myMap; int main() { // Generating testdata for (int i=0;i<10;i++) { std::stringstream kstr; kstr << i; std::string key = kstr.str(); for (int ii=0;ii<=i;ii++) { std::stringstream vstr; vstr << ii; myMap[key].push_back( vstr.str() ); } } //Print map for ( TStringListMap::const_iterator it = myMap.begin(), end = myMap.end(); it != end; ++it ) { std::cout << it->first<< ": "; for( TStringList::const_iterator lit = it->second.begin(), lend = it->second.end(); lit != lend; ++lit ) { std::cout << *lit << " "; } std::cout << std::endl; } return 0; } 

Output:

 0: 0 1: 0 1 2: 0 1 2 3: 0 1 2 3 4: 0 1 2 3 4 5: 0 1 2 3 4 5 6: 0 1 2 3 4 5 6 7: 0 1 2 3 4 5 6 7 8: 0 1 2 3 4 5 6 7 8 9: 0 1 2 3 4 5 6 7 8 9 
0
source

All Articles