You could just write
char a = '1';
char b = '2';
std::string s = std::string( "(" ) + a + "," + b + ")";
Or
char a = '1';
char b = '2';
string s;
for ( char c : { '(', a, ',', b, ')' } )
{
s += c;
}
Here is a demonstrative program.
#include <iostream>
#include <string>
int main()
{
char a = '1';
char b = '2';
std::string s = std::string( "(" ) + a + "," + b + ")";
std::cout << "s = " << s << '\n';
std::string t;
for ( char c : { '(', a, ',', b, ')' } )
{
t += c;
}
std::cout << "t = " << t << '\n';
return 0;
}
The program output is
s = (1,2)
t = (1,2)
Or you could use just a constructor like
std::string s( { '(', a, ',', b, ')' } );
or the method assign
std::string s;
s.assign( { '(', a, ',', b, ')' } );
or append
std::string s;
s.append( { '(', a, ',', b, ')' } );
Here is another demonstrative program.
#include <iostream>
#include <string>
int main()
{
char a = '1';
char b = '2';
std::string s1( { '(', a, ',', b, ')' } );
std::cout << "s1 = " << s1 << '\n';
std::string s2;
s2.assign( { '(', a, ',', b, ')' } );
std::cout << "s2 = " << s2 << '\n';
std::string s3( "The pair is " );
s3.append( { '(', a, ',', b, ')' } );
std::cout << "s3 = " << s3 << '\n';
return 0;
}
Its output is
s1 = (1,2)
s2 = (1,2)
s3 = The pair is (1,2)
"(" + a, theais converted to anintand the add gives a pointer. Then you try to add","to it.std::ostringstream strm; strm << "(" << a << "," << b << ")";