The following statement pipes all sorts of output to the console as a single string of text
std::cout << "Hi, my name is " << name_as_string << " and I am " << age_as_int << " years old, while weighing " << weight_as_double << " kilograms.";
Can we use this same syntax to build a string in a string variable? How is it done?
#include <sstream>
std::ostringstream ss;
ss << "Hi, my name is " << name_as_string;
ss << " and I am " << age_as_int << " years old, while weighing ";
ss << weight_as_double << " kilograms.";
std::string str = ss.str();
You can also use std::istringstream for multiple input, and std::stringstream for both input and output.
std::string str = "1 2 3 4 5";
std::istringstream ss(str);
int i;
while( ss >> i) {
std::cout << i;
}
stringstream will rescue you here;
#include <sstream>
std::stringstream ss;
ss << stuff << to << output;
std::string s = ss.str();
Use std::ostringstream
By using std::stringstream :
#include <sstream>
#include <iostream>
int main()
{
std::stringstream ss;
ss << "Hi, my name is " << name_as_string << " and I am " << age_as_int << " years old, while weighing " << weight_as_double << " kilograms.";
std::cout<<ss.str()<<std::endl;
}
Related
I need to convert string to hex format and append "0x" prefix to hex value.
For Example:
Input: std::string s = "0x06A4";
Output: int num = 0x06A4
I have tried this code:
{
std::stringstream ss;
std::string s = "0x06A4";
int num = std::stoi(s, 0, 16);
std::cout << "value in decimal = " << num << '\n';
std::cout << "value in hexadecimal = " << std::hex << num << '\n';
ss << "0x" << std::hex << num << '\n'; //
std::string res = ss.str();
std::cout << "result " << res << '\n';
}
#yogita, std::hex is just one of the configuration you need. You are probably missing the setfill and the setw configuration, as following:
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::stringstream ss;
std::string s = "0x06A4";
int num = std::stoi(s, nullptr, 16);
std::cout << "value in decimal = " << num << '\n';
std::cout << "value in hexadecimal = " << std::hex << num << '\n';
ss << "0x" << std::hex << std::setfill('0') << std::setw(4) <<num << '\n';
std::string res = ss.str();
std::cout << "result " << res << '\n';
return 0;
}
I want to convert a hexadecimal string to a decimal number (integer) in C++ and tried with following ways:
std::wstringstream SS;
SS << std::dec << stol(L"0xBAD") << endl;
But it returned 0 instead 2989.
std::wstringstream SS;
SS << std::dec << reinterpret_cast<LONG>(L"0xBAD") << endl;
But it returned -425771592 instead 2989.
But, when I use it like below, it works fine and gives 2989 as expect.
std::wstringstream SS;
SS << std::dec << 0xBAD << endl;
But I want to input a string and get 2989 as output, instead integer input like 0xBAD. For example, I want to input "0xBAD" and cast it to integer and then convert to a decimal number.
Thanks in advance.
// stol example
#include <iostream> // std::cout
#include <string> // std::string, std::stol
int main ()
{
std::string str_dec = "1987520";
std::string str_hex = "2f04e009";
std::string str_bin = "-11101001100100111010";
std::string str_auto = "0x7fffff";
std::string::size_type sz; // alias of size_t
long li_dec = std::stol (str_dec,&sz);
long li_hex = std::stol (str_hex,nullptr,16);
long li_bin = std::stol (str_bin,nullptr,2);
long li_auto = std::stol (str_auto,nullptr,0);
std::cout << str_dec << ": " << li_dec << '\n';
std::cout << str_hex << ": " << li_hex << '\n';
std::cout << str_bin << ": " << li_bin << '\n';
std::cout << str_auto << ": " << li_auto << '\n';
return 0;
}
I want to save the output from
std::cout << endl << std::setprecision(20) << std::fixed;
cout << ((.0000023)*(.00000023))<< endl;
Which is: 0.00000000000052900000
-into a string variable.
You could split this into steps using std::ostringstream:
std::ostringstream output;
output << << endl << std::setprecision(20) << std::fixed;
output << ((.0000023)*(.00000023))<< endl;
std::string result_string = output.str();
I suggest to try std::ostringstream:
std::ostringstream oss;
oss << ...;
std::string result(oss.str());
Note that it's slow on some platfroms/compilers.
See std::stringstream to stream into a string.
#include <string>
#include <sstream>
int main()
{
std::ostringstream stream;
stream << 10 + 10;
std::string result = stream.str(); // result contains "20"
return 0;
}
Edit: String manipulators work with std::ostringstream just like std::cout.
stream << std::fixed << (1.0 / 100.0); // result will contain "0.010000"
I'm getting string like 2015-04-29 15:36:16.5761891 +03:00. I can easily exctract the date using std::get_time.
std::tm time;
std::string stringTime = "2015-04-29 15:36:16.5761891 +03:00";
std::istringstream stringStream(stringTime);
stringStream >> std::get_time(&time, "%Y-%m-%d %H:%M:%S");
cout << time.tm_year << endl;
cout << time.tm_mon << endl;
cout << time.tm_mday << endl;
cout << time.tm_hour << endl;
cout << time.tm_min << endl;
cout << time.tm_sec << endl;
It's working fine for me. Now how can I extract UTC offset from this string?
You can just keep on reading like this:
#include <ctime>
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
int main()
{
std::tm time;
std::string stringTime = "2015-04-29 15:36:16.5761891 +03:00";
std::istringstream stringStream(stringTime);
std::string decimals;
std::string offset;
stringStream >> std::get_time(&time, "%Y-%m-%d %H:%M:%S") >> decimals >> offset;
std::cout << time.tm_year << '\n';
std::cout << time.tm_mon << '\n';
std::cout << time.tm_mday << '\n';
std::cout << time.tm_hour << '\n';
std::cout << time.tm_min << '\n';
std::cout << time.tm_sec << '\n';
std::cout << decimals << '\n';
std::cout << offset << '\n';
}
Output:
115
3
29
15
36
16
.5761891
+03:00
I have a simple function that builds a string representation of a simple 'Movie' object. I'm doing this ...
string Movie::getDisplayText() {
ostringstream oss;
oss << "Title: " << this->getTitle() << "\tYear: "+this->getYear() << "\tGenre: " << this->getGenre();
string ret = oss.str();
return ret;
}
But the string that gets built and returned looks like this ...
\�tle: Star Wars�B
Genre: Science-Fiction
When I add this line to test the values are valid ...
cout << "DEBUG Title: " << this->getTitle() << "\tYear: "+this->getYear() << "\tGenre: " << this->getGenre() << "\n" << endl;
... it outputs to 'cout' a correct looking string so I know the values are all initialized correctly ...
DEBUG title='Star Wars'; year='1977'; genre='Science-Fiction';
What's wrong with my ostringstream code?
You have this:
string Movie::getDisplayText() {
ostringstream oss;
oss << "Title: " << this->getTitle() << "\tYear: "+this->getYear() << "\tGenre: " << this->getGenre();
string ret = oss.str();
return ret;
}
It should be
string Movie::getDisplayText() {
ostringstream oss;
oss << "Title: " << this->getTitle() << "\tYear: " << this->getYear() << "\tGenre: " << this->getGenre();
string ret = oss.str();
return ret;
}
Not the difference after the \tYear:. The reason is that adding a string literal and an int together does not result in concatenation.