joining 2 strings

S

sam.barker0

Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?

Cheers,
Sam
 
C

cch@srdgame

于 Sat, 30 Aug 2008 21:01:48 -0700,sam.barker0写到:
Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?

Cheers,
Sam

I am worry that the w was treated as four chars, not what you want.

The stringstream might help.
 
R

red floyd

Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?

Repeat after me. C++ is not Java.

Now, your problem. String literals are of type const char *. You can't
add them. Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah". For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
int w = 0;
std::eek:streamstring ss;
ss << "blah" << w << "blah";
std::string z = ss.str();
}
 
I

Ian Collins

Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";
Define works. Have you checked the result?
 
M

multyinfo

:::WWW.MULTYINFO.COM UPDATES ::: We have just updated our website and
added some new options such as: NEWS FROM ALL OVER THE WORLD. More
than 157 Countries Latest News. LINK EXCHANGE and islam,business,live
tv, play online game ,mobile and we have updated daily please vISIT
REGULARLY Just visit at http://www.multyinfo.com
 
A

andy_westken

Repeat after me.  C++ is not Java.

Now, your problem.  String literals are of type const char *.  You can't
add them.  Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah".  For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
   int w = 0;
   std::eek:streamstring ss;
   ss << "blah" << w << "blah";
   std::string z = ss.str();



}- Hide quoted text -

- Show quoted text -

I am not keen on the insistence that std::eek:stringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance. So while I do
use the 'stream solution in UI code and the like, in more performance
sensitive situations I would generally solve the original problem
using code along the lines of:

string z("blah");
char buffer[12];
z += _atol(w, buffer, 10); // convert long to C string
z += "blah";

(Where I think I'm assuming posix conformance? Is _atol more than
likely to be about?)

I would not even try to use operator+ in this situation, as it uses a
temporary to store the return value for each invocation. Whereas
operator+= simply appends the new string to the buffer of the existing
instance. But if you were determined to do it, then the following
"works".

char buffer[12];
string z = string("blah") + _ltoa(w, buffer, 10) + "blah ";

Note that I would generally wrap the _atol and buffer in a naive
little class. This class is only intended to be used with operator+=
and does will not even compile in most other situations.

class ToString
{
public:
ToString(long value)
{
_ltoa(value, buffer, radix);
}

operator const char*() const
{
return buffer;
}

private:
static const int radix = 10;
// INT_MIN (-2147483647 - 1)
// INT_MAX 2147483647
// longest string = 11 chars + 1 for null terminator
static const int bufferSize = 12;
char buffer[bufferSize];
};

Which allows you to write:

string z("blah");
z += ToString(w);
z += "blah";

The boost.lexical_cast also allows you to write code of a similar form

using boost::lexical_cast;

string z("blah");
z += lexical_cast<string>(w);
z += "blah";

but lexical_cast uses std::eek:streamstream to do the work so has the
same performance as the normal 'stream solution.

Andy
 
E

Erik Wikström

Repeat after me. C++ is not Java.

Now, your problem. String literals are of type const char *. You can't
add them. Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah". For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
int w = 0;
std::eek:streamstring ss;
ss << "blah" << w << "blah";
std::string z = ss.str();



}- Hide quoted text -

- Show quoted text -

I am not keen on the insistence that std::eek:stringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance. So while I do
use the 'stream solution in UI code and the like, in more performance
sensitive situations I would generally solve the original problem
using code along the lines of:

string z("blah");
char buffer[12];
z += _atol(w, buffer, 10); // convert long to C string
z += "blah";

(Where I think I'm assuming posix conformance? Is _atol more than
likely to be about?)

Not very likely at all, as far as I can see. POSIX only defines atol()
but no _atol(), and I think that holds for most functions beginning with
an underscore (I could only find 5 functions in POSIX beginning with an
underscore).

If you are in need of performance and want to construct strings I think
using snprintf() is probably the best idea, just make sure that the
return-value is less than n.
 
P

Paul Brettschneider

Erik said:
(e-mail address removed) wrote:
Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?

Repeat after me. C++ is not Java.

Now, your problem. String literals are of type const char *. You can't
add them. Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah". For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
int w = 0;
std::eek:streamstring ss;
ss << "blah" << w << "blah";
std::string z = ss.str();



}- Hide quoted text -

- Show quoted text -

I am not keen on the insistence that std::eek:stringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance. So while I do
use the 'stream solution in UI code and the like, in more performance
sensitive situations I would generally solve the original problem
using code along the lines of:

string z("blah");
char buffer[12];
z += _atol(w, buffer, 10); // convert long to C string
z += "blah";

(Where I think I'm assuming posix conformance? Is _atol more than
likely to be about?)

Not very likely at all, as far as I can see. POSIX only defines atol()
but no _atol(), and I think that holds for most functions beginning with
an underscore (I could only find 5 functions in POSIX beginning with an
underscore).

I think Andy is thinking of ltoa (convert int to string), which is not
defined by POSIX, AFAIK. The idiomatic (though kind of ugly) POSIX solution
is
char buf[12];
snprintf(buf, sizeof(buf), "%d", i);
or
char buf[200];
snprintf(buf, sizeof(buf), "Blah %dblah", w);
std::string z(buf);
 
O

Old Wolf

I am not keen on the insistence that std::eek:stringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance.

class ToString
{
public:
    ToString(long value)
    {
        _ltoa(value, buffer, radix);
    }

    operator const char*() const
    {
        return buffer;
    }

private:
    static const int radix = 10;
    // INT_MIN     (-2147483647 - 1)
    // INT_MAX     2147483647
    // longest string = 11 chars + 1 for null terminator
    static const int bufferSize = 12;
    char buffer[bufferSize];
};

This seems like a poor idea to me, if your code
is compiled on a system where ints are bigger then
it will silently cause a buffer overflow.

Anyway, I think you will find that stringstream
does something like this internally anyway,
except it doesn't need to allocate as much memory!

(You have to allocate a 'ToString' object, but the
stringstream may already have enough memory allocated
in its internal buffer to convert the int without
having to allocate more memory).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top