 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
LinuxGuy Guest
|
Posted: Fri Sep 23, 2005 10:17 pm Post subject: STL::vector problem |
|
|
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Sep 24, 2005 8:20 pm Post subject: Re: STL::vector problem |
|
|
LinuxGuy wrote:
| Quote: | 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 .
|
position = iterator_you_got - your_vector.begin();
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sat Sep 24, 2005 8:23 pm Post subject: Re: STL::vector problem |
|
|
LinuxGuy wrote:
| Quote: | 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
|
if "it" is an iterator pointing to an element of vector v, then
std::distance(v.begin(), it) gives the position of the element.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Sun Sep 25, 2005 1:36 am Post subject: Re: STL::vector problem |
|
|
LinuxGuy wrote:
| Quote: | 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 .
|
Why are you unhappy about being returned an iterator? You can do
many things with an iterator.
To obtain the offset, simply subtract .begin() from the iterator
that you obtained.
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Sun Sep 25, 2005 1:43 am Post subject: Re: STL::vector problem |
|
|
"LinuxGuy" <rahul.ruikar (AT) gmail (DOT) com> skrev i meddelandet
news:1127453799.089567.131480 (AT) g47g2000cwa (DOT) googlegroups.com...
| Quote: | 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 .
|
For a vector, you can get the index as the distance from the start of
the vector
index = position - vec.begin();
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Timo Geusch Guest
|
Posted: Sun Sep 25, 2005 1:42 pm Post subject: Re: STL::vector problem |
|
|
LinuxGuy wrote:
| Quote: | 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 .
|
Use std::distance to find out how many elements away from the vector's
begin() the found element is.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sun Sep 25, 2005 1:44 pm Post subject: Re: STL::vector problem |
|
|
LinuxGuy wrote:
| Quote: | 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.
|
Yes, and an iterator is much better because it will stay an iterator even
if you switch to a std::list instead one day. Other than that, you could
use the difference to the begin() iterator:
iterator it = find(...)
unsigned index = it-vec.begin();
Just to mention it, you could use the generic std::distance() which tells
you how far two iterators are apart.
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sun Sep 25, 2005 1:44 pm Post subject: Re: STL::vector problem |
|
|
"LinuxGuy" <rahul.ruikar (AT) gmail (DOT) com> writes:
| Quote: | 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 .
|
An iterator does give you the position of the element in the
vector. If you need the index, subtract vector.begin() from the result
of find(). E.g.:
std::vector<int> v;
// fill v
std::vector<int>::iterator const pos7(std::find(v.begin(),v.end(),7));
if (pos7==v.end())
std::cout << "not foundn";
else
std::cout << "index: " << (pos7-v.begin()) << 'n';
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Sun Sep 25, 2005 1:49 pm Post subject: Re: STL::vector problem |
|
|
"LinuxGuy" wrote:
| Quote: | 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 .
|
If vec is the vector and iter is the iterator, then
std::distance(vec.begin(), iter)
is the index.
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ramashish Guest
|
Posted: Sun Sep 25, 2005 1:50 pm Post subject: Re: STL::vector problem |
|
|
| Quote: | 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 .
|
You can use std::find with std::distance to do that, e.g.
std::vector<T>::iterator it=std::find(v.begin(), v.end(), element);
int pos = std::distance(v.begin(), it);
if(pos != v.size()) {
// we found the element
...
}
Ram
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
lancediduck@nyc.rr.com Guest
|
Posted: Sun Sep 25, 2005 1:56 pm Post subject: Re: STL::vector problem |
|
|
Once you have an iterator from any container, you can find the ordinal
position using std::distance
i.e. typedef std::vector<foo> foovec;
foovec vec_of_foo;//put some foo's in
foovec::iterator pos=find(vec_of_foo.begin(),vec_of_foo.end(),somefoo);
std::cout<<"somefoo is at position
"<
For vector or anything with a RandomAccessIterator distance is an O(1)
operation. For list and other containers it is O(N);
see
http://www.roguewave.com/support/docs/sourcepro/stdlibref/distance.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sun Sep 25, 2005 6:56 pm Post subject: Re: STL::vector problem |
|
|
Thomas Tutone wrote:
| Quote: | "LinuxGuy" wrote:
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 .
If vec is the vector and iter is the iterator, then
std::distance(vec.begin(), iter)
is the index.
|
Calling std::distance wll return the index, but is there a better way
to calculate it? And if so, why is it better?
I would argue that subtraction:
iter - vec.begin()
is the better method. For one, that is how std::distance calculates the
result for vector iterators itself anyway. And since the iterator's
container type is a runtime constant, using std::distance is a
pointless abstraction. But not necessarily harmless. std::distance is
really too generic for non templated code. It can find the distance
between iterators no matter the type of their container; subtraction on
the other hand is defined only when it is efficient.
Using std::distance, changing the vector to a std::list silently turns
an efficient operation into an inefficient one. Using subtraction
instead, such a change would trigger a compiler error. The error
(subtraction not defined for a list's iterators) is preferable since it
alerts the programmer who changed the container's type to this new
cost. The programmer can then decide whether obtaining the index of a
list's iterator is still a worthwhile operation.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Mon Sep 26, 2005 8:22 am Post subject: Re: STL::vector problem |
|
|
Greg Herlihy wrote:
[snip]
| Quote: | Calling std::distance wll return the index, but is there a better way
to calculate it? And if so, why is it better?
I would argue that subtraction:
iter - vec.begin()
is the better method. For one, that is how std::distance calculates the
result for vector iterators itself anyway. And since the iterator's
container type is a runtime constant, using std::distance is a
pointless abstraction.
|
Not entirely pointless - it does document what the programmer is doing
more clearly, IMO, then the subtraction. And for most compilers, the
code generated with optimization turned on should be identical.
| Quote: | But not necessarily harmless. std::distance is
really too generic for non templated code. It can find the distance
between iterators no matter the type of their container; subtraction on
the other hand is defined only when it is efficient.
Using std::distance, changing the vector to a std::list silently turns
an efficient operation into an inefficient one. Using subtraction
instead, such a change would trigger a compiler error. The error
(subtraction not defined for a list's iterators) is preferable since it
alerts the programmer who changed the container's type to this new
cost. The programmer can then decide whether obtaining the index of a
list's iterator is still a worthwhile operation.
|
I agree with you in part. But two points in response. First, the OP
wanted an index - presumably because he was going to use that index
elsewhere in connection with operator[]. If so, then if he changed the
container to a list, but continued to use the index, then the compiler
would complain when he attempted to refer to a list element by index
anyway. A more interesting issue would be raised, I suppose, if he
changed the vector to a map, where operator[] is defined.
But second and more importantly - if it is important that the index (or
distance) be computed in constant rather than linear time, I believe
that a compile-time assertion that the iterator must be random access,
along with an explanatory comment, more clearly documents the
programmer's intent, compared to a subtraction that simply mysteriously
fails when the container type is changed (months or years later).
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|