473,569 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help! How to access to std::set elements?

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[5].

Thanks for your help.

Shuisheng

Sep 22 '06 #1
2 8142
shuisheng wrote:
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[5].
std::set<int>:: iterator i = a.begin();
std::advance(i, 5);
// now use *i

'std::set' is not very suitable for random (indexed) access. Perhaps
you need to think of a different container...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 22 '06 #2
shuisheng wrote:
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.
No.
Assume I have a set

std::set<inta;

So I can easily get access to such as a[5].
As far as easiness is concerned, you could use something like:
#include <set>
#include <iterator>

template < typename T, typename C, typename A >
typename std::set<T,C,A> ::const_referen ce
n_th ( std::set<T,C,Ac onst & s, typename std::set<T,C,A> ::size_type n ) {
typename std::set<T,C,A> ::const_iterato r iter = s.begin();
std::advance( iter, n );
return ( *iter );
}

#include <iostream>

int main ( void ) {
std::set< int s;
s.insert( 5 );
s.insert( 3 );
s.insert( 7 );
std::cout << n_th( s, 1 ) << '\n';
}

For the kicks of syntactic sugar, you could write your own little adaptor to
std::set that would provide operator[] and at() based upon this idea.
If the set implementation of your STL keeps track of subtree sizes, it could
be that advance is specialized so that it takes logarithmic time. I doubt,
however, that this is done in any of the usual implementations : most users
would not benefit but all would bear the costs. Most likely, std::advance()
will be linear for set iterators.
Best

Kai-Uwe Bux
Sep 22 '06 #3

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

Similar topics

2
2901
by: Koen | last post by:
Hi all, I've struggled with the following problem and welcome all a suggestions to solve this. I have a form that list records from a table with observations. The record source for the table depends on from where the form is opened, but always looks like: SELECT tblObservation.* FROM tblObservation WHERE ...
4
2172
by: N. Graves | last post by:
Hello, Please help me I'm still a newbie. I have an unbound text box on a report that I would like to build a expression to display it on the report. Something like txtboxSearchCriteria = !! in the OnPage Event and sometime this will work and sometime it will not. Is there a better simpler way?
0
1293
by: mr_ocp | last post by:
Hi Folks I am trying to hyperlink values in B column to the matching worksheets (Names) using the following routines which is not working, the RsRepSummary record set writes values to B column starting from B2 and there are already matching worksheet names in the same workbook, how can I refer to values in cells of B column - thanks much for...
3
8660
by: Eric Porter | last post by:
Dear All, I have a VB6.COM program using a VB6.COM DLL, which in turn uses ADODB that performs various bits of SQL. I have re-written the VB6.COM DLL in C#.NET (with a VB6 shell), which uses the ADODB.Interop library supplied with .NET. The VB6.COM program above calls this C#.NET library in an effort to perform the equivalent...
1
1398
by: Jeremy Winchell | last post by:
I have an applications that imports new AD users. It creates a folder to store the user profile information, and a folder for their home directory. I would like to set the Permissions on these two folder objects, but I am unsure how. I am fairly new to this side of things so I'm looking for some help or guidance on how to do this. I...
0
899
by: serge calderara | last post by:
Dear all, I have a crystal report template whihc is set by default to point to an SQL server data base and a particular server name. If I run that crystal report, I get a logOn dialog which point to those actual datasource. So far so good. Now I want to dynamically change the server name and the database name dynamically from .NET, for...
1
2500
by: Peter Herath | last post by:
I have created a report using sample codes taken by the forum. one problem is that the report displays the field/column names in the table in columnar format(field names display in rows.) but i want to be able to display filed names and its relevant data in tabular format in the repor but the code itself just displays only the fields in rows and...
0
961
by: ashishbathini | last post by:
Guys I need your help .... In this code I have to change the value of the frequency ... the code takes the values of frequency and bit rate as inputs but i have to enter the value of freq manually ... Plz help as i'm unable to understand this high level code PROGRAM A #!/usr/bin/env python #
3
570
by: VK | last post by:
The official date for the launch of Firefox 3 is June 17, 2008 get Firefox 3 during Download Day to help set the record for most software downloads in 24 hours. http://www.spreadfirefox.com/en-US/worldrecord
0
7615
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...
0
8130
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...
1
7677
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...
0
7979
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...
0
5219
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...
0
3653
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...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.