Begginer question about #include <string>

If <string> provides the C++ standard string classes and templates, why is it unnecessary to include in the header of the source code? ..or is it?
Just like in this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
  string mystr;
  float price=0;
  int quantity=0;

  cout << "Enter price: ";
  getline (cin,mystr);
  stringstream(mystr) >> price;
  cout << "Enter quantity: ";
  getline (cin,mystr);
  stringstream(mystr) >> quantity;
  cout << "Total price: " << price*quantity << endl;
  return 0;
}


Even if I dont include the line #include <string> , the result remain unchanged, why does it needed there?
Thanks.
This happens because <string> is included in either <iostream> or <sstream>. However you cannot be sure it will be, so it's better always to specify what you're including.
1. <string> is deprecated... use <cstring>
2. Stop including the entire STD namespace...
@packetpirate - I'm curious... why should you not include the STD namespace? Thanks
<string> is deprecated... use <cstring>

What are you talking about? The language is C++ so it would make sense to use C++ standard libraries, not C. Do you have any reasons to say something like this?
Taken from the sstream header:
1
2
3
4
5
6
// sstream standard header
#pragma once
#ifndef _SSTREAM_
#define _SSTREAM_
#ifndef RC_INVOKED
#include <string> 


As you can see, sstream includes the string class. I see nothing wrong with including "string".

Also, what is wrong with "using namespace std" (I assume that's what you're talking about)?
<string.h> is replaced with <cstring> so that was a misunderstanding. <string> is the proper c++ header for the C+ std string template.
Packetpirate, I'm sorry, but we need you to shut up, because this is bad advice that could lead to a lot of problems. <string> is not deprecated, in fact if anything C strings ought to be deprecated. You probably meant <string.h> but there's a huge difference between <string> and <string.h>.

This is the second time you've made this mistake and I've corrected you.

-Albatross
Yea, about not using namespace std is bit confusing.Btw, thanks for the info everyone.
using namespace std; pollutes the global namespace. That is it drags every symbol from the included header into the global arena.

This can make it hard to tell which class or function you are calling and there are a number of subtle errors that can occur like this.

A better way is to either prefix your std symbols with std:: or you could also import only the sympols you use into the global space:

1
2
3
4
5
6
7
8
#include <string>
#include <vector>

using std::string;
using std::vector;

vector<string> svec;


What you must never do is use 'using namespace' or even 'using std::string' in any header files. They should never introduce symbols into the global namespace from the standard libraries (or any other library for that matter).

However in small example programs you can get away with 'using namespace std;' But I personally feel its best to keep consistant habits.
Last edited on
mainframe639 wrote:
Yea, about not using namespace std is bit confusing.Btw, thanks for the info everyone.


here is a great explanation:

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
Pretty good site Galik.
<string> is deprecated... use <cstring>


string != string.h

You're thinking of the string.h C library. string is the C++ library, and properly referred to in the program.
Topic archived. No new replies allowed.