473,491 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Find out the index of a vector which has the min element

Hi,
How can I find out the index of a vector which has the min element?
I know how to find out the * value * of a vector which has the min
element.
But how can I find out its index?

for example, [100, 98, 1, 98, 28], I would like the function to return
2.

Thank you.

Feb 24 '06 #1
7 7150
In article <11*********************@u72g2000cwu.googlegroups. com>,
Pl********@gmail.com wrote:
Hi,
How can I find out the index of a vector which has the min element?
I know how to find out the * value * of a vector which has the min
element.
But how can I find out its index?

for example, [100, 98, 1, 98, 28], I would like the function to return
2.

Thank you.


return min_element( vec.begin(), vec.end() ) - vec.begin();

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 25 '06 #2
* Pl********@gmail.com:
How can I find out the index of a vector which has the min element?
Look in the header <algorithm> for something akin to "min element".

Hm, are you sure this isn't HOMEWORK?

I'm pretty sure it is, because of the suggestive wording. I just hope
nobody writes your code for you.

I know how to find out the * value * of a vector which has the min
element.
I don't, so you're way ahead of me there. How do you define the value
of a vector, as something other than the vector, that is?

But how can I find out its index?

for example, [100, 98, 1, 98, 28], I would like the function to return
2.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 25 '06 #3
Pl********@gmail.com wrote:
How can I find out the index of a vector which has the min element?
I know how to find out the * value * of a vector which has the min
element.
But how can I find out its index?

for example, [100, 98, 1, 98, 28], I would like the function to return
2.


If you can find the value, couldn't you _remember_ the index where you
found it? How _do_ you find the "value"?

V
--
Please remove capital As from my address when replying by mail
Feb 25 '06 #4
* Alf P. Steinbach:
* Pl********@gmail.com:
How can I find out the index of a vector which has the min element?


Look in the header <algorithm> for something akin to "min element".

Hm, are you sure this isn't HOMEWORK?

I'm pretty sure it is, because of the suggestive wording. I just hope
nobody writes your code for you.


Of course somebody did...

This fact, that people do post homework here, and the fact that other
people actually do their homework, here, must be saying something.

I'm just not sure what.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 25 '06 #5
In article <46************@individual.net>,
"Alf P. Steinbach" <al***@start.no> wrote:
* Alf P. Steinbach:
* Pl********@gmail.com:
How can I find out the index of a vector which has the min element?


Look in the header <algorithm> for something akin to "min element".

Hm, are you sure this isn't HOMEWORK?

I'm pretty sure it is, because of the suggestive wording. I just hope
nobody writes your code for you.


Of course somebody did...

This fact, that people do post homework here, and the fact that other
people actually do their homework, here, must be saying something.

I'm just not sure what.


I'm in a bad mood, what can I say. I've done three homework problems
already today. :-/

I can't always tell though.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 25 '06 #6
Alf P. Steinbach wrote:
I'm pretty sure it is, because of the suggestive wording. I just hope
nobody writes your code for you.


Of course somebody did...


I enjoy writing solutions to homework assignments and I post them
if I think that the solution is kind "creative". For most of these
"solutions" it should be pretty clear that they weren't written by
the student...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 25 '06 #7
* Dietmar Kuehl:
Alf P. Steinbach wrote:
I'm pretty sure it is, because of the suggestive wording. I just hope
nobody writes your code for you.

Of course somebody did...


I enjoy writing solutions to homework assignments and I post them
if I think that the solution is kind "creative". For most of these
"solutions" it should be pretty clear that they weren't written by
the student...


Well, yes, that's something else entirely. The last one I saw from you
could absolutely never be mistaken for something a student produced...
;-) I enjoy them immensely (and perhaps some homework poster will
actually hand in such a solution!), keep 'em coming!

TIA.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 25 '06 #8

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

Similar topics

13
34336
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
29
3934
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
1
2192
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>...
13
4997
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm...
5
2550
by: Draw | last post by:
Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range...
9
28455
by: Henning Hasemann | last post by:
I'm using a stl-priority queue and want - find out if a certain item is contained in the queue - to be able iterate over all items without having to pop() them, order does not matter. I couldnt...
4
11465
by: boheman | last post by:
Hi, I am wondering if there is a simple and quick way to return the indices of sorted vector. for example, I have a vector<intx containing {5, 2, 3, 0, 2}. I can use sort(x.begin(), x.end(),...
11
1930
by: desktop | last post by:
How do I find the last element in the list numsx defined below? int* myfind(int* arr_start, int* arr_end, int& s) { int not_found = 666; int* result = &not_found; while (arr_start !=...
1
2330
by: OriginalCopy | last post by:
This is a demonstrative code which could be used for debugging purposes. And yet I don't know how to insert the necessary data on line 63, purely syntactically speaking ? I'm a beginner with STL, and...
0
7112
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
6974
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
7146
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
7356
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...
0
5448
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
4878
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
3084
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
1389
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 ...
0
277
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...

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.