0

Is there a function to create strings or char[] by concatenating multiple types?

Something like:

int int_type = 5;
float float_type = 3.14;
String string_type = "I'm";
char char_type = ' ';
char char_arr_type[9] = "a pirate"; 

String merged = x_func(int_type, float_type, string_type, char_type, char_arr_type);

// expected: "53.14I'm a pirate"

Wooble
  • 156
  • 2
  • 10
  • 1
    You can turn each argument into a string via [`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string), and then concatenate those individual strings. Alternatively you could `<<` each into a stringstream. What is it you are actually trying to achieve? – DevSolar Apr 02 '22 at 16:59
  • Thank you!! Is there a line solution like the example? I need to pass a string to a logging function with some variables. This function is called multiple times in different ways in the code so I would like to have a "clean" way to call it – Wooble Apr 02 '22 at 17:10
  • [`std::format`, `ostringstream` macro, ...](https://stackoverflow.com/a/5590404/60281) – DevSolar Apr 02 '22 at 19:00
  • I prefer absl::StrCat – Taekahn Apr 03 '22 at 02:21
  • With the damage Google has done to C++ with their "Style Guide", I am very reluctant with recommending anything that comes from their direction. – DevSolar Apr 03 '22 at 14:03

3 Answers3

4

in c++17 you can use fold expression and string stream.

Example:

#include <iostream>
#include <sstream>
using namespace std;

template<typename ... Args>
string makeString(Args ... args)
{   
    stringstream ss;
    (ss << ... << args);
    return ss.str();
}
int main(){
    cout<< makeString("a b" , 1, "c d");
}

Output: a b1c d

Olexy
  • 324
  • 1
  • 9
1

You can use a std::stringstream for this purpose. It works almost the same way as printing all the variables to std::cout. Example:

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

int main() 
{
    // Your variables
    int int_type = 5;
    float float_type = 3.14;
    std::string string_type = "I'm";
    char char_type = ' ';
    char char_arr_type[9] = "a pirate"; 

    // Creating a new stream
    std::stringstream s;
    
    // Print all the variables to the stream
    s << int_type << float_type << string_type << char_type << char_arr_type;
    
    // retrieve the result as std::string
    std::string merged = s.str();
    
    std::cout << merged; // output: 53.14I'm a pirate
}

PS: I'm not sure what String in your code means, C++ only has std::string. Unless this is a typo you might have to do some casting or provide a custom operator<<(std::ostream&, const String&).

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • Thanks!! My fault, I used the code from an Arduino sketch which use a different library for strings. Is there a build in one-line solution like the example? – Wooble Apr 02 '22 at 17:31
  • @Wooble Ahh, ok, I don't know much about arduino. The accepted answer looks good however :) – Lukas-T Apr 03 '22 at 07:00
1

In C++20 and later, you can use std::format():

std::string merged = std::format("{}{}{}{}{}", int_type, float_type, string_type, char_type, char_arr_type);

In C++11 and later, you can use the {fmt} library instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770