-
setw related
I am trying to output some info and in the legacy code, they used setw. I am trying to match that..
Code:
std::ostringstream os;
os << "Name QualityIndicator MaxUlBitRate MaxDlBitRate nGuarUlBitRate nGuarDlBitRate" << std::endl;
for (; itr != end; ++itr)
{
Data = *p_itr;
os << p_itr.sName << setw(11) << p_itr.nQualityIndicator << setw(30) << p_itr.nMaxUlBitRate
<< setw(53) << p_itr.nMaxDlBitRate << setw(78) << p_itr.nGuarUlBitRate
<< setw(102) << p_itr.nGuarDlBitRate << endl;
}
Is there any suggestions to make this better..I somehow feel setw is not the choice, but may be the legacy code chose this for some reason..May be because i am not a c++ expert, not able to see it.
thanks a lot
~p
-
Re: setw related
itr is being incremented within the for loop but is not being used within the loop. Also Data is being assigned within the loop but again not used. p_itr is suggesting it is a pointer but is not changed within the loop and is not being accessed as a pointer :confused:
How is p_itr related to itr? What is end? Where is itr initialised? What is the type of itr and p_itr?
-
Re: setw related
@kaud: sorry it was my mistake, I had tried to just show the kind of psedocode ..
I will add the almost the code now I have changed and works fine.
Code:
Responses::Pc::List const& pcList = dynamic_cast<Responses::Pc::List const&>(msg);
os << "TitleaName Qua MaxUlBitRate MaxDlBitRate nGuarUlBitRate nGuarDlBitRate" << std::endl;
std::vector<Data::Pc::Data>::const_iterator itr = pcList.data.dataItems.begin();
std::vector<Data::Pc::Data>::const_iterator end = pcList.data.dataItems.end();
for (; itr != end; ++itr)
{
OAM::Data::Pc::Data pcData = *itr;
os << left << setw(28) << pcData.sTitleaName << setw(10) << pcData.nQua << setw(23)<<pcData.nMaxUlBitRate
<<setw(24)<< pcData.nMaxDlBitRate<<setw(24)<<pcData.nGuarUlBitRate<<setw(22)<<pcData.nGuarDlBitRate << endl;
}
-
Re: setw related
Seems good to me. If you are using cout, you don't have much of a choice, and need to use stream formatting anyways. It's not pretty, but it does work and is pretty robust. I'd leave the code as-is unless you have a good reason to touch it. All it it doing is printing numbers left aligned to certain widths. Though arguably, the last setw is not needed. Also still arguably, the original code *could* have considered using a more robust tab separation scheme, or better yet, csv-like scheme. I don't have the context.
If you *do* want to put effort into upgrading your app's stream and string formatting facilities, consider using instead The Boost Format library. In particular, you *could* be interested in its absolute tabulation scheme. The equivalent code would look like this:
cout << format("%s %|28t|%s %|38t|%s" %|71t|%s %|95t|%s %|119t|%s \n")
% pcData.sTitleaName % pcData.nQua % pcData.nMaxUlBitRate % pcData.nMaxDlBitRate % pcData.nGuarUlBitRate % pcData.nGuarDlBitRate;
Some differences you'll notice here is that this format guarantees at least 1 space between each word. Furthermore, if a "word" goes past its width boundaries, the following words will not be padded, and hopefully, re-align after a few columns.
-
Re: setw related
Thanks a lot Monarch for the inputs.
Yes I will try going through the boost format , thanks a lot for the link