Concatenating Variables

Concatenating Variables

If you need to concatenate variables of different types to form a string, use a stringstream object. The header contains the declarations of the stringstream family of classes. Using its overloaded << operators, stringstream will convert variables of built-in types such as int, double and char * into a string and store the result in its internal buffer. To extract the resulting string, use the >> operator:

  #include void concat(std::string &result){ long x = 1000; double y = 5.5.; std::stringstream s; s << "(" << x << "," << y << ")"; s>>result; // result is now "(1000,5.5)"}
See also  10 Best Apps to Help You Read More Books in Less Time

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist