Avatar of shaolinfunk
shaolinfunk
Flag for United States of America asked on

How do I use "set"?

I get the error:  "error C2065: 'set' : undeclared identifier"

For this line of code: "set <int> sNumberUsed;"

But I included "#include <set>" at the top of my header file.

Am I missing something?
Editors IDEsVisual C++.NETC++

Avatar of undefined
Last Comment
crysallus

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
phoffric

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
crysallus

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
crysallus

All of the standard C++ libraries are part of the std namespace, so you have 3 options.

1. To include the line:

using namespace std;

Open in new window


at the top of the file after your #include's, as phoffric has done, which essentially brings everything declared in the std namespace into the scope of the file in which it is written.

2. Add a using statement to the top of the file after the #include's for each specific thing from the std namespace that you wish to use. This is normally my preferred option, as the std namespace includes a lot of stuff, and I would say it only makes sense to include all of it if you really are using a lot of it. This is what I've suggested above with:

using std::set;

Open in new window


which just brings set from namespace std into the current scope.

3. Don't add any using statement, and simply write std::set everywhere throughout your code whenever you refer to it. So in your case you would have to write.

std::set<int> sNumberUsed;

Open in new window


and so on whenever you use set. This obviously requires a lot of extra typing, and can make the code a bit messier to read. I wouldn't recommend it.
phoffric

Fair summary, crysallus. I see others here at EE like option 3 (but I agree with you - I don't like it either). Option 2 makes sense, but then in large files, I end up with so many using std:xyz statements, that I just go for option 1. But you are right that everything declared in the std namespace into the scope of the file in which it is written. I just treat all standard library terms as reserved words. But, if then bringing in other third party libraries which use these resever words, then the catch-all statement will result in name clashes. So, you are forced to use option 2, which you will likely complete after a dozen compilations until you get all the using statements in correctly.

If two libraries have the word set, and you use them in the same file, then it looks like option 3 will be needed to choose the appropriate one.
crysallus

Fair point about 3rd party libraries. If you really do have name clashes, then option 3 it probably is, but I'd only do it if it's absolutely necessary, not for such a simple example as for this question.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
phoffric

Agreed.
sarabande

there are a few common names like set, list, find, sort, ... in namespace std which might give conflicts when stl is added to existing projects. i don't think there is really a problem with namespaces of other commercial libraries. those are most probably aware of all names in std namespace and won't use any of these. so there is only case left where a developer intentionally (or by ignorance) uses same names as in stl. i would say in those cases it is good when the conflict comes up cause exchanging a class of the stl by an own class of same name is a serious thing which requires much knowledge and discipline and the correct usage of the using clauses surely is a minor point when doing so.

i mostly use std:: prefix cause i always want to make clear that stl functionality is used, specially for novices or programmers not acquainted with stl.

Sara

phoffric

I've seen this from many postings on this site and elsewhere (but fortunately not at work):
  std::cout << ... << std::endl;
I agree with crysallus - it can get messier to read; and ease of reading is essential to quick clear maintenance if you are inheriting such a mess.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Infinity08

Heh, reminds me of this :

        https://www.experts-exchange.com/questions/24646218/should-one-use-'using-namespace-std'-or-not.html

We should probably have more discussions like these ...
shaolinfunk

ASKER
I went with option 2, since I don't know that I use a lot of stuff from "using namespace std"

so far i'm putting "std::" in front string, vector, and set and that's it.

i intend to use pai at some pointr...is that also from the std namespace?
shaolinfunk

ASKER
so many typos in my last post...

i mean to write "in front OF" and to ask if "pair" also came from the std namespace?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
shaolinfunk

ASKER
nevermind...i just tried it and yes, i think pair is also in the std namespace.

given that i'm using string, vector, set and pair...i think maybe now "using namespace std" is justified instead of typing std:: in front of everything.

anyways, i appreciate the discussion here.  discussing the pros and cons of each option was really helpful!
sarabande

yes, it is std::pair :)

Sara
crysallus

@shaolinfunk: Apologies if I've misunderstood you, but it sounds to me from what you've written that you misunderstood option 2 (or I didn't explain it well enough). You only need to put the following at the top of each source file that you are using these classes in.

using std::set;
using std::string;
using std::vector;
using std::pair;

Open in new window

And so on for whatever else from std you need in that source file. If you add these using statements, then you don't need to put std:: anywhere else in that same file that you include the using statement, you can just write set, string etc. without std:: before it. It's just that it sounds to me like you were still putting std:: everywhere throughout your code, which isn't necessary if you have taken option 2 and added those using statements.

Just thought I'd clarify.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.