473,756 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<a aWord __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 4100

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<a aWord __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::strin g *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.be gin(),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(n ew A("==========") );
g_A.push_back(n ew A("foo"));
g_A.push_back(n ew A("bar"));
g_A.push_back(n ew A("baz"));

copy(g_A.begin( ),g_A.end(),ost ream_iterator<A *>(cout,"\n")) ;
sort(g_A.begin( ),g_A.end(),A:: op_LessThan);
copy(g_A.begin( ),g_A.end(),ost ream_iterator<A *>(cout,"\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<a aWord __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::st ring *> vs in your
case) is a pointer.
std::sort(vs.b egin(),vs.end() ,&scmp); Which in my case would be
std::sort(Colle ction->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 aaWordCollectio n{

public:
//constructors etc are here

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

};

//within the form

Collection= new aaWordCollectio n();
....
bool mycompare(aaWor d *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 aaWordCollectio n{

public:
//constructors etc are here

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

private:
vector<gcroot<a aWord __gc*> > *VectorofWords;

};

//within the form

Collection= new aaWordCollectio n();

....

bool mycompare(aaWor d *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
3417
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 objects in the vector vec_A. Suppose there are K attributes to be added. For each of the attributes I define K vectors of appropriate types. Say, the attributes have types type1, type2, ..., typeK. So I define std::vector<type1> attr1(vec_A.size());
3
4619
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> #include <algorithm> using namespace std;
0
2667
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to Function Pointers Source : Technical Report on C++ Performance Tool : The C/C++ Program Perfometer (Version 2.8.1-1.19-Beta) * http://sourceforge.net/projects/cpp-perfometer/
2
4157
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 vector. Any ideas? Thanks in advance, Fred.
9
2838
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
2793
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 write such layers myself I got stuck on how to get filtered or sorted data from the data-layer. This is what I got: Objects
0
2579
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 sort the records in a plain-text index file based on certain columns. The index file consists of records and fields within the records. The individual fields are separated by semicolons, the records by newlines. The index file is loaded into memory...
3
1896
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
2183
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 getIntValue()". What I need
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.