ITHelpself / CPlusPlus

0 stars 0 forks source link

13. Stream #14

Open ITHelpself opened 4 years ago

ITHelpself commented 4 years ago

string stream


#include <sstream>
#include <string>
using namespace std;
int main()
{
ostringstream ss;
ss << "the answer to everything is " << 42;
const string result = ss.str();
}
ITHelpself commented 4 years ago

class foo
{
// All sort of stuff here.
};
ostream &operator<<(ostream &os, const foo &f);
ITHelpself commented 4 years ago

std::vector<int> v = {1,2,3,4};
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ! "));
ITHelpself commented 4 years ago

std::cout << std::setprecision(3);
std::fixed(std::cout);
std::vector<int> v = {1,2,3,4};
std::copy(v.begin(), v.end(), std::ostream_iterator<float>(std::cout, " ! "));
ITHelpself commented 4 years ago

std::boolalpha(std::cout); // print booleans alphabetically
std::transform(v.begin(), v.end(), std::ostream_iterator<bool>(std::cout, " "),
               [](int val) {
                   return (val % 2) == 0;
               });
//or print the squared element : 
std::transform(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "),
                                              [](int val) {
                                                  return val * val;
                                              });
//Printing N space - delimited random numbers : 
const int N = 10;
std::generate_n(std::ostream_iterator<int>(std::cout, " "), N, std::rand);
ITHelpself commented 4 years ago

int v[] = {1,2,3,4,8,16};
std::transform(v, std::end(v), std::ostream_iterator<int>(std::cout, " "),
[](int val) {
return val * val;
});