I want my zeros to be some other number/character (C++) - c++

double floaty=36.6736872;
cout<<fixed<<setprecision(10)<<floaty;
My output is "36.6736872000";
I want my zeros to be some other number.
Eg: If I want zeros to be ^.
then the output should be 36.6736872^^^
I don't have any idea other than using setw and setfill to get my desired output in single line of code

You can use std::ostringstream, and change the resulting string in any way you see fit:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
int main()
{
double floaty=36.6736872;
std::ostringstream strm;
// Get the output as a string
strm << std::fixed << std::setprecision(10) << floaty;
std::string out = strm.str();
// Process the output
auto iter = out.rbegin();
while (iter != out.rend() && *iter == '0')
{
*iter = '^';
++iter;
}
std::cout << out;
}
Output:
36.6736872^^^

Related

Build incremental std::string

I try to build an std::string in the form of "start:Pdc1;Pdc2;Pdc3;"
With following code I can build the repeated "Pdc" and the incremental string "123" but I'm unable to combine the two strings.
#include <string>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iterator>
#include <numeric>
int main()
{
std::ostringstream ss;
std::string hdr("start:");
std::fill_n(std::ostream_iterator<std::string>(ss), 3, "Pdc;");
hdr.append(ss.str());
std::string v("abc");
std::iota(v.begin(), v.end(), '1');
std::cout << hdr << std::endl;
std::cout << v << std::endl;
std::cout << "Expected output: start:Pdc1;Pdc2;Pdc3;" << std::endl;
return 0;
}
How can I build this string? Preferable without a while or for loop.
The expected output is: start:Pdc1;Pdc2;Pdc3;
std::strings can be concatenated via their operator+ (or +=) and integers can be converted via std::to_string:
std::string res("start:");
for (int i=0;i<3;++i){
res += "Pdc" + std::to_string(i+1) + ";";
}
std::cout << res << "\n";
If you like you can use an algorithm instead of the handwritten loop, but it will still be a loop (your code has 2 loops, but only 1 is needed).
Code to generate your expected string, though with a small for loop.
#include <iostream>
#include <string>
#include <sstream>
std::string cmd(const std::size_t N)
{
std::ostringstream os;
os << "start:";
for(std::size_t n = 1; n <= N; ++n) os << "Pdc" << n << ";";
return os.str();
}
int main()
{
std::cout << cmd(3ul);
return 0;
}

c++ : istream_iterator skip spaces but not newline

Suppose I have
istringstream input("x = 42\n"s);
I'd like to iterate over this stream using std::istream_iterator<std::string>
int main() {
std::istringstream input("x = 42\n");
std::istream_iterator<std::string> iter(input);
for (; iter != std::istream_iterator<std::string>(); iter++) {
std::cout << *iter << std::endl;
}
}
I get the following output as expected:
x
=
42
Is it possible to have the same iteration skipping spaces but not a newline symbol? So I'd like to have
x
=
42
\n
std::istream_iterator isn't really the right tool for this job, because it doesn't let you specify the delimiter character to use. Instead, use std::getline, which does. Then check for the newline manually and strip it off if found:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::istringstream input("x = 42\n");
std::string s;
while (getline (input, s, ' '))
{
bool have_newline = !s.empty () && s.back () == '\n';
if (have_newline)
s.pop_back ();
std::cout << "\"" << s << "\"" << std::endl;
if (have_newline)
std::cout << "\"\n\"" << std::endl;
}
}
Output:
"x"
"="
"42"
"
"
If you can use boost use this:
boost::algorithm::split_regex(cont, str, boost::regex("\s"));
where "cont" can be the result container and "str" is your input string.
https://www.boost.org/doc/libs/1_76_0/doc/html/boost/algorithm/split_regex.html

iomanip set at most 4 digits after decimal point

I want to print to screen some numbers with at most 4 digits after decimal point using iomanip.
I've learned that in default mode setprecision counts not only the digits after decimal point but also the digits in the integer part. This code
#include <iostream>
#include <iomanip>
int main () {
double numbers[] = {3.141516, 1.01, 200.78901, 0.12345};
int len = sizeof(numbers) / sizeof(numbers[0]);
std::cout << std::setprecision(4);
for (int i = 0; i < len; ++i) {
std::cout << numbers[i] << '\n';
}
return 0;
}
outputs:
3.142
1.01
200.8
0.1235
But what I want is: (at most 4 digits after decimal point without trailing zeros)
3.1415
1.01
200.789
0.1235
Is iomanip capable of doing this? Without using other tricks (like round)?
EDIT
It seems that I haven't made it clear enough. My question is iomanip specific
All I want to know is whether iomanip is capable of doing things I've described because iomanip is said to be the standard library for input/output manipulators. The posted question is
Is iomanip capable of doing this?
It's more like "is it supported" rather than "give me any solution".
I have searched it again, looked up iomanip references hoping for a clean and compact way to format floating numbers for at most n digits, using unnecessary libraries as little as possible.
And there seems to be no standard way to achieve this.
One (ugly) option to obtain OP's desired output is to represent the number with the wanted maximum precision and then just remove the unwanted zeroes:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
int main()
{
std::vector<double> numbers {
3.141516, 1.01, 200.78901, 0.12345, 9.99999
};
for (auto x : numbers)
{
// "print" the number
std::stringstream ss;
ss << std::fixed << std::setprecision(4) << x;
// remove the unwanted zeroes
std::string result{ss.str()};
while (result.back() == '0')
result.pop_back();
// remove the separator if needed
if (result.back() == '.')
result.pop_back();
std::cout << result << '\n';
}
std::cout << "\nJustified:\n";
for (auto x : numbers)
{
// "print" the number
std::stringstream ss;
ss << std::fixed << std::setprecision(4) << std::setw(15) << x;
// remove the unwanted zeroes
std::string result{ss.str()};
auto it = result.rbegin();
while (*it == '0')
*it++ = ' ';
// remove the separator if needed
if (*it == '.')
*it = ' ';
std::cout << result << '\n';
}
}
Live example: https://ideone.com/8zP17O
So:
in std::fixedfield mode, std::setprecision sets the maximum number of significant figures, not decimal places;
if you flip to std::fixed it means exact number of decimal places;
C++ does not provide any analogue to the %.Nf printf format string! (up to N decimal places)
I've just thrown together a little fake I/O manipulator that can get the behaviour we both want. It's not terribly performant, but it does the job:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cstdio>
struct sprintf_wrapper
{
sprintf_wrapper(std::string fmt_str)
: _fmt_str(std::move(fmt_str))
{}
struct proxy
{
proxy(const sprintf_wrapper& wrapper, std::ostream& os)
: _wrapper(wrapper)
, _os(os)
{}
std::ostream& Resolve(const double value) const
{
// First find out how many characters we're going to need
const int len = std::snprintf(nullptr, 0, _wrapper._fmt_str.c_str(), value);
if (len < 0)
{
_os.setstate(std::ios::failbit);
return _os;
}
// Then allocate a buffer
std::string result;
result.resize(len);
// Actually serialise the value according to the format string
if (std::sprintf(result.data(), _wrapper._fmt_str.c_str(), value) < 0)
{
_os.setstate(std::ios::failbit);
return _os;
}
// Stream it out
_os.write(result.data(), result.size());
return _os;
}
friend std::ostream& operator<<(const proxy& obj, const double val)
{
return obj.Resolve(val);
}
private:
const sprintf_wrapper& _wrapper;
std::ostream& _os;
};
friend proxy operator<<(std::ostream& os, const sprintf_wrapper& obj)
{
return proxy(obj, os);
}
private:
std::string _fmt_str;
};
inline auto sprintf_f(size_t n, const bool showpos = false)
{
std::stringstream fmt;
fmt << '%';
if (showpos) fmt << '+';
fmt << '.' << n << 'f';
return sprintf_wrapper(fmt.str());
}
int main()
{
std::cout << sprintf_f(2) << 4.123456789 << '\n';
std::cout << sprintf_f(3) << 4.123456789 << '\n';
std::cout << sprintf_f(4) << 4.123456789 << '\n';
std::cout << sprintf_f(5) << 4.123456789 << '\n';
std::cout << sprintf_f(6) << 4.123456789 << '\n';
}
(live demo)
Use a combination of std::fixed and std::setprecision.
#include <iomanip>
#include <iostream>
int main() {
double d = 311.3456;
std::cout << std::fixed;
std::cout << std::setprecision(4);
std::cout << d << std::endl;
}

Limiting decimal places of a float when converting to string in C++

I'm making a function which prints the elements of a std::vector<float>.
Working code:
std::vector<float> components { 1, 2, 3 };
string result = "<";
for ( auto it = begin(this->components); it != end(this->components); ++it ) {
result.append(to_string(*it));
if (it != (this->components))
result.append(", ");
}
result.append(">");
std::cout << result;
The intended result is if "components" has elements 1,2,3, for example, it will print: <1, 2, 3>.
Right now it is printing the numbers as floats, of course, like < 1.000000, 2.000000, 3.000000, >.
Is there a way I can control how many decimal places are put into the string, without having to manually go through it character by character?
As a side note, how do I prevent it from adding a ',' after the last element?
You may use std::stringstream.precision for that.
Just create a std::stringstream convert it to a string and your done.
Like so:
stringstream ss;
ss.precision(3);
ss << "<";
for ( auto it = begin(this->components); it != end(this->components); ++it ) {
ss << *it;
if (it != (this->components))
ss << ", ";
}
ss << ">";
string result = ss.str();
you can use sprintf() before casting:
float a = 1.000000;
char aa[20];
sprintf(aa, "%1.3f", a);
Here is the complete code which i ran:
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
std::vector<float> components{ 1, 2, 3 };
string result = "<";
for (auto it = components.begin(); it != components.end(); ++it) {
float a = *it;
char aa[20];
sprintf(aa, "%1.3f", a);
result.append(string(aa));
if (it+1 != components.end())
result.append(", ");
}
result.append(">");
std::cout << result.c_str();
getchar();
return 0;
}
Output:
I would do it like this using a stringstream.
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
int main()
{
std::vector<float> components {1, 2, 3, 1.5f, 2.5f, 3.5f, 1.25f, 2.25f, 3.25f, 1.12345f};
std::stringstream result;
result << "<";
for(auto it = std::begin(components); it != std::end(components); ++it)
{
if(it != std::begin(components))
{
result << ", ";
}
result << *it;
}
result << ">";
std::cout << result.str();
return 0;
}
You could also use std::fixed and std::setprecision to modify the output further as you desire.
Printing the comma before the next item for all but the first item fixes your trailing comma issue.
Here's a demo of it working:
As #Axalo already noted, you can use setprecision with an ostream to set its precision (and it can be used with any ostream, not just cout).
To eliminate the trailing comma, I'd probably use the infix iterator I've posted elsewhere.
Using that, the code could be written something like this:
#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>
#include "infix_iterator.h"
int main () {
// Data that would display extra precision if we didn't stop it
std::vector<float> components { 1.123f, 2.234f, 3.345f };
std::ostringstream buff("<", std::ios::app);
buff << std::setprecision(2);
std::copy(components.begin(), components.end(),
infix_ostream_iterator<float>(buff, ", "));
buff << ">";
std::cout << buff.str();
}
Result: <1.1, 2.2, 3.3>

Reverse an ostringstream by line

ostringstream ss;
ss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
cout << ss.str();
should print the following:
(1,2)
(1,3)
(1,4)
How could i reverse the output by line so that it prints:
(1,4)
(1,3)
(1,2)
You could use a custom std::streambuf which internally keeps a stack of std::strings and puts them together upon using a str() member. For example:
#include <iostream>
#include <numeric>
#include <streambuf>
#include <string>
#include <vector>
class stackbuf
: public std::streambuf
{
std::vector<std::string> d_lines;
int overflow(int c) {
if (c != std::char_traits<char>::eof()) {
this->d_lines.back().push_back(c);
if (c == '\n') {
this->d_lines.push_back(std::string());
}
}
return std::char_traits<char>::not_eof(c);
}
public:
stackbuf(): d_lines(1) {}
std::string str() const {
return std::accumulate(this->d_lines.rbegin(),
this->d_lines.rend(),
std::string());
}
};
int main()
{
stackbuf sbuf;
std::ostream out(&sbuf);
out << "(1, 2)\n(1, 3)\n(1, 4)\n";
std::cout << sbuf.str();
}
For a real-world application you should, obviously, set up a buffer in the stream buffer to improve the performance. You may also want to create a custom stream directly initializing the stream's stream buffer.
Using your original code with C++98:
ostringstream ss;
ss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
cout << ss.str();
//assign a string to the contents of the ostringstream:
string rawlines = ss.str();
//now create an input stringstream with the value of the rawlines
istringstream iss(rawlines);
string temp;//just a temporary object used for storage
vector<string> lines;//this is where your lines will be held
//now iterate over the stream and store the contents into the vector `lines`:
while(getline(iss, temp)) {
lines.push_back(temp);
}
//now reverse the contents:
reverse(lines.begin(), lines.end());
//see what's inside:
for (vector<string>::const_iterator it = lines.begin(); it != lines.end(); ++it) {
cout << *it << endl;
}
This will print:
(1,4)
(1,3)
(1,2)
As desired
NOTE: This strips the newlines from the the original string.
And, this requires:
//for `getline`:
#include <cstdlib>
//for `reverse`:
#include <algorithm>
//for `string`:
#include <string>
//for `vector`:
#include <vector>
You can use reverse iterators:
std::ostringstream ss{ "(1,2)\n(1,3)\n(1,4)\n" };
std::string str = ss.str();
std::copy( str.rbegin(), str.rend(),
std::ostream_iterator<std::string>{std::cout, "\n"} );
This code will require:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <sstream>
and basic C++11 support.
This would be the classic way, best leveraging the standard C++ library.
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
int main(int argv, char* arv[])
{
ostringstream oss;
oss << "(1,2)\n" << "(1,3)\n" << "(1,4)\n" ;
cout << oss.str() << "----------\n";
// Reverse lines
// Fill an istringstream with buffer contents of the ostringstream
istringstream iss(oss.str());
stack<string> stk;
while (iss) {
string s;
if (!getline(iss, s)) break; // Read a line
s += '\n'; // Put back newline stripped by readline
stk.push(s); // Push line to stack
}
oss.clear(); // Clear state of the ostringstream
oss.str(""); // Clear contents of the ostringstream for reuse
while (!stk.empty()) {
string s;
s = stk.top(); // Get top of stack
oss << s; // Output it to the ostringstream
stk.pop(); // Pop and throw away top of stack
}
cout << oss.str();
return 0;
}