Hi,
I have vector with some elements. now I want to search particular
element and find out the position of that element in that vector (
index ). I used find algorithm but it gives Iterator in return.
please help me to find out position of any element in that vector .
any help is welcome
Thanks
Rahul 6 9015
"LinuxGuy" <ra**********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com... Hi,
I have vector with some elements. now I want to search particular element and find out the position of that element in that vector ( index ). I used find algorithm but it gives Iterator in return. please help me to find out position of any element in that vector .
any help is welcome Thanks
Rahul
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vec;
vec.push_back(42);
vec.push_back(99);
vec.push_back(25);
vec.push_back(32);
int value(25);
std::vector<int>::const_iterator it
(std::find(vec.begin(), vec.end(), value));
std::cout << "Value " << value;
if(it != vec.end())
std::cout << " found at vector element "
<< it - vec.begin();
else
std::cout << " not found";
std::cout.put('\n');
return 0;
}
-Mike
This is just a wild guess - and probably not the right way but can't
you do something like
int index = (objaddr - firstobj_addr) / sizeof(object)
For an iterator 'i' into vector 'v', use
int index = (int)std::distance(v.begin(), i);
to find the index.
(This will break down if your vector is so large that an int can't
store the index - in which case look at the actual return type of
std::distance)
>> int index = (int)std::distance(v.begin(), i); to find the index.
instead, use :
std::vector<int>::difference_type index;
index = std::distance (v.begin(), i );
std::cout << "\n Position in the vector: " << index << "th Element from
the begining of the vector " << std::endl ;
~hth
M
LinuxGuy wrote: Hi,
I have vector with some elements. now I want to search particular element and find out the position of that element in that vector ( index ). I used find algorithm but it gives Iterator in return. please help me to find out position of any element in that vector .
any help is welcome Thanks
Rahul
Given a vector v, and an iterator, iter, in v, the expression:
iter - v.begin()
calculates the "position" (or index) of the element in v that iter
references.
Greg
tmartsum schreef: This is just a wild guess - and probably not the right way but can't you do something like
int index = (objaddr - firstobj_addr) / sizeof(object)
Addresses differences are expressed in numbers of objects in between,
not bytes. Therefore, you don't have to divide by sizeof, and secondly,
there must be objects in between (or allocated memory where they could
be constructed).
The basic idea is right, though, but you don't need the address when
you have an iterator. (objIter-firstObjIter) works just as well, and
it also works for std::deque (=>more general solution)
HTH,
Michiel Salters This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vasileios Zografos |
last post by:
Hi there I am using the STL vector and I was wondering, is there an
existing method for reversing the contents of one vector?
e.g. vector<int> v1 which has entries 1,2,3,4,5
and I want to end...
|
by: luigi |
last post by:
Hi,
I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version...
|
by: silverburgh.meryl |
last post by:
in STL, why vector has an API to return the n-th element, but list does
not have such API?
http://www.sgi.com/tech/stl/Vector.html
http://www.sgi.com/tech/stl/List.html
Thank you.
|
by: Christian Chrismann |
last post by:
Hi,
I've a runtime problem with STL vectors.
Here is the simplified version of the code:
template <class Tclass A {
...
private:
vector<T*myvector;
typename vector<T*>::itarator mIt;
|
by: krunalbauskar |
last post by:
Hi,
Explicit instantiation of STL vector demands explicit instantiation of
all the templates it using internally.
For example -
<snippet>
#include <iostream>
#include <vector>
|
by: creativeinspiration |
last post by:
Hi Everybody,
I have a question. I have an STL vector of structs such as:
struct sname
{
bool member1;
bool member2;
.
.
.
|
by: chsalvia |
last post by:
I have a question about the design of STL vector. One thing I wonder
was why the STL designers chose to have the insert() and erase()
functions take an iterator as the first argument, rather than...
|
by: zl2k |
last post by:
hi, all
What is the default size of a stl vector? Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first...
|
by: =?Utf-8?B?QWxleA==?= |
last post by:
Hi all,
I am devloping an application in VC++ with Visual Studio 2005. In this
applicatin, Threadpool and stl classes are there. All threads will
use(sending by pointer to vector) stl vector...
|
by: Builder |
last post by:
How can I import the following COM interface to C#?
DECLARE_INTERFACE_(IVertices, IUnknown)
{
STDMETHOD(get_vertices) (THIS_ vector<POINT>& vertices) PURE;
STDMETHOD(set_vertices) (THIS_...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |