Reading strings into a vector, without using namespace std I have been told that using namespace std is a bad practice (especially in header files). But wouldn't this make the program less readable?

int main()
{
    std::string text;
    std::vector&lt;std::string&gt; svec;

    while(std::cin &gt;&gt; text)
        svec.push_back(text);

    for(std::vector&lt;std::string&gt;::size_type i = 0; i &lt; svec.size(); i++)
        std::cout &lt;&lt; svec[i]  + " " &lt;&lt; svec[i].size() &lt;&lt; "\n";

    return 0;
}


Does this not make the for loop difficult to read, even for a simple program as this? Is there a better way to do this?
I'll assume that you've read Why is using namespace std considered bad practice? so you understand the reasons that this can cause problems for your code.  I'm not going to address that any further in this answer.

You have alternatives, that reduce the risk compared to using namespace std; at file scope:


Selectively import names you're going to use, in the scope you're going to use them:

#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;

int main()
{
    using std::string;
    using std::vector;

    string text;
    vector&lt;string&gt; svec;

    while (std::cin &gt;&gt; text)
        svec.push_back(text);

    for (vector&lt;string&gt;::size_type i = 0; i &lt; svec.size(); i++)
        std::cout &lt;&lt; svec[i]  + " " &lt;&lt; svec[i].size() &lt;&lt; "\n";

    return 0;
}


This is an important technique when using free functions such as begin() and end() in generic code, where you want the std:: implementations to be available, but for argument-dependent lookup to prefer a more specific (local namespace) override if available.
Reduce the need to actually write typenames, with greater use of auto.  For example, your output loop can be

for (const auto&amp; element: svec)
    std::cout &lt;&lt; element &lt;&lt; " " &lt;&lt; element.size() &lt;&lt; "\n";


Using auto more liberally can be very helpful when you decide that a method needs to be generic; you won't need to trawl through it updating the types to match the new signature.


As an aside, note that some namespaces (e.g. std::literals and its contained namespaces) are intended to be imported with a using namespace directive; you may still want to restrict their scope to just that part of your code that needs it, though - and never in a header file!