what is the difference between

std::cout<<"dfdfd"<<endl;//everywhere  

and

using namespace std::cin

putting above line at the top of the main function

and putting the below line at the top of the main

using namespace std;

and + 2 bonus questions.

1.Are there any other namespaces than 'std' in c++?
2. Are cin and cout the only ones in the namesapce 'std'?

try to answer all if you can.thanks

Dani AI

Generated

Quick, practical summary: there are three ways to refer to standard-library names — fully qualify them (prefix with std::), import a single name into the current scope with a using-declaration, or import everything from a namespace with a using-directive. A using-declaration affects only the block or translation unit where it appears and brings just one name into scope. A using-directive makes every name from that namespace visible and is convenient for tiny examples but risky in real code because it can create name collisions and pollute other translation units (never put a using-directive in a header).

Example patterns (safe to use in implementation files or limited scopes):

using std::vector;
vector<int> v;

namespace fs = std::filesystem;
fs::path p("file.txt");

Troubleshooting tips: if you hit "ambiguous" or "redefinition" errors after adding a using, remove it and qualify the name to find the conflict. If a symbol unexpectedly refers to something else, prefer the fully qualified form until you are sure a using-declaration is safe. Also note that manipulators like endl flush the stream; use '\n' when you do not need the flush for better performance.

Answers to the bonus questions: yes — lots of namespaces exist. You and libraries create namespaces all the time (for example Boost, your projects, etc.). The standard library itself has many nested namespaces (for example std::chrono, std::literals, std::pmr, std::filesystem) and hundreds of names beyond just stream objects (containers, algorithms, smart pointers, threading, and more). As pointed out, you can define your own namespace; is right that namespaces are primarily about avoiding name conflicts; and ’s link is a useful tutorial for deeper reading.

Recommended Answers

All 5 Replies

Rather than trying to explain, I'll link you to a article that talks about namespace. In general for simple question you should try to be more independent. Take care.

  1. There are plenty namespaces in c++, manly because everyone can create a namespace:

    namespace something{
    
        //insert your classes, variables, and code here
    
    }
    
  2. cin and cout are few of the items from the std namespace. Basically std namespace conains all the classes and the implementations of the standard C++, meaning it will come with your compiler/package, not requring you to actually implement things.

Other things:

using namespace std;

says to the compiler that you are applying the namespace std to every function that you work with, and is not been implemented by you/you use a standard include

using namespace std::cin;

means that you apply the namespace std only to the cin function:

//code
    cin>>variable1;
    std::cout<<varaible1;
//more code

sorry, it is only (without the 'namespace')

using std::cin

If

All the files in the C++ standard library declare all of its entities within the std namespace,

(above sentence is on http://www.cplusplus.com)
then why is there even a namespace? Becasue all are in one namespace,there won't be any redeclaration problems. It is not like there is another otally different 'cin/cout' in another name sapce. so why is there a std ?

then why is there even a namespace?

That's there so that YOU can use your own namespaces. In large programs, you may face name conflicts (especially if you're just a part of a 100 developer team!). To avoid that you may have custom namespaces for convenience.

so why is there a std ?

Well the C++ standard committe decided to put all the standard objects (variables and stuffs) in one organisation level (that's the std namespace) rather than scattering them over different namespaces like std1, std2, ..., etc.
Their reason was to just show us the feature existed, the real use comes when you use it for your own contexts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.