473,769 Members | 8,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Obtaining the position of an element with a collection

Hello.

To find the position of an element within a collection, I currently
follow these two steps:

1. obtain an iterator to the element (i.e. find it)
2. use std::distance with coll.begin() and the previous iterator

This works, but I still would like to know if there is a better way
which I might be overlooking.

Thank you,

--
Ney André de Mello Zunino
Jul 22 '05 #1
2 1761

"Ney André de Mello Zunino" <zu****@inf.ufs c.br> wrote in message news:2u******** *****@uni-berlin.de...
Hello.

To find the position of an element within a collection, I currently
follow these two steps:

1. obtain an iterator to the element (i.e. find it)
2. use std::distance with coll.begin() and the previous iterator

This works, but I still would like to know if there is a better way
which I might be overlooking.

Thank you,

--
Ney André de Mello Zunino


It depends on what kind of "collection ". With vectors and deques, the
index is the postion. With maps, "position" is not a very useful concept
because lookup is done (behind the scenes) using b-tree technology.
With lists, there is no index and no random iterators, so the fastest
way to find effective "index" or "position number" would be a linear
search:

list<std::strin g> Blat;
// (put stuff into list here)
list<std::strin g>::iterator Bob, Jack;
Bob = Blat.find("Vani lla");
int i=0;
for (Jack = Blat.begin(); Jack != Bob ; ++Jack)
{
++i;
}

The value of i will now be the effective "index" of the first occurence
of "Vanilla" in your list.


Jul 22 '05 #2
Ney André de Mello Zunino wrote:
To find the position of an element within a collection, I currently
follow these two steps:

1. obtain an iterator to the element (i.e. find it)
2. use std::distance with coll.begin() and the previous iterator

This works, but I still would like to know if there is a better way
which I might be overlooking.

For containers with random iterators you can subtract the iterators,
which is what std::distance implementation for random iterators will
probably do too.
So the answer is, your approach sounds OK. :-)

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3

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

Similar topics

1
2595
by: Eshrath Ali Khan | last post by:
Hi, I have a requirement where I am transforming a XML into a html using an XSL. I need to create a object for the each and every occurrence of an element named "tgroup" in the xml. Also I need to assign a unique id for this object based upon the its occurence in the xml document. (note: the unique id should not be assigned depeneding upon its position with respect to the parent element but with respect to its number of occurence in the...
24
31400
by: Jeff Johnson | last post by:
Hi All, I'm setting a <div> on a page on a lengthy page with a great deal of text. I want the div to land where the user can see it. I need to capture the vertical scrollbar position and use that integer to set the 'Y' coordinate for the div. Can this be done? If so, how? If not, any other suggestions?
14
21928
by: Zenobia | last post by:
Hello folks, Is it possible to position an item within a <td> element? For instance see below. The table has multiple rows, one for each database record. Each row has 3 hrefs associated with it (for edit, delete and update). I want to position these 3 href items in the same columns, which would be posible if the span could be positioned - for instance on the extreme right of the td in which it is contained.
3
19783
by: Markus Ernst | last post by:
Hello Reading the follwing document: http://www.w3.org/TR/WD-positioning-970131#In-flow it seems very clear that position:relative should be relative to the parent element. So in the following test case element1 and element2 should be placed side by side inside a centered white container element: http://www.markusernst.ch/test.htm
2
5709
by: gregl | last post by:
Anyone know how to obtain the true size of the html document's body? The control contains the size that the control was set to. The body object appears to contain the same size. That information must be stored somewhere in order for the scroll bars to be enabled when the body is larger than the container. Also, I have noticed that if I initialize the control with a large size, the vertical scrollbar is disabled, but is still visible,...
3
4084
by: axlq | last post by:
I wrote the function below to get the vertical scroll position of an anchor. That is, a URL of the form http://www.example.com/mypage.html#anchorname should scroll to the point on the page that has an anchor <a name="anchorname">...</a>). Doing this in Javascript is necessary in the presence of a dynamic HTML page having some hidden blocks. DHTML messes up the scroll position in Opera and Mozilla (but not IE) so I'm trying to fix it...
6
2622
by: honguin | last post by:
Hi, The current scenario is that I have an iframe inside an asp.net web page where I periodically check the url inside the iFrame. I know that all this information can be obtained in the client-side during a page load. Does anyone know an efficient way to obtain the current url of the src attribute of the iFrame? Or if there is a better solution? Many thanks
2
12617
by: anumsajeel | last post by:
Hi, Error Message:- error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. Target Site:- MySql.Data.MySqlClient.Driver GetConnection() Error Source:- MySql.Data
0
10219
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, 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...
0
10049
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
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,...
0
9865
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
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...
0
6675
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();...
0
5310
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
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
3
2815
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.