473,505 Members | 13,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

position in a vector

7 New Member
Hi,

I have an iterator that points to an item in a vector. How do I get the item's position in the vector (its distance from vec.begin() )?

Thanks,
Avi.
Sep 5 '06 #1
4 2078
D_C
293 Contributor
Does this help Avi?
Expand|Select|Wrap|Line Numbers
  1.   vector<unsigned int>::iterator it;
  2.   for(it = vec.begin(); it != vec.end(); it++)
  3.     cout << it-vec.begin() << " " << *it << " " << vec.end()-it << endl;
Sep 5 '06 #2
avimitrani
7 New Member
Looping over the vector may be costly. I'm looking for an 'arithmetic' calculation.

are the last two lines correct?:

vector<int> vec
// Here vec is filled...
vector<int>::iterator p;
// Here p is made to point to an element of vec...

vector <int>::difference_type position;
position = p - vec.begin()

Thanks,
Avi.
Sep 5 '06 #3
vmohanaraj
14 New Member
You can use the distance function provided for this purpose in STL. See the following example.

#include <iostream>
#include <vector>


int main() {
std::vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);

std::vector<int>::const_iterator i1, i2;

i1 = v.begin();

i2 = i1+2;
std::cout << distance(i1, i2);
}

The output for this would be 2.
Sep 5 '06 #4
avimitrani
7 New Member
Thank you, vmohanaraj.
This is what I was looking for.
Sep 6 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2367
by: Matthias | last post by:
Hello, I have a Canvas-Widget and will use as a "array of pixel". At Positon x,y I print a rectangle with a special color. I give the rectangle no objectname. Then I will ask the...
7
3581
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
2368
by: Paul | last post by:
Hi all, I am making a DirectInput application wich must report correctly the cursor position in windowed mode (it means it has to be the same as Microsoft Window´s cursor position). First of...
4
2776
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)...
5
1881
by: Sunil Varma | last post by:
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. }
3
1735
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...
1
1305
by: TiggiePurr | last post by:
Helpppp..... I have tons of code and can't seem to figure out why I'm getting a segmentation fault on the last line... int twoc; int i; twoc = count1s *2; // definitely init vector...
2
1266
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...
0
1096
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
7213
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
7098
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
7366
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
5610
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,...
1
5026
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
4698
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...
0
3187
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
1526
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
754
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.