![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
Vector Sorting
I'm trying to sort a multidimensional (2D) vector of vectors. I want the sorting to proceed like:
vector< vector<char> > v; ... // I've filled the list by now: // "v[0]" contains the vector of elements 'b','b' // "v[1]" contains the vector of elements 'b','a' // "v[2]" contains the vector of elements 'a','a' // "v[0]" contains the vector of elements 'a','a' // "v[1]" contains the vector of elements 'b','a' // "v[2]" contains the vector of elements 'b','b'
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;} |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
How does it not work? What is your output? The following works just fine for me, and the standard confirms that it should work:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
// Uncomment if you get errors
//#define NO_KOENIG
#ifdef NO_KOENIG
namespace std {
#endif
template <typename T>
ostream& operator<< ( ostream& out, const vector<T>& v )
{
copy ( v.begin(), v.end(), ostream_iterator<T> ( out, " " ) );
return out;
}
#ifdef NO_KOENIG
}
#endif
int main()
{
char a[] = {'b','b'};
char b[] = {'b','a'};
char c[] = {'a','a'};
vector<vector<char> > v;
v.push_back ( vector<char> ( a, a + 2 ) );
v.push_back ( vector<char> ( b, b + 2 ) );
v.push_back ( vector<char> ( c, c + 2 ) );
sort ( v.begin(), v.end() );
copy ( v.begin(), v.end(), ostream_iterator<vector<char> > ( cout, "\n" ) );
} |
|
|
|
|
|
#3 |
|
Professional Programmer
|
Thanks eggbert. That code wasn't quite what I had, and it works fine for me too. However, it would be great if you could clear something up for me. What exactly does
v.push_back ( vector<char> ( a, a + 2 ) ); ( a, a + 2 )
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;} |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
One constructor for the vector class takes a beginning and ending iterator that specify a sequence. You can think of a as a.begin() and a + 2 as a.end(). That's basically what it is except using pointers instead of the iterator abstraction.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|