473,737 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

everywhere, i need to use std::

[1] using namespace std

In my C++ program, even after applying [1], I need to use the std
namespace with the scope resolution operator, like, std::cout,
std::vector. This I found a little bit cumbersome to always include
std.

I somewhere found a trick to overcome this problem. By using

using std::cout;
using std::vector;

rather than using [1].

is it always done like this or is there any other concept that I still
do not know.

regards,
vijay.
Jul 23 '05 #1
15 3676
You can just add the statement:

using std;

Then you can use cout, vector etc. directly.

Deepa
--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Enter model in plain text;generate sequence diagram in PDF/Word

Jul 23 '05 #2
drdoubt wrote:
[1] using namespace std
using namespace std;

In my C++ program, even after applying [1], I need to use the std
namespace with the scope resolution operator, like, std::cout,
std::vector. This I found a little bit cumbersome to always include
std.

I somewhere found a trick to overcome this problem. By using

using std::cout;
using std::vector;

rather than using [1].

is it always done like this or is there any other concept that I still
do not know.

If you place using namespace std; in the beginning of a scope, there is
no use for explicit scope resolution in that scope.
For example the following should compile successfully with your compiler:
#include <iostream>
int main()
{
using namespace std;

cout<<"Hello world!\n";
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3
EventHelix.com wrote:
You can just add the statement:

using std;

Then you can use cout, vector etc. directly.

using namespace std;

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

"EventHelix.com " <ev********@gma il.com> ????
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
You can just add the statement:

using std; A little mistake, should be
using namespace std;

Then you can use cout, vector etc. directly.

Deepa
--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Enter model in plain text;generate sequence diagram in PDF/Word

Jul 23 '05 #5
[1] using namespace std

But here we need to [1] in every other function where cout, vector may
be used. The problem is that it then becomes a little bit redundant
because we either have to use scope resolution or place [1] in every
function definition.

I just wanted to know whether I can just place [1] in the beginning of
the file and not use scope resolution anywhere else in the program.

regards,
vijay.

Jul 23 '05 #6
vijay wrote:
[1] using namespace std

But here we need to [1] in every other function where cout, vector may
be used. The problem is that it then becomes a little bit redundant
because we either have to use scope resolution or place [1] in every
function definition.

I just wanted to know whether I can just place [1] in the beginning of
the file and not use scope resolution anywhere else in the program.

You mean to place it in the global scope and thus bring everything
defined in namespace std in the global namespace.
Well, namespace mechanism exists so as to be able to use different
facilities from various libraries.
Suppose you have another library or platform providing some other string
type with the name "string".
If you have not such concerns, then I guess you may place the facilities
you want in the global namespace, however you must keep in mind what the
namespace mechanism exists to offer and what problems may arise in the
future.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7
Ioannis Vranos wrote:
If you have not such concerns, then I guess you may place the facilities
you want in the global namespace, however you must keep in mind what the
namespace mechanism exists to offer and what problems may arise in the
future.

The preferred method however is to use the using namespace std; and
using std::some_facil ity; in as a small scope as possible, for example:

#include <iostream>
#include <ostream>
#include <vector>


void printfunc(const std::vector<int > &someVec)
{
using namespace std;

for(vector<int> ::size_type i=0; i<someVec.size( ); ++i)
cout<<someVec[i]<<endl;
}
int main()
{
using std::vector;

vector<int> vec(10,2);

printfunc(vec);
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #8
On 22 Feb 2005 10:10:31 -0800, vijay
<Ta***********@ yahoo.com> wrote:
[1] using namespace std

But here we need to [1] in every other function where cout, vector may
be used. The problem is that it then becomes a little bit redundant
because we either have to use scope resolution or place [1] in every
function definition.

I just wanted to know whether I can just place [1] in the beginning of
the file and not use scope resolution anywhere else in the program.


You certainly can, it's just the same as declaring something extern.
That's what I do, I see no point in hiding the std namespace (doing so
makes the code unreadable with std:: in front of everything). You can
do the same with other namespaces if you want as well.

Chris C
Jul 23 '05 #9
On Tue, 22 Feb 2005 20:19:43 +0200, Ioannis Vranos
<iv*@remove.thi s.grad.com> wrote:
Suppose you have another library or platform providing some other string
type with the name "string".
Then you have a problem if you want to use std::string as well. I would
do using na,espace std; globally and get the individual things I needed
from the library separately, probably doing

typedef otherlib::strin g OtherStringT;

to get at the one in the other library.
If you have not such concerns, then I guess you may place the facilities
you want in the global namespace, however you must keep in mind what the
namespace mechanism exists to offer and what problems may arise in the
future.


It's normally safe to include namespace std in a module, as long as you
don't also globally include another namespace as well.

Chris C
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2238
by: asj | last post by:
luke: BORRRRRRRRING......the most interesting initiatives are happening on the client side - in small wireless devices such as cellphones and smartphones, where J2ME has become the de facto standard for application development. Article from Javaworld.com: "Java everywhere is for world domination" http://www.javaworld.com/javaworld/jw-08-2003/jw-0822-wireless.html? Some quotes:
5
1946
by: Mariusz Sakowski | last post by:
Can someone translate this code to a C++? repeat file.Read(bufor, 1); for p := 1 to KeyCount do begin buf := buf xor Keys; end; targetFile.Write(bufor, 1); until file.Size = file.Position;
8
2843
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl list class must Ioverride in order for this to work?
3
2827
by: Ramon F Herrera | last post by:
Greetings++, folks: I am really desperate on this search. I have been looking all over for a sample code that uses the GPGme (GPG Made Easy) SDK library to decrypt a file. I finally found the code (see below), but it doesn't compile with gcc. Could you folks translate it to C, please? I specially need the callback function that handles the passphrase.
7
1748
by: berkay | last post by:
i have a txt file; berkay#white jack#black smith#jane writes in it. and after i run the program it only prints smith jane and crashes what is wrong?
4
1127
by: Learnicus | last post by:
No matter where i choose to create a new application VS2005 tells me that a webiste already exists there. Any ideas? Lenny
1
2093
by: robinsand | last post by:
I am a new C++ programmer. I am still having trouble with certain data types and constructors, among other things. I'm not sure if I've used "std::string" properly throughout this program. I need to fix that, as well as add exception-handling, make sure everything is documented properly, and make certain that the code compiles and runs correctly. I'm under a very tight deadline, as this needs to run perfectly within the next 24 hours.
5
3007
by: prakash.mirji | last post by:
I am using evaluation copy of RW 9.0 for porting one of C++ application on RHEL4 (x86 platform). We are getting some issues into RW template classes. Please need assistance on this issue. Here is the problem: Application is using below mentioned template class which is declared at static in Engine.h
10
2115
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes #these are the operations + = , - = , * = , 1/ = only if 0 not in .
0
9331
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9257
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6052
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3278
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.