Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 28th, 2005, 11:22 PM   #1
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
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'
And I want to sort it to make SURE that the vectors elements are now:
// "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'
I've tried sort(v.begin(), v.end());, but that does not work. Please help!
__________________
% 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;}
iignotus is offline   Reply With Quote
Old Apr 29th, 2005, 9:59 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
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" ) );
}
Eggbert is offline   Reply With Quote
Old Apr 29th, 2005, 3:17 PM   #3
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
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 ) );
mean? I know you are creating a vector from the character array and pushing it onto the vector v, but I don't understand the segment
( a, a + 2 )
Thanks again.
__________________
% 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;}
iignotus is offline   Reply With Quote
Old Apr 29th, 2005, 4:02 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
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.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:04 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC