How do I include the string header?
You want to include <string>
and use std::string
:
#include <string>
#include <iostream>
int main()
{
std::string s = "a string";
std::cout << s << std::endl;
}
But what you really need to do is get an introductory level book. You aren't going to learn properly any other way, certainly not scrapping for information online.
where do i include the string library in my C++ header file
Put it in the header file, and prefix your usage of string with the namespace std
.
Header:
#include <string>
class engineer
{
std::string company;
};
In the implementation file (.cpp
) you can prefix the names or have a using
directive.
Implementation:
using namespace std; // using directive, no longer need to use std::
Avoid putting the using
directive in a header file, as that pollutes the global namespace and can cause problems with naming collisions in other libraries you may wish to use.
include string or string.h
<string>
is a C++ standard library include, and <string.h>
is C standard library include.
The equivalent of <string.h>
in C++ is <cstring>
, although both will work.
The difference is: <cstring>
wraps everything in the std
namespace whereas <string.h>
puts everything in the global namespace.
Also, expect some stricter type safety rules from <cstring>
.
When to include the string header in your program?
Your teacher was correct.
Your program worked without <string>
by chance. Your standard library implementation, of that version, on that platform, under those circumstances, on that day, transitively included what you needed via <iostream>
. The standard library is just code, like yours, and it just so happens that your particular implementation contains, inside <iostream>
, an #include <string>
. It could be buried behind many other #include
s but got there eventually. But that's honestly pure chance, and does not mean that this is something the language guarantees, or something that must always be the case even in practice.
You should always code to standards.
If you're using features from <string>
, include <string>
.
Just today I was trying to build my big project with a new toolchain and found a few places where I'd accidentally relied on transitive includes, and it broke the build as a result because the new standard library implementation had a slightly different arrangement of headers. I dutifully added the missing #include
s and now the world is a better place for it.
Why do we need to include the string header file when std::string will suffice to initialize a string in C++?
There is no guarantee your code will work without <string>
. That is the header std::string
is defined in, so you must include it if you want your code to be portable.
Your code may work or appear to work because <iostream>
directly or indirectly includes <string>
.
When do I use '#include string' at the start of a C++ program?
Do I only use
#include <string>
if a variable represents a string?
Yes.
Use #include <string>
when you use a variable that has type std::string
.
The code "text here"
, contrary to intuition, is not a std::string
; it is a string literal, and a C-style string, and a const char[10]
convertible to const char*
. Welcome to C++ with its legacy oddities.
Class definition in header file and including string
should I include string? I read somewhere that this can cause issues so if thats not the solution how should i proceed?
You should include <string>
. You probably read that it's not ok to put using namespace std;
in the header, which is true. But there's nothing wrong with including a header if you need it. You'll need to qualify the uses of string
though:
#ifndef CLASS_Bed
#define CLASS_Bed
//////BED
#include <string>
class Bed:public Item
{
std::string frame;
std::string frameColour;
std::string mattress;
public:
int Bed(int count);
int Bed(int count, int number, std::string name, std::string frm, std::string fclr, std::string mtres);
void count();
void printDetails();
}; //<-- note semi-colon here
#endif
Do I have to use #include string beside iostream?
Yes, you have to include what you use. It's not mandated that standard headers include one another (with a few exceptions IIRC). It might work now, but might fail on a different compiler.
In your case, apparently <iostream>
includes <string>
, directly or indirectly, but don't rely on it.
Is string header file needed anymore?
Some compiler implementations include the header <string>
in the header <iostream>
.
But you shall not rely on this.
Related Topics
Reading an Application's Manifest File
How to Check That the Passed Iterator Is a Random Access Iterator
Simpler Way to Create a C++ Memorystream from (Char*, Size_T), Without Copying the Data
C++, Static VS. Namespace VS. Singleton
How to Get Current CPU and Ram Usage in C++
In C++ Main Function Is the Entry Point to Program How to Change It to an Other Function
Strange Ambiguous Call to Overloaded Function Error
Incrementing Iterators: Is ++It More Efficient Than It++
Constexpr Initialization of Array to Sort Contents
What Is the Purpose of Ref-Qualified Member Functions
Why Isn't There an Operator[] for a Std::List
Is Wchar_T Needed for Unicode Support
Is Writing to &Str[0] Buffer (Of a Std:String) Well-Defined Behaviour in C++11
C++ - Interval Tree Implementation
Int *Array = New Int[N]; What Is This Function Actually Doing