473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

where is std::string defined?

#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
Jul 22 '05 #1
5 2466
Joe Laughlin wrote:
#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

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Jul 22 '05 #2
Artie Gold wrote:
Joe Laughlin wrote:
#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


Is it good practice to include the <string> header then?
Jul 22 '05 #3
Joe Laughlin wrote:
Artie Gold wrote:
Joe Laughlin wrote:
#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

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

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Jul 22 '05 #4
Joe Laughlin posted:
Artie Gold wrote:
Joe Laughlin wrote:
#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


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
Jul 22 '05 #5
Joe Laughlin wrote:
Is it good practice to include the <string> header then?


If you intend to use stuff declared in it -- yes, definitely!

- J.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
7
by: Dylan | last post by:
How do I insert a NULL character into a std::string? thanks
12
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
7
by: Marcus Kwok | last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the "all characters" marker. I tried to use it this way, but my program crashes: #include <iostream> #include <string> int...
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
10
by: mr_sorcerer | last post by:
Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn't check null pointers?
2
by: HerbD | last post by:
I have a loooong debugging session behind me! I finally found the reason for the problem and now would like to know, if it is a bug in my code or not standardconformant behavour of the compiler(s) or...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.