C++ Workshop - Getting Started with C++ (Ch. 1 & 2)

Started by
182 comments, last by Dbproguy 16 years ago
Well, the organizer has suggested Microsoft Visual C++ 2005 Express Edition. Look here for information about the workshop.

The STL is the "Standard Template Library" and it is a collection of utilities that are part of the C++ language and that make your life easier. std::cout is a part of the STL, for instance.


jfl.

[Edited by - jflanglois on June 2, 2006 4:33:41 PM]
Advertisement
Thanks for the advice everyone. I do have one more question though. What exactly is a namespace?......I'm somewhat confused by the definitions I've found...
Quote:Original post by Yu Une
@jflanglois
Hmm.. what compiler should I use tho I have Dev C++ but it's pak installer doesn't work, other wize it run fine :), btw what's a STL implementation?


Not sure about the pak installer (not familiar with Dev-CPP). If you are running windows, you could download the express edition of Visual Studio 2005. Otherwise, you could use Code::Blocks as an IDE with the default compiler (make sure to download with the compiler).

STL is short for the C++ Standard Template library. It is a library of classes, functions, and other utilities to help you program. It contains things for storing data, writing data to the screen or a file, as well as many other functions. It is up to the maker of the C++ compiler to come up with their standard template library.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Quote:Original post by Joelvtx
Thanks for the advice everyone. I do have one more question though. What exactly is a namespace?......I'm somewhat confused by the definitions I've found...


The creation and use of namespaces will be covered in a later week, but a short description is a namespace simply allows you to bundle a lot of different things together under one name to make it easier to handle. The STL is huge, and there are many other libraries too. In large projects you like to know where various things are coming from so if you see std::map you know that map is a part of the namespace "std" or in other words its a part of the STL. If on the other hand you see boost::shared_ptr you know that shared_ptr is part of the "boost" namespace. It is just a way of tidying up large amounts of code.

Hope that helps somewhat.
@Stephen_R

Does this mean that every time I use a function(or whatever the proper term is), such as "cin" or "cout", I must declare the standard namespace in some way? (Sorry if the terminology isn't correct for these things.....) Or are "cin" and "cout" the only two? Sorry if that doesn't make much sense.
Quote:Original post by Joelvtx
@Stephen_R

Does this mean that every time I use a function(or whatever the proper term is), such as "cin" or "cout", I must declare the standard namespace in some way? (Sorry if the terminology isn't correct for these things.....) Or are "cin" and "cout" the only two? Sorry if that doesn't make much sense.


Yes, you will have to prefix everything from that namespace with "std::". This include cin, cout, endl, vector, list, map, and hundreds of others. Of course, you can use a "using" clause (as mentioned earlier), but this can cause problems later on.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Thanks you :-) That helps a lot. I was confused at first by why I had to use standard namespace, now it makes sense.
I have a question it might be stupid but what is the difference between '\n' and endl characters. thanks
Just passing through. Random comments:

Quote:Original post by jflanglois
As for std::cout, I'm not entirely sure if it is required to be a buffered stream (it isn't as far as I can tell on WinXP/VC2005)...

It's buffered by default, but it automatically flushes if you read any data from std::cin. You can switch off that buffering, but that's really not relevant to this workshop. Yet. [smile]

Quote:Original post by Sr_Guapo
-I would avoid "using" directives. They can cause problems if you have another class with the same name. It also generally helps make your code much clearer.

Full qualification is annoying and just clutters the screen. using obeys scope, so use it within the smallest appropriate scope and you are virtually guaranteed not to have a problem:
#include <iostream>int main(){  using namespace std;  // blah...}void some_func(){  using std::ifstream;  // blah...}


Quote:-Use "std::endl" instead of "\n". std::endl does more than kick to the next line; It flushes the buffer to the screen. This isn't so important with console output, but makes a difference for file output. Also, I believe some operating systems may require more than just an endline (such as a carriage return), so it's best to keep your code platform independent.

Actually, use std::endl when you want a newline and a flush. Use '\n' when you only want a newline, and use std::flush when you only want to flush.
Quote:Original post by Oluseyi
Just passing through. Random comments:

Quote:Original post by jflanglois
As for std::cout, I'm not entirely sure if it is required to be a buffered stream (it isn't as far as I can tell on WinXP/VC2005)...

It's buffered by default, but it automatically flushes if you read any data from std::cin. You can switch off that buffering, but that's really not relevant to this workshop. Yet. [smile]

Quote:Original post by Sr_Guapo
-I would avoid "using" directives. They can cause problems if you have another class with the same name. It also generally helps make your code much clearer.

Full qualification is annoying and just clutters the screen. using obeys scope, so use it within the smallest appropriate scope and you are virtually guaranteed not to have a problem:
#include <iostream>int main(){  using namespace std;  // blah...}void some_func(){  using std::ifstream;  // blah...}


Quote:-Use "std::endl" instead of "\n". std::endl does more than kick to the next line; It flushes the buffer to the screen. This isn't so important with console output, but makes a difference for file output. Also, I believe some operating systems may require more than just an endline (such as a carriage return), so it's best to keep your code platform independent.

Actually, use std::endl when you want a newline and a flush. Use '\n' when you only want a newline, and use std::flush when you only want to flush.


Ah, great points. Thanks for the clarification!
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute

This topic is closed to new replies.

Advertisement