Connecting Tech Pros Worldwide Forums | Help | Site Map

Sorting a vector of classes

caxmester@gmail.com
Guest
 
Posts: n/a
#1: Dec 2 '05
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?:(


Neelesh Bodas
Guest
 
Posts: n/a
#2: Dec 2 '05

re: Sorting a vector of classes



caxmester@gmail.com wrote:[color=blue]
>
> 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?:([/color]

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

n2xssvv g02gfr12930
Guest
 
Posts: n/a
#3: Dec 2 '05

re: Sorting a vector of classes


caxmester@gmail.com wrote:[color=blue]
> 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?:(
>[/color]
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
Dervish
Guest
 
Posts: n/a
#4: Dec 2 '05

re: Sorting a vector of classes


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::op_LessThan);
copy(g_A.begin(),g_A.end(),ostream_iterator<A*>(co ut,"\n"));
return true;
}

Axter
Guest
 
Posts: n/a
#5: Dec 2 '05

re: Sorting a vector of classes



caxmester@gmail.com wrote:[color=blue]
> 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.
>[/color]

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.

caxmester@gmail.com
Guest
 
Posts: n/a
#6: Dec 2 '05

re: Sorting a vector of classes


Neelesh Bodas:[color=blue]
> As it indicates, this is VC++ specific. Try your code on another
> compiler.[/color]

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.
[color=blue]
>std::sort(vs.begin(),vs.end(),&scmp);[/color]
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:[color=blue]
> 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[/color]
Working on it...

caxmester@gmail.com
Guest
 
Posts: n/a
#7: Dec 3 '05

re: Sorting a vector of classes


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.

caxmester@gmail.com
Guest
 
Posts: n/a
#8: Dec 3 '05

re: Sorting a vector of classes


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.

Closed Thread