How to avoid typing std:: (scope resolution operator)

This is a discussion on How to avoid typing std:: (scope resolution operator) within the C++ Programming forums, part of the General Programming Boards category; When I include iostream library, I am typing std::cout everytime I send something to display. Is there a way to ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    How to avoid typing std:: (scope resolution operator)

    When I include iostream library, I am typing std::cout everytime I send something to display.

    Is there a way to avoid this and simply use cout instead of
    std::cout. I thought of mapping std::cout to cout (using define). But that would probably be akward.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Very simple answer. here is an example

    Code:
    #include <iostream>
    
    using namespace std;   // this declares cout, endl cin and a few other standard functions
    
    int main()
    {
    cout << "Hi sharon this is the answer!" << endl;
    
    cin.get();
    
    return 0;
    }
    Another way but requires more typing is to declare all std:: declarations before you use them, like this:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main()
    {
    // code
    return 0;
    }
    Either version works, but stick with the top one if it means less work! lol - pete

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That's simple enough, just use the "using" directive:
    Code:
    using std::cout;
    //or even
    using namespace std;
    Edit: Too slow...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    right after your includes stick either
    using std::cout;

    or
    using namespace std;

    With the first method you will have to do similar things for everything that resides in the standard namespace, the second on the other hand will bring bring everything in the standard namespace. Choose your preffered method. Now if you only want function-scope you can also tuck that inside a function.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Recommended if you want to save typing:

    Code:
    #include <iostream>
    
    //nothing here...
    
    void fn()
    {
        //then...
        using namespace std;
        cout << "Stuff!";
    }
    
    int main()
    {
        std::cout << "This produces an error!";
    }
    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;}

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you should generally use using std::cout; instead of using namespace std; because the latter is a sloppy way of doing things. For beginner programs it's fine, but It's not a habit I would get into.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by major_small
    For beginner programs it's fine, but It's not a habit I would get into.
    Is there any reason why you shouldnt use using namespace std?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    There is a way to cut down on scope declarations that's much easier too. Take a look at this:
    Code:
    copy(list.begin(), list.end(), std::ostream_iterator(std::cout, "\n"));
    Because copy() takes std parameters, the compiler assumes copy() is in the std namespace too. It'll work for other things in a namespace too.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For those who may be interested in further details about what citizen mentioned, (and to make Googling for details a bit easier) I believe it is called "Koenig lookup". I first head of it here.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  2. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 10:17 AM
  3. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 01:38 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. What is a scope operator?
    By Gamma in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21