I am not sure if this is something that is covered by the Standard, or
if it's an implementation detail of my Standard Library.
I am reading in a large amount of data into a std::set. There is an
overload for std::set::inser t() that takes in an iterator as a hint as
to where the new value should be inserted, and my implementation
(Dinkumware) says that if the hint is good (meaning the iterator points
immediately before or after where the inserted value should go) then the
insertion can happen in amortized constant time rather than logarithmic
time.
I would like to take advantage of this fact. My input data should
already be in sorted order, therefore I think I can use my_set.end() as
the hint to insert(). Is this a valid assumption, or does this depend
on how std::set is actually implemented?
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Oct 26 '06
12 3523
Pete Becker <pe********@acm .orgwrote:
Marcus Kwok wrote:
>Mark P <us****@fall200 5remove.fastmai lcaps.fmwrote:
>>Keep in mind that inserting elements into a set in sorted order may be far from optimal and that the run time may be dominated by the rebalancing required to maintain the red-black tree.
Hmm, OK, in that case let me add something to my situation. I have a large list of data that I am adding to my container in sorted order. There is a possibility of duplicates, but we do not want to add duplicates. Initially, I was pushing the data to the back of a vector, but checking if the element was already in the vector before pushing it back. This repeated searching was very inefficient and was what caused me to redesign it using a set, since I can just insert without needing to check before. Using a set instead of the previous method with vector caused a 4-5x speed increase.
Do you have another suggestion for something I can try?
std::unique or std::unique_cop y.
Thanks, I'll look into them.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
David Harmon <so****@netcom. comwrote:
If your data is sorted, then you only need to check if each item is
the same as the previous, right? And at the same time, a check if
the item is "less than" the previous will verify that it really is
sorted.
That's a good idea. Unfortunately, I should have said that our data
"should be" sorted before reading in, but there is the possibility that
something might come in out of order... though this possibility is
rather low, I still need to handle it properly in case it isn't.
Thanks for your input.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Marcus Kwok wrote:
David Harmon <so****@netcom. comwrote:
>If your data is sorted, then you only need to check if each item is the same as the previous, right? And at the same time, a check if the item is "less than" the previous will verify that it really is sorted.
That's a good idea. Unfortunately, I should have said that our data
"should be" sorted before reading in, but there is the possibility that
something might come in out of order... though this possibility is
rather low, I still need to handle it properly in case it isn't.
Thanks for your input.
So apply std::sort to the initial data, then apply std::unique or
std::unique_cop y as Pete Becker suggested.
As a rule of thumb, if the data only needs to be sorted once, then a
dynamically sorted container such as std::set is probably overkill
(read: inefficient). std::set is nice if you need to sort "online", but
red-black tree algorithms are much more complex than simple sorting. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ma740988 |
last post by:
The position returned via the STL std::set container never made much
sense to me. When you insert elements within the container, the
position returned - via find - does not reflect the actual position of
the value within the container. std::string - for instance - does.
How then do I get the actual position of the value when using
std::set?
# include <iostream>
using std::cout;
using std::cin;
|
by: snnn |
last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as
iterator set::begin() const.
However, why should a const object returns a non-const iterator?
Then, I found, in this book, the semantic of set::iterator is defined as same as set::const_iterator. Both of them must be const!
I tried to read the source of GNU STL(version 3.4.1).They were using a red-black tree to implant it (std::set has a...
|
by: Peter Jansson |
last post by:
Hello,
I have the following code:
std::map<int,std::set<std::string> > k;
k="1234567890";
k="2345678901";
//...
std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)
throw(std::runtime_error)
|
by: danibe |
last post by:
I never had any problems storing pointers in STL
containers such std::vector and std::map. The benefit
of storing pointers instead of the objects themselves
is mainly saving memory resources and performance (STL
containers hold *copies* of what's passed to them via
the insert() method).
However, I am not sure how to accomplish that using
std::set. For various reasons, I cannot use vector or
map in my application. But I would like to...
|
by: Cory Nelson |
last post by:
Does anyone know how std::set prevents duplicates using only std::less?
I've tried looking through a couple of the STL implementations and
their code is pretty unreadable (to allow for different compilers, I
guess).
| |
by: shuisheng |
last post by:
Dear All,
std::set is sorted. So I am wondering is there any fast way to access
(sucn as random access) to its elements just like std::vector.
Assume I have a set
std::set<inta;
So I can easily get access to such as a.
|
by: Renzr |
last post by:
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me help, thank you. I am a freshman about the STL. The following is
the code.
#include <set>
#include <iostream>
#include <vector>
int main()
{
|
by: mathieu |
last post by:
hi there,
I would like to know if the following piece of code is garantee to
work. I am afraid that the cstring address I am using in the std::map
found from a request in std::set is not garantee to remain the same as
the std::set grows...
thanks
-Mathieu
|
by: Markus Dehmann |
last post by:
I want to derive from std::set, like shown below. But when I try to
declare an iterator over the contained elements I get an error, see
the twp uncommented lines:
#include <set>
template<class T>
class MySet : public std::set<T>{
public:
MySet() : std::set<T>() {}
void foo(){
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |