Stringstream can convert data into a string - C++ Data Type

C++ examples for Data Type:Number Format

Introduction

stringstream has formatting facilities provided by the standard input and output stream classes.

The following code uses the left-shift operator (<<) to write a combination of text and numeric data to string stream:

Demo Code

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;


int main() {/*w w  w.j  a va2  s. c o  m*/

   stringstream ss;

   ss << "There are " << 9 << " apples in my cart.";
   cout << ss.str() << endl;  // stringstream::str() returns a string with the contents

}

Result


Related Tutorials