473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Dec 2 '05 #1
7 4072

ca*******@gmail.com 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.

Dec 2 '05 #2
ca*******@gmail.com 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
Dec 2 '05 #3
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;
}

Dec 2 '05 #4

ca*******@gmail.com 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.

Dec 2 '05 #5
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...

Dec 2 '05 #6
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.

Dec 3 '05 #7
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.

Dec 3 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
3
by: cylin | last post by:
Dear all, After sorting a vector, how to get max in this vector? I got a strange result of my test code. ------------------------------------------------- #include <iostream> #include <vector>...
0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
2
by: fred_stevens | last post by:
Hi all you C boffins: I need to sort a vector of doubles is ascending order. Qsort will return the sorted vector, but I need a vector of the indices of the sorted vector, not the actual sorted...
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
3
by: Pino | last post by:
HI all, I am learning c++ ( just 1 month) and would want an aid for a code. I have a integer vector of length N. ie 3 3 3 4 4 1
6
by: Tim Frink | last post by:
Hi, I need some help with STL lists. I've a list with pointers to some objects, let's say std::list< ObjectA* myList. The objects of the class ObjectA all have a function "int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.