|
|
Comments and Discussions
|
|
 |
|

|
I don't think this project is available any longer. I get an obtuse "Forbidden" error from Google when I follow this link.
|
|
|
|

|
From an amateur C++ programmer who needs the functionality of a sorted vector: is this supposed to compile on linux as well? it won't with
g++ -o -c main.o main.cpp
but is that because I need to include some libraries? If so, which ones?
Thanks very much.
|
|
|
|

|
First, thank you. This class will be very useful.
I do need to point out, though, that if your class exposes the internal container, it also should detect when that container becomes "dirty" (i.e. if a non-const method is called on the underlying container). Then sorted_vector can call sort() internally (which clears the "dirty" flag) when any member function that relies on the internal order is called. This would, of course, require declaration of vec_ as mutable, since it may need to be resorted in const member functions (like iterator begin() const).
An alternative approach would be to assume that, if a large number of inserts are to be done consecutively, then the iterator-range version of insert() will be called. This function can be optimized with the multiple-push_back()-and-sort() algorithm.
We know we're reinventing the wheel. We're just trying to do it better.
|
|
|
|

|
Thanks a lot for your remarks.
If find the idea to replace the low level interface by
an appropriate handling of the "insert range" function very appealing - on the other hand it will break the existing interface.
In any case I will provide a new version of the sorted vector which runs on the various MSVC versions.
Martin
|
|
|
|

|
Hi,
I found and tested your sorted_vector using MSVC7.1. There is one important change in template syntax: the obligatory use of the keyword typename in certain places. In order for the file to compile, the following lines need to be changed:
typedef typename Cont::allocator_type allocator_type;
typedef typename Cont::size_type size_type;
typedef typename Cont::difference_type difference_type;
typedef typename Cont::reference reference;
typedef typename Cont::const_reference const_reference;
typedef typename Cont::value_type value_type;
...
typedef typename Cont::iterator iterator;
typedef typename Cont::const_iterator const_iterator;
...
typedef typename Cont::const_reverse_iterator
const_reverse_iterator;
typedef typename Cont::reverse_iterator reverse_iterator;
Thanks for the article!
Gabor
|
|
|
|

|
I have an object a
class object
{
private:
string streetname ;
vector listofhomes ;
}
i want to use STL template to build a program like
this class will have a
{ reed street,
list of houses in this street( names).
}
I want this to be collection. Like
{ streetname, list of houses)
(streetname, list of houses)...
How do i do it ?
Thanks in advance.
jeevan
|
|
|
|

|
you have some of the worst coding i have ever seen.
go get a life!!
Arash
|
|
|
|

|
Such insight. Such wisdom. How helpful you are. I bow before you.
In other words, if you've got a comment to make, try to make it a helpful one, not an arrogant one. You're only making an idiot of yourself.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not
as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|

|
Hey, before you flame on others, go get yourself a good psychiatrist
|
|
|
|

|
This implementation is clever and very useful for appropriate situations. One must be careful however. In addition to the disadvantage of not preserving iterators over insert/delete operations, there should be a performance hit on insert/delete operations themselves, even if you use push_back() and then sort() afterward. The first call to sort() will work fine, but after that another sort() can do no better than O(N). This is because the container contents are already mostly sorted to begin with, causing a worst-case condition on the sort algorithm. If your code does a lot of inserts and deletes througout the program, it may be better to use a set/multiset. If you do use a sorted_vector, I think it is fastest to use push_back() and sort() when inserting many items in sequence, but use insert() when inserting one item or only a few. A "few" is defined as few in comparison to the number of items already sorted in the container.
As a performance check of this phenomenon, do the following. Fill the container with 100 items using push_back(), but do not sort them. Make sure they are in some random order. Now add one item with push_back() and call sort(). The time to insert the item and sort is the benchmark, and this first test should be the fastest. Make sure the inserted item lies somewhere in the middle of the sorted set. Now take the same 100 items but sort them to begin with. Then add the one item again with push_back() and call sort() again, timing only the insertion and sort. It should end up much slower. Now take the 100 items again in initial sorted order and add the one item using insert() only, and time the insert() call. If my hunch is correct, it will do better than the second test, and may even be closer to the first. I haven't tried this myself - its just theory.
Ray Virzi
|
|
|
|

|
Thank you for your comment, Ray Virzi.
It shows much insight into the problems of
STL containers and especially vectors, which
are usually implemented as one single memory
block, requiring memory moves or copies, when
inserting into the middle of the sequence.
I think, that a sorted sequence should not be
used, when insertions and retrieval operations
alternate each other, but think (in accordance with you)
that a sorted sequence is useful even for large sets,
provided that you initially build the set and then
later on onyl retreive from the set.
Regards Martin
|
|
|
|

|
The std namespace is reserved for the standard C++ library and library implementor extensions. Anyone else adding to this namespace is prohibited by the standard.
From Section 17.4.3.1, para 1
"It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std. Such a specialization (complete or partial) of a standard library template results in undefined behavior unless the declaration depends on a userdefined name of external linkage and unless the specialization meets the standard library requirements for the original template."
|
|
|
|

|
Reading this back after submitting it, it sounds a bit curt. Sorry about that, it wasn't meant to be
|
|
|
|

|
Your interface describes some template members that take templated iterators, but the code only handle vector iterators, and doesn't have templated members.
You should either change your code, or your interface documentation.
|
|
|
|

|
Hi,
you are right, the members you are talking about were written by me as template members initially ,
but on some VC6 versions (depending on the SP) this resulted in an internal compiler error
(one of the constructors caused it).
template<class InIt>
sorted_vector(InIt first, InIt last, const Pred& comp = Pred(), const A& al = A());
Because of that I changed the code to a non templated version.
On VC7, there was no such problem.
I think I will change the code in a way,
that templated members are used on VC7 but not on VC6 (using condidtional compilation).
Expect an update in the next days.
Recently there was a poll asking whether full standard C++ comformance is important.
I think it is important. Certainly, if you want to
write your own STL components and if you want to use
third party software.
Useful libraries like 'boost' or 'Loki' will
not compile at all under VC6 and must be modified to compile under VC7.
Regards Martin
|
|
|
|

|
I know this is a problem in VC6 - this is why I was curious to see how you implemented the interface you gave.
I thinking sometime to put to use such vector instead of associative containers (map & set) following Scott Meyers advise in Item 23 of Effective STL. Meyers give a list of reasons why sorted vector is better - you can add it to your list of resources
The only problem I have replacing map<> with sorted_vector > is the constness issue, where map<> iterator point to pair and sorted_vector > iterator points to pair. Do you have an idea how such a problem can be solved? I want the compiler to guard against invalid change of keys.
|
|
|
|

|
Intresting,
I really tried to preserve the constness of
the key, but was not able to get a program,
which compiles. I even think it is nearly impossible
to store a const element in a vector. But it
is possible to preserve the constness when using a tree, because you
construct the tree element once and never change
or move it later.
Please tell me, if you see a solution to this problem.
Regards Martin
|
|
|
|

|
Martin Holzherr wrote:
Useful libraries like 'boost' or 'Loki' will
not compile at all under VC6 and must be modified to compile under VC7
It is true that a lot of Loki will not compile under MSVC6 or 7.0 because it uses partial template specialization which is long overdue is MSVC. However, a lot of Boost is compilable with these 6.0 and 7.0 because of a huge effort by the Boost developers to provide workarounds for the limitations.
FYI, MSVC7.1 (aka Everett) will compile the Boost libraries without any workarounds (it passes all the Boost regression tests). MSVC7.1 finally supports partial template specialization along with other missing standard language features . In fact, Boost was used as a test bed for early alpha releases of the compiler.
|
|
|
|

|
Craig Henderson wrote:
FYI, MSVC7.1 (aka Everett) will compile the Boost libraries without any workarounds (it passes all the Boost regression tests). MSVC7.1 finally supports partial template specialization along with other missing standard language features
Thanks for the info on Boost and MSVC7.1. Do you know if MSVC7.1 compiles Loki?
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
A template container which implements set/multiset functionality using a vector
| Type | Article |
| Licence | |
| First Posted | 20 Nov 2002 |
| Views | 100,624 |
| Downloads | 831 |
| Bookmarked | 27 times |
|
|