473,729 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About the find() is STL algorithm

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 it returns the iterator to
the last element in the range, not to the last element in the
container, to the last element in the range. That being said, how can
we tell if find() has been successful in finding the element we need?
Its easy when we are searching the whole container, we can always the
container's end() method, but when we are searching only a subset of
all the elements in a container, what then? Do we have a better way
than checking the element that find returns again?(sounds inefficient,
repetitive).

Would appreciate your thoughts on this matter.

Thanks, Draw

Jun 10 '06 #1
5 2576
* Draw:

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 it returns the iterator to
the last element in the range, not to the last element in the
container, to the last element in the range.


A range of elements is specified as an iterator A to the first element,
and an iterator B one past the last element.

If std::find fails it returns B, in all cases.

Compare the result iterator to B.

--
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?
Jun 10 '06 #2
Alf P. Steinbach wrote:
* Draw:

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 it returns the iterator to
the last element in the range, not to the last element in the
container, to the last element in the range.


A range of elements is specified as an iterator A to the first element,
and an iterator B one past the last element.

If std::find fails it returns B, in all cases.

Compare the result iterator to B.

--

Thanks, but I don't think I got an answer for my question. Are you
saying that I have to always give the start and end as the range? What
if I don't want to, what if I want to search only the first 10 elements
of the vector?
I went ahead and tried to do that, the code is right after I stop
blabbering :-), and find() does indeed return the last element of the
range if it doesn't find anything(not that of the container), but what
if the last element of the range does satisfy the condition find() is
looking for, in that case, it would still return the last element in
the range. Thats ambiguous, isn't it?
And what you say above proves it, it does return the last element in
the range you provide, but you are able to work around the situation I
mention because your last element happens to equal the iterator
*.end()(indeed, one past the last element).

//[START]

#include<iostre am>
#include<algori thm>
#include<vector >
int main(void){
unsigned int index;
vector<int> intvec(20,3);
vector<int>::it erator it,ret;
for(it = intvec.begin(), index=0;it != intvec.end();++ it,++index){
*it =+ index;
cout << "vector Element value equated to subscript "<<*it<<end l;
}
intvec[15] = 45;
cout << "Finding the element with value equal to 45 \n";
it = intvec.begin();
ret = find(it,it+10,4 5);
if(ret == intvec.end()){
cout << "No such value in the vector.\n";
}
else{
cout << "Element value " << *ret << endl;
}
return(0);
}

//[END]

Jun 11 '06 #3
Draw wrote:
Alf P. Steinbach wrote:
* Draw:

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 it returns the iterator to
the last element in the range, not to the last element in the
container, to the last element in the range.


A range of elements is specified as an iterator A to the first element,
and an iterator B one past the last element.

If std::find fails it returns B, in all cases.

Compare the result iterator to B.

--

Thanks, but I don't think I got an answer for my question. Are you
saying that I have to always give the start and end as the range? What
if I don't want to, what if I want to search only the first 10 elements
of the vector?
I went ahead and tried to do that, the code is right after I stop
blabbering :-), and find() does indeed return the last element of the
range if it doesn't find anything(not that of the container), but what
if the last element of the range does satisfy the condition find() is
looking for, in that case, it would still return the last element in
the range. Thats ambiguous, isn't it?


The "one past the last element" concept is what you seem to be missing.

The range is specified by giving an iterator A to the first element and
an iterator B to _one past_ the last element in the range i.e. iterator
B itself does not point to an element within the range. Iterator B will
therefore never be returned if the element is found.

Jun 11 '06 #4
Markus Schoder wrote:
Draw wrote:
Alf P. Steinbach wrote:
* Draw:
>
> 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 it returns the iterator to
> the last element in the range, not to the last element in the
> container, to the last element in the range.

A range of elements is specified as an iterator A to the first element,
and an iterator B one past the last element.

If std::find fails it returns B, in all cases.

Compare the result iterator to B.

--

Thanks, but I don't think I got an answer for my question. Are you
saying that I have to always give the start and end as the range? What
if I don't want to, what if I want to search only the first 10 elements
of the vector?
I went ahead and tried to do that, the code is right after I stop
blabbering :-), and find() does indeed return the last element of the
range if it doesn't find anything(not that of the container), but what
if the last element of the range does satisfy the condition find() is
looking for, in that case, it would still return the last element in
the range. Thats ambiguous, isn't it?


The "one past the last element" concept is what you seem to be missing.

The range is specified by giving an iterator A to the first element and
an iterator B to _one past_ the last element in the range i.e. iterator
B itself does not point to an element within the range. Iterator B will
therefore never be returned if the element is found.


Thanks Guys! Now I see. Thanks for clarifying that.

Jun 11 '06 #5
Draw wrote:
Alf P. Steinbach wrote:
* Draw:
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 it returns the iterator to
the last element in the range, not to the last element in the
container, to the last element in the range.
A range of elements is specified as an iterator A to the first element,
and an iterator B one past the last element.

If std::find fails it returns B, in all cases.

Compare the result iterator to B.

--


Thanks, but I don't think I got an answer for my question. Are you
saying that I have to always give the start and end as the range? What
if I don't want to, what if I want to search only the first 10 elements
of the vector?
I went ahead and tried to do that, the code is right after I stop
blabbering :-), and find() does indeed return the last element of the
range if it doesn't find anything(not that of the container), but what
if the last element of the range does satisfy the condition find() is
looking for, in that case, it would still return the last element in
the range. Thats ambiguous, isn't it?
And what you say above proves it, it does return the last element in
the range you provide, but you are able to work around the situation I
mention because your last element happens to equal the iterator
*.end()(indeed, one past the last element).

See the changed code.

//[START]

#include<iostre am>
#include<algori thm>
#include<vector >
int main(void){
unsigned int index;
vector<int> intvec(20,3);
vector<int>::it erator it,ret;
for(it = intvec.begin(), index=0;it != intvec.end();++ it,++index){
*it =+ index;
cout << "vector Element value equated to subscript "<<*it<<end l;
}
intvec[15] = 45;
cout << "Finding the element with value equal to 45 \n";
it = intvec.begin();
ret = find(it,it+10,4 5);

// if(ret == intvec.end()){
if (ret == (it+10)) {
// If find does not find the value in [it, it+10), it
// will return it+10, which is one element past the last
// element in [it, it+10) cout << "No such value in the vector.\n";
}
else{
cout << "Element value " << *ret << endl;
}
return(0);
}

//[END]

Jun 11 '06 #6

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

Similar topics

17
2129
by: Rich S. | last post by:
Hello Just like everyone else, I am excited about how easily one can create Windows applications with the .NET platform, but for shareware, I have some serious reservations 1. Your code can be easily decompiled. This would make it very difficult to implement any sort of license restrictions on your app, because anything you write can be easily viewed and cracked. This also removes any privacy for your intellectual property 2. Jim...
7
7072
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so if that's your only reason not to help, please do. Otherwise, I guess it's OK. But, just remember, I'm not asking you to do my work for me, just to point out my error. My problem is not with the algorithm itself (standard divide and conquer on...
4
6484
by: Code4u | last post by:
I need to write an algorithm that sheds the outliers in a large data set, for example, I might want to ignore the smallest 2% of values and find the next smallest. Boost has a nth_element algorithm, however, it partially sorts the data. I have a requirement that the data remain in the orginal order. Of course I could make a copy of the vector or array and then apply nth_element, but this would be expensive in memory and would require...
50
2853
by: Joseph Casey | last post by:
Greetings. I have read that the mistake of calling free(some_ptr) twice on malloc(some_data) can cause program malfunction. Why is this? With thanks. Joseph Casey.
5
1090
by: Crirus | last post by:
What do you think about this approaches, wich one is the best? For x As Integer = u.GetViewBounds.X To u.GetViewBounds.X + u.GetViewBounds.Width For y As Integer = u.GetViewBounds.Y To u.GetViewBounds.Y + u.GetViewBounds.Height Dim Dx As Integer = x - u.Location.X Dim Dy As Integer = y - u.Location.Y If Dx * Dx + Dy * Dy <= u.ViewRange * u.ViewRange Then 'punctul e in cercul vizual
2
1779
by: xeyder | last post by:
Hi everyone.. I need help about graph theory ( network problems) .. I have below problem . Does anyone know any existence algorithms or solutions to this problem ?? The Problems is: " We can use graphs to represent a communication network. In such graphs, the vertices represent communication stations and the edges represent communication
1
7432
by: Ghada Al-Mashaqbeh via DotNetMonster.com | last post by:
Hi all, I want to implement Lee Maze routing algorithm in C#, I have many problems especially in gaining usefull resources about the algorithm work, please if can any body help me to find usefull resources. Also, if anybody can send me the a link to the full artice of the following paper I will be very glad, the paper is: “An Algorithm for Path Connection And Its Application”, CY Lee, IRE
1
2602
by: hn.ft.pris | last post by:
"Forward maximum match" mean that there is a dictionary contains thousands of items which are single words. For example, "kid", "nice", "best"... And here get a string "kidnicebs", the forward maximum match algorithm need to find the longest string that matches the string in dictionary. Because the longest string in the dictionary has a length of 4, so the algoritm pick the first four characters of the string(that's why it's called...
30
2740
by: gaoxtwarrior | last post by:
a sort which is stable means it keeps the object's original order. A sort which is in place is stable. does anyone can explain me what is sort in place? thx.
0
8913
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.