473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1737

"Ney André de Mello Zunino" <zu****@inf.ufsc.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::string> Blat;
// (put stuff into list here)
list<std::string>::iterator Bob, Jack;
Bob = Blat.find("Vanilla");
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
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...
24
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...
14
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...
3
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...
2
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...
3
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...
6
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.