how would u do this?
i have the following strings:
char *str1 = "123";
char *str2 = "456";
and now i want to print "123456"
what is the best way to do that?
Printable View
how would u do this?
i have the following strings:
char *str1 = "123";
char *str2 = "456";
and now i want to print "123456"
what is the best way to do that?
How about that:
Code:printf("%s%s", str1, str2);
or you could use the standard string class
Code:#include <string>
#include <iostream>
using std::string;
using std::cout;
// ....
string one = "part one";
string two = "and";
string three = "two";
one += ' ' + two + ' ' + three + '.';
cout << one;
do ppl use string type instead of using char arrays? i want to know what the pro guys do lol :P
made_of_asp: well imagine i want to work out that char array..using printf wont work here then ;)
anyways..there is here a thing that does confusion to me........how much spaces/slots in a char array u usually put? 30? 60? 200? the performance difference is big?
you can also doQuote:
Originally posted by sunburnt
or you could use the standard string class
Code:#include <string>
#include <iostream>
using std::string;
using std::cout;
// ....
string one = "part one";
string two = "and";
string three = "two";
one += ' ' + two + ' ' + three + '.';
cout << one;
String class is very heavy and is part of STL. I mostly write C-style code, so i can't use it.Code:sprintf(szString, "%s%s", string1, string2);
so i should avoid using the string class?
No, that's what it's for. The standard library's string class is not really slower then c strings, sometimes even faster. It is also much less error prone. By using string you don't have to worry about allocating and freeing memory, whether or not you need to append a '\0', etc. So, unless you either a. Must use plain C; or b. Are a wizard programmer writing kernel code, use std::string.Quote:
so i should avoid using the string class?
tks all
in which case its faster than c strings(char arrays)?
that isnt workin on vc++7...what should i change?Quote:
Originally posted by sunburnt
or you could use the standard string class
Code:#include <string>
#include <iostream>
using std::string;
using std::cout;
// ....
string one = "part one";
string two = "and";
string three = "two";
one += ' ' + two + ' ' + three + '.';
cout << one;
You shouldn't really avoid STL. But it does increase file size (greatly).
I can do everything in STL with plain C, so I don't really need it. I don't think string class is faster than char arrays, it has an overhead. But I might be wrong.
I don't think string is ever faster either. But it is written to cause very little runtime overhead.
And its ease of use makes up for any disadvantages.
I always use string except when I'm into absolute speed, and I seldom am.
Working for me if I put the actual code in main.Quote:
that isnt workin on vc++7...what should i change?
hmmm ok then ill investigate about the string class