473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

After sorting a vector, how to get max in this vector?

Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec[i] << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl;
cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl;
cout << endl;
return 0;
}
-------------------------------------------
The max should be 10.
But this program's result is -842150451.
What's wrong?
Thanks for your help.

Regards,
cylin.
Jul 22 '05 #1
3 4601
cylin wrote:
Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec[i] << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

vec.end() points to one beyond the end of the array.

*( vec.end() -1 )

cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vec[vec.size()] is undefined.

vec[vec.size()-1] is probably what you want.

cout << endl;
return 0;
}
-------------------------------------------
The max should be 10.
But this program's result is -842150451.
What's wrong?
Thanks for your help.

Regards,
cylin.


Jul 22 '05 #2
Thanks............
I forgot the index is out of range.

Regards,
cylin.
"Gianni Mariani" <gi*******@mariani.ws>
???????:br********@dispatch.concentric.net...
cylin wrote:
Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec[i] << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

vec.end() points to one beyond the end of the array.

*( vec.end() -1 )

cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vec[vec.size()] is undefined.

vec[vec.size()-1] is probably what you want.

cout << endl;
return 0;
}
-------------------------------------------
The max should be 10.
But this program's result is -842150451.
What's wrong?
Thanks for your help.

Regards,
cylin.


Jul 22 '05 #3
"cylin" <cy***@avant.com.tw> wrote in message
news:br************@ID-154203.news.uni-berlin.de...
Thanks............
I forgot the index is out of range.

Regards,
cylin.
"Gianni Mariani" <gi*******@mariani.ws>
???????:br********@dispatch.concentric.net...
cylin wrote:
Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec[i] << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

vec.end() points to one beyond the end of the array.

*( vec.end() -1 )

cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vec[vec.size()] is undefined.

vec[vec.size()-1] is probably what you want.

cout << endl;
return 0;
}
-------------------------------------------
The max should be 10.
But this program's result is -842150451.
What's wrong?
Thanks for your help.

Regards,
cylin.

Even easier, I think:

vec.front() for the first element (a reference, not an iterator) and
vec.back() for the last one (again a reference). Hence:

cout << "Min=" << vec.front() << endl;
cout << "Max=" << vec.back() << endl;

Easier to read and less prone to errors IMHO.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4

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

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
4
by: Michael | last post by:
Suppose I have: #include <vector> using std::vector; class C { public: int a,b;
0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
3
by: Jeff Dege | last post by:
Suppose I have a problem for which an associative array is the most natural fit, but which I need to be able to output in sorted order of the values. STL map would seem like a good fit, except...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.