Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 2nd, 2005, 11:15 AM
caxmester@gmail.com
Guest
 
Posts: n/a
Default Sorting a vector of classes

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

  #2  
Old December 2nd, 2005, 11:45 AM
Neelesh Bodas
Guest
 
Posts: n/a
Default 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.

  #3  
Old December 2nd, 2005, 12:45 PM
n2xssvv g02gfr12930
Guest
 
Posts: n/a
Default 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
  #4  
Old December 2nd, 2005, 12:55 PM
Dervish
Guest
 
Posts: n/a
Default 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;
}

  #5  
Old December 2nd, 2005, 02:35 PM
Axter
Guest
 
Posts: n/a
Default 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.

  #6  
Old December 2nd, 2005, 10:05 PM
caxmester@gmail.com
Guest
 
Posts: n/a
Default 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...

  #7  
Old December 3rd, 2005, 12:25 AM
caxmester@gmail.com
Guest
 
Posts: n/a
Default 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.

  #8  
Old December 3rd, 2005, 12:25 AM
caxmester@gmail.com
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles