implementation of the set - c++

Is it possible to find in the net the full implementation of the STL Set, particularly I'm interested in the iterator?

Under Visual Studio, an easy way is to right click on a #include <set> and "Open Document" : the IDE will search the include paths for you and open up the file regardless of your installation directory
The sources for libstdc++ are available and can be easily browsed online (as a matter of fact, I'm often referring to this site as a documentation) : the code for set can be found here.

If you have a C++ compiler you should be able to look in the <set> header file.

You can also browse the SGI STL online here: http://www.sgi.com/tech/stl/set.

If you are having a hard time locating the implementation of set, then you will probably find it very difficult to understand what you are reading when you eventually do find it. The STL is well known for being one of the hardest bits of C++ code to understand, and this is especially true of the more complex container classes it provides (std::find is actually pretty easy, by comparison).
Instead, it might be an easier task to look at some example code written for use by normal humans, instead of the inscrutable code found in the most common STL implementations. This looks promising, and compares pretty well with what std::set actually really does.

Related

How to determine which header files to include?

Say I have the below (very simple) code.
#include <iostream>
int main() {
std::cout << std::stoi("12");
}
This compiles fine on both g++ and clang; however, it fails to compile on MSVC with the following error:
error C2039: 'stoi': is not a member of 'std'
error C3861: 'stoi': identifier not found
I know that std::stoi is part of the <string> header, which presumably the two former compilers include as part of <iostream> and the latter does not. According to the C++ standard [res.on.headers]
A C++ header may include other C++ headers.
Which, to me, basically says that all three compilers are correct.
This issue arose when one of my students submitted work, which the TA marked as not compiling; I of course went and fixed it. However, I would like to prevent future incidents like this. So, is there a way to determine which header files should be included, short of compiling on three different compilers to check every time?
The only way I can think of is to ensure that for every std function call, an appropriate include exists; but if you have existing code which is thousands of lines long, this may be tedious to search through. Is there an easier/better way to ensure cross-compiler compatibility?
Example with the three compilers: https://godbolt.org/z/kJhS6U
Is there an easier/better way to ensure cross-compiler compatibility?
This is always going to be a bit of a chore if you have a huge codebase and haven't been doing this so far, but once you've gone through fixing your includes, you can stick to a simple procedure:
When you write new code that uses a standard feature, like std::stoi, plug that name into Google, go to the cppreference.com article for it, then look at the top to see which header it's defined in.
Then include that, if it's not already included. Job done!
(You could use the standard for this, but that's not as accessible.)
Do not be tempted to sack it all off in favour of cheap, unportable hacks like <bits/stdc++.h>!
tl;dr: documentation
Besides reviewing documentation and doing that manually (painful and time consuming) you can use some tools which can do that for you.
You can use ReSharper in Visual Studio which is capable to organize imports (in fact VS without ReSharper is not very usable). If include is missing it recommends to add it and if it is obsolete line with include is shown in more pale colors.
Or you can use CLion (available for all platforms) which also has this capability (in fact this is the same manufacture JetBrains).
There is also tool called include what you used, but its aim is take advantages of forward declaration, I never used that (personally - my team mate did that for our project).

Decoupled version of hash_map/unordered_map

I'm writing an embedded application, and the environment I use does not, unfortunately, have C++11 support at present.
I need to implement a hash/unordered map (a regular std::map won't do for performance reasons), but can't seem to find a way to do it cleanly.
Boost doesn't want to work without bringing in practically the whole library. Even the original STL hash_map from SGI wants several headers, and duplicates standard library functionality, causing ambiguous function calls. It's a real mess.
For ease of implementation, versioning, quality control, V&V, etc. I really need something that leverages the existing standard library and exists in only a few header files that I can put right in the same folder as all the other source/header files. Does such a thing exist, or am I without hope? I've searched for a long while, but have come up empty-handed.
Thanks very much for any help. I can certainly clarify further if necessary.
Did you look at the GNU implementation? On my Ubuntu Machine, unordered_map.h does not include anything. This file is located at
/usr/include/c++/4.6/bits/unordered_map.h
which is about 400 lines although the file "unordered_map" in /usr/include/c++/4.6/ has more headers but you can tweak those I guess.
I think you can find the source code for implementation from GNU.org (?) and compile it yourself?

C++ #include statement

I am a Java developer and C++ beginner. In Java, I can import objects easily using (Ctrl + Shift + O). In C++ however, I have to manually type #include each time, wasting my time. In addition, I often don't know where the required object is.
Is there an easy way to import or type "#include" automatically? If not, is there a plug-in or add-on to do that? I am using Eclipse IDE for Blackberry 10, along with C++ Cascades.
This is just how C++ works. The #include functionality is primitive compared to a Java import: each #include is simply replaced by the text of the included file (and so on, recursively) as if it had been copied and pasted in there.
This sometimes has advantages, and it's certainly simple, but it does mean that there's no reliable way to know ahead of time what is defined by a particular included file. So, if you need the vector type, for example, that is in vector; but if you need the va_list type, that is in stdarg.h. Generally, things are reasonably consistent, but not perfectly so, and there's nothing to enforce it anyway. This is probably why most IDEs don't provide much help for it. You just need to know what the rules are (if there are any) for the libraries you're using.
IDE support for C++ is generally not as good as it is for Java or C#. This is one example (there are plenty of other ones). If you are expecting a Java or C#-level of assistance, you are likely to end up disappointed. On the plus side, while sorting out the #include list is annoying, there are lots of other difficulties encountered when working with C++, so it rarely ends up the main problem.
see this bug report.
It seems that people have been discussing this for about 10 years but it's not implemented yet.
Personally I believe as a C++ programmer you should be trying to eliminate excessive use of include's in your files and use forward declarations instead therefore it's not a feature many programmers are looking for. If you prefer not to have that much control over the program, you can always code in java or c#.

Handling Deprecated Includes With Autoconf

I have been using ext::hash_map in a C++ project and notice that in g++ 4.3 it is deprecated in favor of unordered_map. I need to maintain compatibility with old systems that do not have unordered_map available. It occurs to me that this is just the sort of thing that could be handled by autoconf, which I am already using. I have been unsuccessful in finding documentation on doing so, however.
I would guess that I need to do something like the following:
- Replace all instances of ext::hash_map in my code with MY_HASH_MAP
- Replace all instances of ext/hash_map in my code with MY_HASH_INCLUDE
- Include a line in configure.ac using some combination of AC_CHECK_HEADERS and AC_DEFINE
I have not been able to figure out exactly the proper autoconf magic to make this work, and am wondering if it is even the best solution anyway. Surely this is the sort of thing that many other people will have dealt with and there might be some standard solution that I have been unable to find?
So, three related questions:
- Is there a standard way of handling this that I should be using?
- How do I do this in autoconf?
- Should I just use -Wno-deprecated and assume the C++ standard is not going to be updated within my lifetime?
You could use AC_CHECK_HEADERS([my_header_file]) to see which files are present -- then create a new class MyApp::hash_map which depending on how what defines are used wraps the functionality accordingly.
I would consider leaving the code as is and turning off the 'deprecated' warning instead, especially if you have to support older systems where you only have ext::hash_map available.
IIRC ext::hash_map is not part of the standard anyway, so the main harm that could be done is that (eventually) the G++ maintainers will remove support for it. However if you rejig the code to include both hash_map and the tr1 unordered_map, you've suddenly doubled the testing effort for this particular bit of code. Unless there is a specific reason that might require you do duplicate the effort, save it for something more worthwhile.

list of public functions/classes with their corresponded header files

I tried to find a place where I can find ready to copy list of all functions and classes available in each stl header file.
Looking through /usr/include/c++ is not so convenient as I expected.
Google very often shows
http://www.cplusplus.com/reference/
which is not so convenient to copy and paste.
Does anyone knows a good place to look?
Thanks.
Edit:
This is for an "auto stl header" plugin. So I don't need an examples. Just correspondence of each std::xxx to <yyy>.
For Vim users and those who might be interested
Using half given links and the std header files I've created auto std include vim plugin. I can't choose right one answer by now. But the method with which I can receive such kind of information more automatically is still considering by me.
Thanks.
I'm not sure if I understood correctly, but if you need a reference and a list of functions from the headers, then maybe dinkumware manuals. If you want examples then try this. If you want an absolute and the true reference then go to the ISO standard.
I forgot to mention SGI STL programmers guide...
One site that I've always found useful for STL is SGI's STL page.
Have a look in the Index page and the Index by Category page.
HTH
cheers,
SGI has the most comprehensive documentation of the STL (except streams)
A list of of most things is here organised by category:
http://www.sgi.com/tech/stl/table_of_contents.html
A Full list is here:
http://www.sgi.com/tech/stl/stl_index.html
I just use this page:
http://www.sgi.com/tech/stl
Why not run your own implementation's headers through a tool like Doxygen?
Well, everyone's STL may not be exactly ISO standard, but the GCC doxygen output might fit your needs nevertheless. Start here: http://gcc.gnu.org/onlinedocs/libstdc++/
Browsing down through the output, I cannot find any single page that lists all classes and their functions together, but maybe you could write a little wget-based script to aggregate it in the format that you want.
I like the SGI docs.The categorized index seems like it is close to what you want in a list. The main page.