-3

I want to create string "hello1world by this two strings:

string s1 = "hello"
string s2 = "world" 

I do this:

string my_str;
sprintf(my_str, "%s1%s", s1, s2);

But I have an error:

error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char*’ for argument ‘1’ to ‘int sprintf(char*, const char*, ...)’
     sprintf(my_str, "%s1%s", s1, s2);

How to do it properly?

Kenenbek Arzymatov
  • 8,439
  • 19
  • 58
  • 109
  • 3
    I recommend you check e.g. [this `std::string` reference](http://en.cppreference.com/w/cpp/string/basic_string). Look at the overloaded operators. I'm sure you could find something useful to *add* two strings. – Some programmer dude Oct 08 '16 at 18:16
  • 2
    Please get aware of C-functionality and C++-functionality (Your mixture is plain wrong). –  Oct 08 '16 at 18:17

5 Answers5

2

Please don't use sprintf. It only supports char[] strings, i.e. C strings. So that's why it doesn't compile when you pass std::string.

With std::string, a simple + is all you need:

string my_str = s1 + s2;
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
2

If you want to concatenate the two strings, you can use + operator of std::string

string s1 = "hello";
string s2 = "world";
string my_str = s1 + "1" + s2;
Ahmad Khan
  • 2,655
  • 19
  • 25
1

For case of two strings formatting use operator+=:

std::string myStr(s1 + "1" + s2);

If you need to do formatting of many strings, a more efficient way will be:

std::string myStr;
myStr.reserve(1048576);      // Reserve space in buffer to prevent reallocation
while (<many iterations>)
{
  myStr.append(s1);
  myStr.append("1");
  // Do more formatting here
  myStr.append(s2);
}

If you have some non-string values to include in your result you can use std::stringstream from <sstream> header:

std::stringstream ss;
ss << "hello";
ss << 1;
ss << "world";
std::string myStr(ss.str());

I need to mention (thanks @IInspectable for hint) std::stringstream is pretty slow. C++11 offers better way for numbers formatting with use of std::to_string.

Nikita
  • 6,270
  • 2
  • 24
  • 37
  • You cannot be serious... -1, sorry. – IInspectable Oct 08 '16 at 18:37
  • @IInspectable I knew that `stringstream` is not very fast, but now i see it's almost 4x slower than `operator+`. Thanks for comment. I've included hint about that in the answer and added more fast approaches. – Nikita Oct 08 '16 at 19:14
  • I wasn't actually commenting on the performance. Performance varies across implementations anyway. Suggesting string streams to concatenate strings, however, is just so utterly convoluted, and non-intuitive, that I cannot imagine the confusion of the mind suggesting such. At any rate, I revoked the down-vote after your update. – IInspectable Oct 08 '16 at 19:52
  • @IInspectable I agree, original post doesn't properly reflect my idea, I proposed `stringstream` for case when non-string values should be included into result. The reason of it is `1` in the format string `"%s1%s"`. I thought it would be useful to print something other than strings. – Nikita Oct 08 '16 at 20:12
0
std::string s3 = s1 + "1" + s2;
jackdaw
  • 564
  • 3
  • 12
0

you have many things to do you can use operator +

string a = "hello";
string b = "world";
string ans = a + "1" + b;

you can use string::append

  string a = "hello";
  string b = "world";
  string ans = "";
  ans.append(a);
  ans.append("1");
  ans.append(b);

you can use insert but not pereferd in concate better use + operator or append

string a = "hello";
string b = "world";
a.insert(a.size(),1,'1');
a.insert(a.size(),b);

insert with a string use it as string::insert(index,string);
with characters use it as string::insert(index,len,char);

user7179690
  • 1,051
  • 3
  • 17
  • 40