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

Home Posts Topics Members FAQ

Find the position of an elemant in a vector

Hi,

I've to write a function similar to this.

int process(const vector<int>& vct,int key)
{
// Here I've to find the position of key in the vector and do some
processing.
}

Is there any fucntion in algorithm which directly gives the position of
an elemant.

find() returns an iterator but not the postion of the elemant.
Is there any function equivalent to indexof()?

Regards,
Sunil

Nov 16 '06 #1
5 1886

Sunil Varma wrote:
Hi,

I've to write a function similar to this.

int process(const vector<int>& vct,int key)
{
// Here I've to find the position of key in the vector and do some
processing.
}

Is there any fucntion in algorithm which directly gives the position of
an elemant.

find() returns an iterator but not the postion of the elemant.
Is there any function equivalent to indexof()?
Use the result of find() - vct.begin().

Cheers! --M

Nov 16 '06 #2

mlimber wrote:
Sunil Varma wrote:
Hi,

I've to write a function similar to this.

int process(const vector<int>& vct,int key)
{
// Here I've to find the position of key in the vector and do some
processing.
}

Is there any fucntion in algorithm which directly gives the position of
an elemant.

find() returns an iterator but not the postion of the elemant.
Is there any function equivalent to indexof()?

Use the result of find() - vct.begin().

Cheers! --M
Hi limber,

It worked fine.

Thank you.

Nov 16 '06 #3

There is a more 'official' method:

the 'distance' function is guaranteed to work, whereas the subtraction
is not. Especially when you swap your vector with a list at some
point, the subtraction will give you a compiler error, whereas
'distance' will give you the right answer. The subtraction can only
work on random access iterators.
For vectors, however, the subtraction is fine and works.

Stefan

mlimber wrote:
Sunil Varma wrote:
Hi,

I've to write a function similar to this.

int process(const vector<int>& vct,int key)
{
// Here I've to find the position of key in the vector and do some
processing.
}

Is there any fucntion in algorithm which directly gives the position of
an elemant.

find() returns an iterator but not the postion of the elemant.
Is there any function equivalent to indexof()?

Use the result of find() - vct.begin().

Cheers! --M
Nov 16 '06 #4
"Sunil Varma" <su*******@gmai l.comwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
find() returns an iterator but not the postion of the elemant.
How's that again? An iterator *is* the position of the element.
Nov 16 '06 #5


On 16 Nov, 21:08, "Andrew Koenig" <a...@acm.orgwr ote:
"Sunil Varma" <sunil....@gmai l.comwrote in messagenews:11* *************** ******@k70g2000 cwa.googlegroup s.com...
find() returns an iterator but not the postion of the elemant.How's that again? An iterator *is* the position of the element.
Not quite, more of a pointer to the element, not it's position. It does
not tell you anything about where in a container (of any type) the
element is. It's not much of a differance but think of it like this:
take the first element of a list and take an iterator to it, the insert
a new element at the beginning of the list, the iterator still points
to the original element even though it has changed position in the
list.

--
Erik Wikström

Nov 17 '06 #6

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

Similar topics

7
12806
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member function find() and I have to use std::find(). The linker fails on unsatisified symbols in find(). I think this is because find() is a template...
7
3588
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I...
1
2201
by: Adam Teasdale Hartshorne | last post by:
I would be extremely grateful if somebody could tell me what as I getting wrong with this little bit of code to find the index of a particular element in a std::vector std::vector<int> allVertices ; getTriangleVertices(t, allVertices) ; //Fills the allVertices vector getTriangleVertices(t, allVertices) ; print("All Vertices",...
4
2783
by: Thomas Kowalski | last post by:
Hi, I have an sorted vector<doublev and want to insert a new double val. What I need is the position the val have been inserted at in the vector. My basic idea is something like this (pseudo-code) : it = lower_bound(v.begin(), v.end(), greater(val)); v.insert(it,val); size_t pos = it - v.begin(); Does anyone know whether lower_bound...
3
1739
by: toton | last post by:
Hi, I have a struct Point { int x, int y; } The points are stored in a std::vector<Pointpoints; (global vector) I want to add equality (operator == ) for the point, which will check equality based on the position of the point in the vector rather than its x,y or any other criterion. Thus 2 free point (which are not in the vector are always...
22
3598
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
2
1271
by: DruManiac | last post by:
hi everyone, ive found a simple drop down menu script but for some reason there is only one div tag set for 2 menus.. and i want to position them seperately... ill give the css positioning of what i want: position:absolute; top:40px; left:50%; margin-left:-100px; position:absolute; top:40px; left:50%; margin-left:-50px; i want to position...
5
3014
by: JD | last post by:
Hi, There are about 10+ 3-D points on the same line. I want to find the two end points from these points. The efficiency is not an issue because there are only 10 points or so. I just need a cleaner code to do it. Should I use an STL set to store the distances among them and then retrieve the max distance from the last element in the...
0
1104
by: thomas | last post by:
---code--- int func(int &elem) const{ return elem+2; } vector<intvec(30,0); foreach(vec.begin(), vec.end(), func); ----code----- As we see that the "func" function can only do operations on the "elem" itself, not the iterator.
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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. ...
0
8120
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
7672
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
6283
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...
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
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
937
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.