Thread: Newbie Questions

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    Newbie Questions

    Hello -

    I'm just starting into the world of C++. While I'm sure that these answers will come eventually, I have a couple of simple questions that I would like to know now please.

    using namespace std;
    Is this like a library named 'std'?
    How do I know when/if to use that line of code?
    Where does this information physically reside?
    How will I know if my comuputer has it or not?

    cout << "Hello World";
    To use cout, do I need a namespace reference or an include reference?
    How will I know which one to use?

    If I need an include reference, how will I know which include to use?
    For example, if I need #include <iostream> before I use cout, how do I know iostream is 'related' to cout? How would I find out what else is 'related' to #include <iostream>?
    How do I know if my computer has #include <iostream>?
    Do the contents of <iostream> ever change?
    When I compile my program to run on another machine, would the other machine need the namespace std and the <iostream>?

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A namespace is a way to group names (variables, classes, functions, etc) so that conflicts don't occur. All the standard library names are in the std namespace, so to use them you have to tell the compiler to look there for the name you are using (like cout, cin, string, etc). Use that line of code if you don't want to specify std:: in front of each standard library name (like std::cout, std::cin, std::string, etc). Typing out std:: every time is better in general, but the using directive is ok as well, especially for small beginner programs. There are plenty of threads about the benefits of one or the other if you search here.

    To use cout, you need both an include and a namespace reference. You #include <iostream> to tell the compiler where to look for cout, and you add std:: or using namespace std to tell the compiler that the name is in the std namespace.

    You identify which header to include by using your reference book/website/tutorial. It will tell you which one to use when you learn about whatever you are using.

    Since <iostream> is a standard C++ header, all modern standard-compliant compilers will have it. The contents are specified by the standard, so they don't change (until the next standard is released, which takes a long time). The actual physical implementation differs on different compilers and operating systems, but you shouldn't worry about it. From your perspective, as long as you are using a reasonably new compiler (less than 8 years old), and you write standard code, your code will have the same requirements and work the same on any machine with any compiler.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Is this like a library named 'std'?
    Conceptually, yes. std represents the entire C++ standard library. However, physically, different components of the library reside in different files, so you can import only the parts of the library you need piecewise. Input/output functions and objects like cout and cin are declared in a file named "iostream". String related objects and functions can be found in another file called "string", and so on.

    Physically, a namespace is used to resolve conflicts. Say you have your own object called cout. Even if you import library code from <iostream>, the standard version is called std::cout, not cout, so there are no conflicts. This is why many programmers (including me) advise to not use the using directive excessively (there are exceptions) as it just dumps the carefully-placed contents of the namespace back out again, effectively defeating the very purpose of namespaces in the first place.

    For example, if I need #include <iostream> before I use cout, how do I know iostream is 'related' to cout? How would I find out what else is 'related' to #include <iostream>?
    By reading references. Get a book or bookmark some sites. cppreference.com contains just about everything in the C++ library and how to use them. Or try this nifty new site named after the number with a hundred 0s.

    Do the contents of <iostream> ever change?
    Taken from the preface of WG14-N1124 C language draft:
    1. This International Standard specifies the form and establishes the interpretation of programs written in the C programming language. It specifies
    - the representation of C programs;
    - the syntax and constraints of the C language;
    ** etc... **

    2. This International Standard does not specify
    - the mechanism by which C programs are transformed for use by a data-processing system;
    - the mechanism by which C programs are invoked for use by a data-processing system;
    ** etc... **
    Basically, what sort of code the compiler or library provides is to be treated as completely irrelevant, as long as the code adheres to the standard guidelines. Which means you should also adhere to the standard guidelines and not depend on how your particular compiler or library works. It's amazing how many newbies give us grief because they don't understand this little phrase.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    > It's amazing how many newbies give us grief because they don't understand this little >phrase.


    We were all a newbie once....
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 06:37 AM
  2. newbie questions
    By raptorx in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 09:30 PM
  3. I have some newbie questions
    By Myra Mains in forum C++ Programming
    Replies: 6
    Last Post: 05-06-2002, 09:30 PM
  4. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 08:27 PM
  5. newbie questions again
    By dune911 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2001, 02:43 PM