473,507 Members | 6,295 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 8130
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_reference
n_th ( std::set<T,C,Aconst & s, typename std::set<T,C,A>::size_type n ) {
typename std::set<T,C,A>::const_iterator 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
2895
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...
4
2170
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 = !!...
0
1291
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...
3
8657
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...
1
1389
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...
0
898
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...
1
2491
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...
0
955
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...
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. ...
0
7105
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...
0
7308
Oralloy
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,...
1
7023
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...
0
7479
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...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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 ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.