Re: Sorting records using sort()
In article <pan.2004.01.02 .12.56.21.62577 3@remove.this.p art.rtij.nl>,
Martijn Lievaart <m@remove.this. part.rtij.nl> writes[color=blue]
>On Fri, 02 Jan 2004 03:13:26 -0800, Elijah Bailey wrote:
>
>[ Please don't top post, thank you. M4 ]
>
>[ Crossposted to csc++, is this standard complient? ]
>
>[ Short description of the problem ]
>
>We have an array of n*m bytes. It holds n objects of size m. Both are only
>known at runtime. For some strange reason we want to sort this using
>std::sort. We know there are other solutions, the question is, can it be
>done using std::sort.
>
>Constraints: Not clear, but memory seems to be an issue, creating an array
>of pointers and sort that is out.[/color]
But if n and m are only known at execution time the array must be
created dynamically which immediately makes me think of using a vector.
The following trivial program demonstrates that.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n, m;
cout << "N and m";
cin >> n >> m;
vector< vector<char> > a(n, m);
for(int i(0); i != n; ++i){
for(int j(0); j != m; ++j){
a[i][j] = 'a' - i - j;
}
}
for(int i(0); i != n; ++i){
for(int j(0); j != m; ++j){
cout << a[i][j] << " ";
}
cout << '\n';
}
sort(a.begin(), a.end());
cout << "\n\n";
for(int i(0); i != n; ++i){
for(int j(0); j != m; ++j){
cout << a[i][j] << " ";
}
cout << '\n';
}
}
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk
Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
In article <pan.2004.01.02 .12.56.21.62577 3@remove.this.p art.rtij.nl>,
Martijn Lievaart <m@remove.this. part.rtij.nl> writes[color=blue]
>On Fri, 02 Jan 2004 03:13:26 -0800, Elijah Bailey wrote:
>
>[ Please don't top post, thank you. M4 ]
>
>[ Crossposted to csc++, is this standard complient? ]
>
>[ Short description of the problem ]
>
>We have an array of n*m bytes. It holds n objects of size m. Both are only
>known at runtime. For some strange reason we want to sort this using
>std::sort. We know there are other solutions, the question is, can it be
>done using std::sort.
>
>Constraints: Not clear, but memory seems to be an issue, creating an array
>of pointers and sort that is out.[/color]
But if n and m are only known at execution time the array must be
created dynamically which immediately makes me think of using a vector.
The following trivial program demonstrates that.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n, m;
cout << "N and m";
cin >> n >> m;
vector< vector<char> > a(n, m);
for(int i(0); i != n; ++i){
for(int j(0); j != m; ++j){
a[i][j] = 'a' - i - j;
}
}
for(int i(0); i != n; ++i){
for(int j(0); j != m; ++j){
cout << a[i][j] << " ";
}
cout << '\n';
}
sort(a.begin(), a.end());
cout << "\n\n";
for(int i(0); i != n; ++i){
for(int j(0); j != m; ++j){
cout << a[i][j] << " ";
}
cout << '\n';
}
}
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk
Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Comment