473,395 Members | 2,443 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,395 software developers and data experts.

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 1874

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*******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.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.orgwrote:
"Sunil Varma" <sunil....@gmail.comwrote in messagenews:11**********************@k70g2000cwa.g ooglegroups.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
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...
7
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...
1
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>...
4
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)...
3
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...
22
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...
2
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...
5
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...
0
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.