#include <vector>

Dear all,
I am reading the textbooks by Walter Savitch on the chapters of Standard Template Library. In most of the cases Savitch will write something like using std::cout, using std::list<int>::iterator etc etc using instructions.
My question is, why not simply write #include <iostream> then #include <vector> once and for all? Why have to delcare what functions in what libraries to use one by one? Could have just include the whole library???
And actually, what if I have both a vector and a list and such that I need to use iterators in both cases? In that case if I declare it like using std::list<int>::iterator then using::std::vector<int>::iterator at the same time I am afraid there could be confusion. Is that if I simply declare #include <vector> instead then it will all be fine?
Bosco
I have not understood the greate sense of your question "My question is, why not simply write #include <iostream> then #include <vector> once and for all? "

What did you mean? As far as I know all programmers do as you pointed out "simply write #include <iostrem> then #include <vector>" but only in whose cases then they need these two libraries.

What is the problem?!
Last edited on
Hmm... Put it this way, let's say I have written #include <vector> at the beginning of the code, then when I want to use iterators and reverse iterators for a vector<char> named CHARACTER.

Do I still need to specifically declare using std::vector<char>::iterator and std::vector<char>::reverse_iterator? Or, the #include <vector> statement is already inclusive of these two declarations?

I hope this is clearer?

P.S. Likewise, after I have declared #include <iostream>, is it actually useful to further include the below two lines "using std::cout;" and "using std::endl;"? I thought after you have included the whole iostream library, then of course all those cout and endl have also been included, and I supposed it is not necessary to include those two extra lines. But in this textbook by Walter Savitch he included the two lines after #include <iostream> anyhow. And I don't see the significance of it.
cout, cin, endl, vector, etc are all in the standard namespace. You have to express that you want to use this namespace:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;  // Uses the entire namespace
//Or
using std::cin;
using std::cout;
..etc

// Or during your code

std::cout << "The number 256 in hex is: " << std::hex << 256 << std::endl;


For small projects it doesn't really matter. Your book is helpful by doing things it's way though, as you get to see what the program is going to be using from iostream or vector. Generally, you want to use as little as you can.

Also, when you make the iterator you give it a name. So long as the names are different, there's no problem (just like any other variable).
1
2
std::vector<char>::iterator vectorIterator;
std::list<char>::iterator listIterator;

Last edited on
The problem that you do not understand what is a declaration and what is a namespace. There is nothing common between #include <vector> and using std::vector<char>::iterator except that without the header you cannot reference its declarations.
<vector> contains template definitions of the template class vector. The directive using std::vector<char>::iterator is invalid. You could write using std::vector;
std::vector<char>::iterator and std::vector<char>::reverse_iterator are specializations of typenames defined inside class std::vector.
Thank you guys I think I start to get the gist of it...
Topic archived. No new replies allowed.