473,769 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(1 0);
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 4620
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(1 0);
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*******@mari ani.ws>
???????:br***** ***@dispatch.co ncentric.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(1 0);
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.co m.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*******@mari ani.ws>
???????:br***** ***@dispatch.co ncentric.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(1 0);
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
3417
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 objects in the vector vec_A. Suppose there are K attributes to be added. For each of the attributes I define K vectors of appropriate types. Say, the attributes have types type1, type2, ..., typeK. So I define std::vector<type1> attr1(vec_A.size());
2
2306
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
1236
by: Michael | last post by:
Suppose I have: #include <vector> using std::vector; class C { public: int a,b;
0
2670
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to Function Pointers Source : Technical Report on C++ Performance Tool : The C/C++ Program Perfometer (Version 2.8.1-1.19-Beta) * http://sourceforge.net/projects/cpp-perfometer/
18
3058
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 frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary containers and sort those (only pointers have to be copied around then). However, that also means if I...
0
2580
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 sort the records in a plain-text index file based on certain columns. The index file consists of records and fields within the records. The individual fields are separated by semicolons, the records by newlines. The index file is loaded into memory...
3
1835
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 that maps are sorted on key, not value. I can picture a couple of approaches, but I'm not clear on what would be cleanest.
11
2018
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 the same exact sorting counts also..shouldn't they differ somewhat? Source and data file below. Thanks
18
4914
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 an appreciation for it. So I decided to toy with some idea, just for fun and for evaluating how rusty I'd become. "Let me write a simple functor for sorting the elements of a collection! I will start with a simple collection of integers.", I...
0
10211
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...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
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, and deployment—without 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...
1
7408
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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 we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.