devxlogo

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)"}

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

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.