iostream use of << to construct string - c++

How can << be used to construct a string ala
int iCount;
char szB[128];
sprintf (szB,"%03i", iCount);

using namespace std;
stringstream ss;
ss << setw(3) << setfill('0') << iCount;
string szB = ss.str();

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
int iCount = 42;
ostringstream buf;
buf << setw(3) << setfill('0') << iCount;
string s = buf.str();
cout << s;
}

How can << be used to construct a string ala
This doesn't make any sense.
Use std::ostringstream in C++ if you want to do the similar thing.
std::ostringstream s;
int x=<some_value>;
s<< std::setw(3) << std::setfill('0') <<x;
std::string k=s.str();

Related

string hex to pointer get value

From the console i am asking for a hexadecimal string to convert to a pointer to reference an item in memory.
#include <iostream>
#include <sstream>
#include <Windows.h>
int char_to_pointer(std::string input);
int main() {
int sample = 100; // lets say this address is 0xc1f1
std::string input_;
std::cout << "addr:" << &sample << std::endl;
std::cout << "what is the memory address?:" << std::endl;
std::cin >> input_;
unsigned int inp = char_to_pointer(input_);
std::cout << "imp: " << inp << std::endl;
Sleep(10000);
return 0;
}
int char_to_pointer(std::string input) {
return std::stoul(input, nullptr, 16);
}
My problem is that char_to_pointer only converts the hex string into a decimal.
this is what i want:
input: "0xc1f1"
output: 100
I found the solution:
#include <iostream>
#include <sstream>
#include <Windows.h>
#include <string>
int *char_to_pointer(std::string input);
int main() {
int sample = 100; // lets say this address is 0xc1f1
std::string input_;
std::cout << "addr:" << &sample << std::endl;
std::cout << "what is the memory address?:" << std::endl;
std::cin >> input_;
int *inp = char_to_pointer(input_);
std::cout << "imp: " << inp << std::endl;
std::cout << "imp*: " << *inp << std::endl;//This was my solution
std::cout << "imp&: " << &inp << std::endl;
Sleep(10000);
return 0;
}
int *char_to_pointer(std::string input) {
return (int *)std::stoul(input, nullptr, 16);
}

stringstream loses precision while converting

I have a small problem with stringstream. It loses precision when I use it to convert string to double.
const std::string str = "44.23331002";
double x;
stringstream ss;
ss << str;
ss >> x;
cout << str << " = " << x << endl;
The output is:
44.23331002 = 44.2333
Why is this? Does it convert to float and has limited digit precision?
You need to set the precision on the output stream:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
const std::string str = "44.23331002";
double x;
stringstream ss;
ss << str;
ss >> x;
cout << str << " = " << std::setprecision(10) << x << endl;
}
Output:
44.23331002 = 44.23331002
(Demo)
calcto answered correctly.and You can change precision in two way:
std::setprecision(10) and std::cout.precision(10)

How do you parse a string with numbers and chars and separate the numbers and chars if there is no spaces in the string?

I have a string
123test
how do I separate 123 and test and store the in two variables?
Use sscanf, for example:
char str[] = "123test";
char str2[10];
int i;
sscanf(str, "%d%s", &i, str2);
Use the tools provided by C++: strings and streams.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
int num;
std::string str;
std::istringstream ss{"123test"};
ss >> num >> str;
std::cout << "num = " << num << std::endl;
std::cout << "str = " << str << std::endl;
return 0;
}
Link to demo

The easiest way to read formatted input in C++?

Is there any way to read a formatted string like this, for example :48754+7812=Abcs.
Let's say I have three stringz X,Y and Z, and I want
X = 48754
Y = 7812
Z = Abcs
The size of the two numbers and the length of the string may vary, so I dont want to use substring() or anything like that.
Is it possible to give C++ a parameter like this
":#####..+####..=SSS.."
so it knows directly what's going on?
#include <iostream>
#include <sstream>
int main(int argc, char **argv) {
std::string str = ":12341+414112=absca";
std::stringstream ss(str);
int v1, v2;
char col, op, eq;
std::string var;
ss >> col >> v1 >> op >> v2 >> eq >> var;
std::cout << v1 << " " << v2 << " " << var << std::endl;
return 0;
}
A possibility is boost::split(), which allows the specification of multiple delimiters and does not require prior knowledge of the size of the input:
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
int main()
{
std::vector<std::string> tokens;
std::string s(":48754+7812=Abcs");
boost::split(tokens, s, boost::is_any_of(":+="));
// "48754" == tokens[0]
// "7812" == tokens[1]
// "Abcs" == tokens[2]
return 0;
}
Or, using sscanf():
#include <iostream>
#include <cstdio>
int main()
{
const char* s = ":48754+7812=Abcs";
int X, Y;
char Z[100];
if (3 == std::sscanf(s, ":%d+%d=%99s", &X, &Y, Z))
{
std::cout << "X=" << X << "\n";
std::cout << "Y=" << Y << "\n";
std::cout << "Z=" << Z << "\n";
}
return 0;
}
However, the limitiation here is that the maximum length of the string (Z) must be decided before parsing the input.
You can use scanf. It is not overly C++ - ish, but it does the trick with remarkably few lines of code:
char a[101], b[111], c[121];
sscanf(":48754+7812=Abcs", ":%100[^+]+%110[^=]=%120s", a, b, c);
string sa(a), sb(b), sc(c);
cout << sa << "-" << sb << "-" << sc << endl;
The idea is to specify the characters accepted by the strings that you read using a very limited regular expression syntax. In this case, the first string is read up to the plus, and the second string is read up to the equals sign.
for example.
#include <boost/regex.hpp>
#include <iostream>
int main()
{
boost::regex re("\":(\\d+)\\+(\\d+)=(.+)\"");
std::string example = "\":48754+7812=Abcs\"";
boost::smatch match;
if (boost::regex_match(example, match, re))
{
std::cout << "f number: " << match[1] << " s number: " << match[2] << " string: " << match[3]
<< std::endl;
}
else
{
std::cout << "not match" << std::endl;
}
}
and second variant, work only with string.
#include <string>
#include <iostream>
int main()
{
std::string s = "\":48754+7812=Abcs\"";
std::string::size_type idx = s.find(":");
std::string::size_type end_first = s.find("+", idx + 1);
std::string f_number = s.substr(idx + 1, end_first - (idx + 1));
std::cout << f_number << std::endl;
std::string::size_type end_second = s.find("=", end_first + 1);
std::string s_number = s.substr(end_first + 1, end_second - (end_first + 1));
std::cout << s_number << std::endl;
std::string::size_type string_end = s.find("\"", end_second);
std::string str = s.substr(end_second + 1, string_end - (end_second + 1));
std::cout << str << std::endl;
}

How to turn std::string length into hex and than back into string?

So... I want to create simple HTTP Chunked transfer encoding prototype. I have messages as std::strings. And my server API is all string based... so I wonder how to turn std::string length into hex and than back into string?
So say we had std::string("This is the data in the first chunk\r\n").length() that would return say int 37. I want to convert it into hex 0x25 and than to get out from that hex std::string("25"). How to do such thing (using stl and boost)?
#include <sstream>
#include <iomanip>
#include <iostream>
int main() {
// To string:
std::ostringstream oss;
oss << std::hex << 37;
std::string result = oss.str();
std::cout << result << '\n';
// Back from string (if you need it):
std::istringstream iss(result);
int original;
if (!(iss >> std::hex >> original)) {
// handle error here
} else {
std::cout << original << '\n';
}
}
std::stringstream buffer;
buffer << std::hex << your_number;
buffer.str() will now give you a hex-representation of your number; If you want the 0x before the number, use this:
buffer << std::hex << showbase << your_number;
#include <sstream>
#include <iomanip>
std::ostringstream str;
str << "0x" << std::hex << length;
std::string result = str.str();
Demonstrated here.
Stringstreams are one way:
std::ostringstream ss;
ss << "0x" << std::hex << 12345;
std::string aString = ss.str();
An alternative is the all-mighty boost::format.
This would be a solution:
std::string convert_length_to_hex(const std::string& str)
{
std::ostringstream result;
result << std::hex << str.length();
return result.str();
}
Here is my example of dec to hex conversion and then back
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
std::string dec2hex(int dec){
std::string result;
std::stringstream ss;
ss << std::hex << dec;
ss >> result;
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;}
int hex2dec(std::string& hex){
int x;
std::stringstream ss;
ss << std::hex << hex;
ss >> x;
return x;
}
int main()
{
std::string hex = "ABCD";
int dec = hex2dec(hex);
std::cout << dec << std::endl;
std::cout << dec2hex(dec) << std::endl;
return 0;
}