where is std::string defined?

J

Joe Laughlin

#include <iostream>

int main()
{
std::string hello = "Hello World\n";
return 0;
}


This compiles fine. Is std::string defined in cstring? Or is it included
in iostream?

Thanks,
Joe
 
A

Artie Gold

Joe said:
#include <iostream>

int main()
{
std::string hello = "Hello World\n";
return 0;
}


This compiles fine. Is std::string defined in cstring? Or is it included
in iostream?
It is defined in <string>. The header <iostream> evidently includes
<string> on your implementation.

HTH,
--ag
 
J

Joe Laughlin

Artie said:
It is defined in <string>. The header <iostream>
evidently includes <string> on your implementation.

HTH,
--ag

Is it good practice to include the <string> header then?
 
A

Artie Gold

Joe said:
Is it good practice to include the <string> header then?
Yes. If you use *anything* from the standard library, *always* include
the header in which it is defined.

HTH,
--ag
 
J

JKop

Joe Laughlin posted:
Is it good practice to include the <string> header then?


I'd advocate it!

I even go one step futher:


#include <cstddef>
#include <cstring>

inline std::size_t strlenPlusNull(const char* const str);
{
std::size_t temp = std::strlen(str);

return temp += 1;
}


The function "strlen" returns an object of type "std::size_t". As such, if
you include the header that has "strlen" in it, then it simply must also
contain the definition of "std::size_t". But still... I like to include
"cstddef", whose actual job it is to define "std::size_t".

So for your program just there, I myself would do:

#include <iostream>
#include <string>


-JKop
 

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
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top