1

everybody I have problem with string concatenation in C++, here is my code

map<double, string> fracs;
for(int d=1; d<=N; d++)
    for(int n=0; n<=d; n++)            
        if(gcd(n, d)==1){
            string s = n+"/"+d;// this does not work in C++ but works in Java
            fracs.insert(make_pair((double)(n/d), s));
            }

How can I fix my code?

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
torayeff
  • 37
  • 1
  • 8

8 Answers8

2

In C++ you have to convert an int to a string before you can concatenate it with another string using the + operator.

See Easiest way to convert int to string in C++.

Community
  • 1
  • 1
Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
2

Try like this.

stringstream os;
os << n << "/" << d;
string s =os.str();
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
2

Use streams, in your case, a stringstream:

#include <sstream>
...
    std::stringstream ss;
    ss << n << '/' << d;

Later, when done with your work, you can store it as an ordinary string:

const std::string s = ss.str();

Important (side-) note: Never do

const char *s = ss.str().c_str();

stringstream::str() produces a temporary std::string, and according to the standard, temporaries live until the end of the expression. Then, std::string::c_str() gives you a pointer to a null-terminated string, but according to The Holy Law, that C-style-string becomes invalid once the std::string (from which you receved it) changes.

It might work this time, and next time, and even on QA, but explodes right in the face of your most valuable customer.

The std::string must survive until the battle is over:

const std::string s = ss.str(); // must exist as long as sz is being used
const char *sz = s.c_str();
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

I think sprintf(), which is a function used to send formatted data to strings, would be a much clearer way to do it. Just the way you would use printf, but with the c-style string type char* as a first(additional) argument:

char* temp;
sprint(temp, "%d/%d", n, d);
std::string g(temp);

You could check it out at http://www.cplusplus.com/reference/cstdio/sprintf/

peteAye
  • 3
  • 3
0

n and d are integers. Here is how you can convert integer to string:

std::string s;
std::stringstream out;
out << n << "/" << d;
s = out.str();
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
0

Unlike in Java, in C++ there is no operator+ that explicitly converts a number to a string. What is usually done in C++ in cases like this is...

#include <sstream>

stringstream ss;
ss << n << '/' << d; // Just like you'd do with cout
string s = ss.str(); // Convert the stringstream to a string
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
0

You could use a stringstream.

stringstream s;
s << n << "/" << d;
fracs.insert(make_pair((double)n/d, s.str()));
jpm
  • 3,165
  • 17
  • 24
0

No one has suggested it yet but you can also take a look at boost::lexical_cast<>.

While this method is sometimes criticized because of performance issues, it might be ok in your situation, and it surely makes the code more readable.

ereOn
  • 53,676
  • 39
  • 161
  • 238