I want to print out the keys and values in a map in a organized table. I am trying to use setw and left but the output is
The 1
hello1
and I want it to be like
The 1
hello 1
what i have done so far
// System includes
#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iomanip>
/************************************************************/
Local includes
/************************************************************/
// Using declarations
using std::cout;
using std::map;
using std::unordered_map;
using std::string;
using std::cin;
using std::endl;
using std::ifstream;
using std::left;
using std::setw;
/************************************************************/
Function prototypes/global vars/typedefs
/************************************************************/
int
main (int argc, char* argv[])
{
//call the wordCount function on the user specified input file
unordered_map <string,int> exampleMap;
exampleMap["The"]++;
exampleMap["hello"]++;
cout << left;
//iterate through the map and print out the elements
for( unordered_map<string, int>::iterator i=exampleMap.begin(); i!=exampleMap.end(); ++i)
{
cout << setw(5) << (*i).first << setw(15) << (*i).second << endl;
}
return EXIT_SUCCESS;
}
you need to specify a width larger than 5(If you want the field to be larger than 5 characters). You don't need the second call to setw.
try
for( auto i=exampleMap.begin(); i!=exampleMap.end(); ++i)
{
cout << setw(8) << i->first << i->second << '\n';
}
Related
If I create a map for names and birthdays. When i enter birthdates that end in 0 it changes it the number to an octal. how do i print out the birthday ie 010525 == 4437 so when i call it -> second it will print 010525
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 010525;
birth["Dragon"] = 010266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
Output:
Chicken 4437
Dragon 4278
There are 2 ways to fix this problem:
(1) The first way is to enter the birthday without the leading 0 as follow:
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
Then use setfill() and setw() to fill the leading 0.
Please note that the code that uses std:setw() and std::setfill() will compile only with new C++ compiler versions such as C++14 (or C++11)
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << setfill('0') << setw(6) << it ->second << endl;
}
}
(2) The second way is to store the birthday as string. This way you can enter the birthday with the leading 0.
Here is the code:
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, string>birth;
birth["Chicken"] = "010525";
birth["Dragon"] = "010266";
map<string, string>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
You can go with using both as string in map like :
map<string, string>birth;
and rest keep as it is.
i'm studying C++ for C programmers course (coursera) and in module 4 there is an example for how to use istream iterators to load data to STL vector ..but when i tried the code it only printed the first number from the file. i can't find the mistake in the code.
note :the instructor didn't run the code, he Taught is using PDF. so maybe there something missing in it.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
fstream data_file("data.txt");
istream_iterator<int> start_file(data_file), end_file;
vector<int> data(start_file, end_file);
int sum = 0;
for (auto i = start_file; i != end_file; i++)
{
sum += *i;
cout << *i << endl;
}
cout << data.size()<<endl;
cout << sum << endl;
cout << (sum* 1.0) / data.size() << endl;
return 0;
}
i'm having problem with this code :
#include <iostream>
#include <math.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <stdio.h>
using std::cout; using std::cerr;
using std::cin; using std::string;
using std::endl;
int main(int argc,char* argv[])
{
for(int x = 0; x <= 2013; x++)
{
cout << "Iteration: "<< x << "\r";
cout << "";
}
return 0;
}
i need my code to print "Iteration: 0" and than just refresh that 0 to 1,2,3,4.... everything on one console line. I used the carriage return but it doesn't work,the line are printed one after another as when i use "\n". The enviroment is linux 64 bit. The IDE is eclipse 8.01.
You have to put it at the beginning of the line:
cout << "\rIteration: "<< x;
EDIT: I have tested this modification of the original OP's code and it prints what he wants. Also, Oh dear look what I've found.
Also, as suggested by #Wintermute, you can do the following inside the for loop, for better visualization:
cout << "\rIteration: "<< x << std::flush;
sleep(1);
I am playing around with containers, and at the moment trying to use a vector<vector<queue<int>>>. The form of this container is such that the 'first' vector's index is client ID, 'second' vector's index is priority level. i.e. a message of type int is pushed into a queue of certain priority, belonging to a certain client.
I am trying to find an easy way to find out if a client has any messages i.e. if any of it's priority levels has a non-empty queue. I used this simple piece of code to illustrate what I am trying to do:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
vector<vector<queue<int>>> node_pri_msg;
queue<int> pri_msg;
node_pri_msg.resize(2);
node_pri_msg[1].resize(2);
node_pri_msg[0].resize(2);
for (int i=0; i<2; i++)
{
node_pri_msg[i].push_back(pri_msg);
}
node_pri_msg[0][1].push(3);
if (node_pri_msg[1].empty())
{
cout << "empty-check succeeded" << endl;
}
}
but it does not work i.e. it seems to think that the node_pri_msg[1] is non-empty, though there are no messages in any of the queues 'belonging' to it. Is there an easy way to do this?
I think you would be better served with this:
#include <iostream>
#include <queue>
#include <map>
using namespace std;
int main()
{
typedef std::queue<int> MessageQueue;
typedef std::map<int, MessageQueue> PriorityMap;
typedef std::map<int, PriorityMap> ClientMap;
ClientMap clients;
clients[10][1].push(1);
clients[10][1].push(2);
clients[11][2].push(3);
cout << boolalpha;
cout << clients[1].empty() << endl;
cout << clients[10].empty() << endl;
cout << clients[10][0].empty() << endl;
cout << clients[10][1].empty() << endl;
cout << clients[10][1].size() << endl;
return 0;
}
Output
true
false
true
false
2
I'm playing around with boost::iostreams and the user guide talks about filter "counter". So I try it with this code:
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
namespace io = boost::iostreams;
int main()
{
io::counter cnt;
io::filtering_ostream out(cnt | io::null_sink());
out << "hello";
std::cout << cnt.lines() << " " << cnt.characters() << std::endl;
}
It always gives
0 0
which doesn't seem to be what I am expecting.
A preliminary tracing with gdb suggests the counter object that is doing the counting has a different address with object 'cnt'. I suppose it is some kind of copying in pipeline? If that's the case, how can filter "counter" be of any use?
Looking at the documentation you can use either:
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
namespace io = boost::iostreams;
int main()
{
io::counter cnt;
io::filtering_ostream out(cnt | io::null_sink());
out << "hello";
std::cout << out.component<io::counter>(0)->lines() << " " << out.component<io::counter>(0)->characters() << std::endl;
}
or:
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
#include <boost/ref.hpp>
namespace io = boost::iostreams;
int main()
{
io::counter cnt;
io::filtering_ostream out;
out.push(boost::ref(cnt));
out.push(io::null_sink());
out << "hello";
std::cout << cnt.lines() << " " << cnt.characters() << std::endl;
}