How to Include the String Header

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 #includes 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 #includes 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



Leave a reply



Submit