Sorting a vector of classes

C

caxmester

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?:(
 
N

Neelesh Bodas

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.
 
N

n2xssvv g02gfr12930

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
 
D

Dervish

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*>(cout,"\n"));
sort(g_A.begin(),g_A.end(),A::eek:p_LessThan);
copy(g_A.begin(),g_A.end(),ostream_iterator<A*>(cout,"\n"));
return true;
}
 
A

Axter

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.
 
C

caxmester

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
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...
 
C

caxmester

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.
 
C

caxmester

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top