Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sorting a vector of classes

Reply
Thread Tools

Sorting a vector of classes

 
 
caxmester@gmail.com
Guest
Posts: n/a
 
      12-02-2005
Ok this shouldn't be nearly this hard, but i've checked EVERYWHERE from
msdn to scouring google & I can't figure it out.

I basically have a vector of pointers to "aaWord" objects, each of
which contain titles (as strings) and I want to order the vector
alphabetically by those strings. I'm using __gc and pointers and all
that. Visual C++ .NET

vector<gcroot<aaWord __gc*> > *VectorofWords;

Since obviously ordinary sort wouldn't work, I tried making my own
predicate function looking like

static bool op_LessThan( aaWord* a, aaWord* b ){...}

but it throws a Fatal Error C1001 INTERNAL COMPILER ERROR and there's
no way i'm getting through that.

Am I just making a stupid syntax error?

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-02-2005

wrote:
>
> but it throws a Fatal Error C1001 INTERNAL COMPILER ERROR and there's
> no way i'm getting through that.
>
> Am I just making a stupid syntax error?


[Compiler Specific]
As it indicates, this is VC++ specific. Try your code on another
compiler.

 
Reply With Quote
 
 
 
 
n2xssvv g02gfr12930
Guest
Posts: n/a
 
      12-02-2005
wrote:
> Ok this shouldn't be nearly this hard, but i've checked EVERYWHERE from
> msdn to scouring google & I can't figure it out.
>
> I basically have a vector of pointers to "aaWord" objects, each of
> which contain titles (as strings) and I want to order the vector
> alphabetically by those strings. I'm using __gc and pointers and all
> that. Visual C++ .NET
>
> vector<gcroot<aaWord __gc*> > *VectorofWords;
>
> Since obviously ordinary sort wouldn't work, I tried making my own
> predicate function looking like
>
> static bool op_LessThan( aaWord* a, aaWord* b ){...}
>
> but it throws a Fatal Error C1001 INTERNAL COMPILER ERROR and there's
> no way i'm getting through that.
>
> Am I just making a stupid syntax error?
>

Since you've not provided even a simplified section of code. Perhaps
the sample below will help.

std::string str[6] = { "for", "all", "the", "tea", "in", "china" };

bool scmp(std::string *p1,std::string *p2)
{
return p1->compare(*p2) > 0;
}

void VTest(void)
{
std::vector<std::string *> vs;
std::string *ps = str + sizeof(str)/sizeof(*str);
do
{
--ps;
vs.push_back(ps);
}
while (ps != str);
std::vector<std::string *>::iterator it = vs.end();
do
{
--it;
std::cout << (*it)->c_str() << " ";
}
while (it != vs.begin());
std::sort(vs.begin(),vs.end(),&scmp);
std::cout << std::endl;
it = vs.end();
do
{
--it;
std::cout << (*it)->c_str() << " ";
}
while (it != vs.begin());
std::cout << std::endl;
}

JB
 
Reply With Quote
 
Dervish
Guest
Posts: n/a
 
      12-02-2005
Most likely it's syntax error.
E.g. following works:
struct A
{
string s_;

A(const string s):s_(s){}

static bool op_LessThan( A* l, A* r ){return l->s_ < r->s_;}
};

ostream& operator << (ostream& o, A* a)
{
o << a->s_;
return o;
}

int main(int,char**)
{
std::vector<A*> g_A;

g_A.push_back(new A("=========="));
g_A.push_back(new A("foo"));
g_A.push_back(new A("bar"));
g_A.push_back(new A("baz"));

copy(g_A.begin(),g_A.end(),ostream_iterator<A*>(co ut,"\n"));
sort(g_A.begin(),g_A.end(),A:p_LessThan);
copy(g_A.begin(),g_A.end(),ostream_iterator<A*>(co ut,"\n"));
return true;
}

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      12-02-2005

wrote:
> Ok this shouldn't be nearly this hard, but i've checked EVERYWHERE from
> msdn to scouring google & I can't figure it out.
>
> I basically have a vector of pointers to "aaWord" objects, each of
> which contain titles (as strings) and I want to order the vector
> alphabetically by those strings. I'm using __gc and pointers and all
> that. Visual C++ .NET
>
> vector<gcroot<aaWord __gc*> > *VectorofWords;
>
> Since obviously ordinary sort wouldn't work, I tried making my own
> predicate function looking like
>
> static bool op_LessThan( aaWord* a, aaWord* b ){...}
>
> but it throws a Fatal Error C1001 INTERNAL COMPILER ERROR and there's
> no way i'm getting through that.
>


Consider using a clone smart pointer like that in the following:
http://code.axter.com/copy_ptr.h
or a COW smart pointer
http://code.axter.com/cow_ptr.h

Example usage:
vector<copy_ptr<aaWord> > *VectorofWords;

Both the above smart pointers apply the comparison operators on the
object instead of on the address of the pointer.

 
Reply With Quote
 
caxmester@gmail.com
Guest
Posts: n/a
 
      12-02-2005
Neelesh Bodas:
> As it indicates, this is VC++ specific. Try your code on another
> compiler.


I dont' know if that's a viable option, as I'm not familiar with any
other compilers and time's a factor.



n2xssvv g02gfr12930:
I'm already doing essentially what you're suggesting. My vector,
though, is of pointers to classes which contain strings(rather pointers
to strings). And the vector itself (vector<std::string *> vs in your
case) is a pointer.

>std::sort(vs.begin(),vs.end(),&scmp);

Which in my case would be
std::sort(Collection->getwordvector()->begin(),
Collection->getwordvector()->end(), op_LessThan);

Again this seems like a really simple problem & if the compiler error
readouts were more relevant i'd have fixed it by now



Dervish:
How important is the ostream& operator << to that implementation? I'm
using Windows Forms (which I should probably have said in the
beginning). My sort statement is nearly identical to yours & it's still
giving the same problem.



Axter:
> Consider using a clone smart pointer like that in the following:
> http://code.axter.com/copy_ptr.h
> or a COW smart pointer
> http://code.axter.com/cow_ptr.h

Working on it...

 
Reply With Quote
 
caxmester@gmail.com
Guest
Posts: n/a
 
      12-03-2005
Trying to give a larger sample code here

__gc class aaWord{
public:
//constructors etc are here

virtual String *getWordType() {
return title;
}


protected:
String * title;

};

__gc class aaWordCollection{

public:
//constructors etc are here

vector<gcroot<aaWord __gc*> > *getwordvector(){
return VectorofWords;
}
private:
vector<gcroot<aaWord __gc*> > *VectorofWords;

};



//within the form

Collection= new aaWordCollection();


....


bool mycompare(aaWord *x, aaWord *y){
gcroot<String*> s1 = x->gettitle();
gcroot<String*> s2 = y->gettitle();
int res = String::Compare(s1, s2);
return res <= 0;
}


....

sort(Collection->getwordvector()->begin(),
Collection->getwordvector()->end(), mycomparen);


I've been playing around a lot with the function so please ignore any
inconsistencies.

 
Reply With Quote
 
caxmester@gmail.com
Guest
Posts: n/a
 
      12-03-2005
Trying to give a larger sample code here

__gc class aaWord{
public:
//constructors etc are here

virtual String *gettitle() {
return title;

}

protected:
String * title;

};

__gc class aaWordCollection{

public:
//constructors etc are here

vector<gcroot<aaWord __gc*> > *getwordvector(){
return VectorofWords;
}

private:
vector<gcroot<aaWord __gc*> > *VectorofWords;

};

//within the form

Collection= new aaWordCollection();

....

bool mycompare(aaWord *x, aaWord *y){
gcroot<String*> s1 = x->gettitle();
gcroot<String*> s2 = y->gettitle();
int res = String::Compare(s1, s2);
return res <= 0;
}

....

sort(Collection->getwordvector()->begin(),
Collection->getwordvector()->end(), mycomparen);

I've been playing around a lot with the function so please ignore any
inconsistencies.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
After sorting a vector, how to get max in this vector? cylin C++ 3 12-19-2003 05:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57