[NEWBIE] from C string to std::string

S

Stefano Sabatini

Hi guys,
which is the best way to define a std::string using some C strings
(arrays of chars)?

Here it is the code:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string s;
// this won't even compile, since in this case "this is a" and
// others string are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string.");
cout << s;

// a better way??
// s = ...

return 0;
}
 
S

Stefano Sabatini

Hi guys,
which is the best way to define a std::string using some C strings
(arrays of chars)?

Here it is the code:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string s;
// this won't even compile, since in this case "this is a" and
// others string are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string.");
cout << s;

// a better way??
// s = ...

return 0;
}

Mmh..., it was very simple, reading another thread and FAQ-C++-lite
then I finally found a satisfying solution:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(void) {
string s;
// this won't work, since in this case "this" are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string");
cout << s;

// a better way??
std::eek:stringstream o;
int i=-1;
o << "this is " << " a string " << "and this is a number: " << i << endl;
cout << o.str();

return 0;
}

for (int i=0; i < 10; i++)
cout << "regards ";
 
A

anon

Stefano said:
Mmh..., it was very simple, reading another thread and FAQ-C++-lite
then I finally found a satisfying solution:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(void) {
string s;
// this won't work, since in this case "this" are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string");
cout << s;

// a better way??
std::eek:stringstream o;
int i=-1;
o << "this is " << " a string " << "and this is a number: " << i << endl;
cout << o.str();

return 0;
}

This doesn't corespond to what you asked, as you haven't mentioned
numbers - only char arrays. I expected something like this:

string s( "a very "
"long "
"string" );
 
S

Stefano Sabatini

This doesn't corespond to what you asked, as you haven't mentioned
numbers - only char arrays. I expected something like this:

string s( "a very "
"long "
"string" );

Hi, yes indeed, though what I was trying to ask for was if that was
possible to use a string in way similar to an iostream. sstream
addresses this requirement.

Thanks for your attention.

Regards.
 
L

Linonut

* Stefano Sabatini peremptorily fired off this memo:
Hi, yes indeed, though what I was trying to ask for was if that was
possible to use a string in way similar to an iostream. sstream
addresses this requirement.

Thanks for your attention.

Don't forget about:

s += "string value";
 
B

Bo Persson

Stefano said:
Hi guys,
which is the best way to define a std::string using some C strings
(arrays of chars)?

Here it is the code:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string s;
// this won't even compile, since in this case "this is a" and
// others string are interpreted like char arrays
// s = "this is a" + " string.";

Adding two string literals together is done by the preprocessor. You
just have to put them adjacent to each other, WITHOUT any operators:

s = "this is a" " string.";


Of course, why would you want to do this? :)


Bo Persson
 
J

Jim Langston

Stefano said:
Hi guys,
which is the best way to define a std::string using some C strings
(arrays of chars)?

Here it is the code:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string s;
// this won't even compile, since in this case "this is a" and
// others string are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string.");
cout << s;

// a better way??
// s = ...

return 0;
}

Only the first string has to be converted to a std::string. Consider.

#include <string>

int main()
{
std::string s1;
s1 = "String1";
s1 += "string2";

std::string s2;
s2 = std::string("String1") + "String2";

std::string s3;
s3 = std::string("String1") + "String2" + "String3" + "String4";
}
 
R

Richard Herring

[QUOTE="Bo Persson said:
Hi guys,
which is the best way to define a std::string using some C strings
(arrays of chars)?

Here it is the code:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string s;
// this won't even compile, since in this case "this is a" and
// others string are interpreted like char arrays
// s = "this is a" + " string.";

Adding two string literals together is done by the preprocessor. You
just have to put them adjacent to each other, WITHOUT any operators:

s = "this is a" " string.";


Of course, why would you want to do this? :)
[/QUOTE]

One reason might be to embed comments:

std::string angleExpression(
"^\\s*" /* skip leading whitespace */
"([-+]?)" /* exp1 is optional sign */
"\\s*" /* skip whitespace */
"(\\d*)" /* exp2 is integer degrees */
"([:nsew])" /* exp3 is punctuation, maybe quadrant letter */
"(\\d*)" /* exp4 is integer minutes */
"[:']" /* punctuator between min & sec */
"(\\d*)" /* exp5 is integer part of seconds */
"[.,]?" /* maybe there's a decimal part */
"(\\d*)" /* exp6 is fraction of seconds */
"[:"]?" /* may be final punctuation */
"\\s*" /* whitespace */
"([nsew]?)" /* exp7 is quadrant letter */
);
 
J

Juha Nieminen

Bo said:
s = "this is a" " string.";

Of course, why would you want to do this? :)

I use this quite a lot with strings which are longer than a
reasonably-sized code line. I also often use it with multi-lined strings
(ie. strings which have newlines in them), eg like this:

s = "This is a long text with\n"
"several lines using the\n"
"newline character.\n";
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top