矢量类字符串排序 [英] vector class string sort

查看:68
本文介绍了矢量类字符串排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在我需要做的事情上遇到了一些问题。

我正在使用一个向量来存储一个数组一个特定的类对象已满

的数据。


class part_info

{

public:

string part_no;

string raw_qty;

int qty;

int ref;


part_info():part_no(""),raw_qty(""),qty(0),ref(0){}

part_info(string NewPartNo,string NewRawQty,int NewQty,int

NewRef):part_no(NewPartNo),raw_qty(NewRawQty),qty(NewQty),

ref(NewRef){}


};


.....


无效设置{

NAMEVECTOR theVector;

NAMEVECTOR :: iterator theIterator;

.....


//向我的向量添加数据集

theVector.push_back(part_info(part_no,raw_qty,qty,ref));


}


我已经完成了将数据添加到我的数据中vector我希望能够

按part_no排序。

我已经看过了覆盖运算符<然后使用std :: sort,

但是我正在编译错误,而我理解了基本的

发生了什么,我必须遗漏一些东西。


有人可以帮我这个吗?


谢谢

Venthor



typedef vector< part_infoNAMEVECTOR;

解决方案

Venthor写道:


您好,

我在我需要做的事情上遇到了一些问题。

我正在使用矢量来存储一个特定类对象的数组完整

的数据。


class part_info

{

public:

string part_no;

string raw_qty;

int qty;

int ref;


part_info():part_no(""),raw_qty(""),qty(0),ref(0){}

part_info(字符串NewPartNo,字符串NewRawQty,int NewQty,int

NewRef):part_no(NewPartNo),raw_qty(NewRawQty),qty(NewQty),

ref(NewRef){}


};


....


无效设置{

NAMEVECTOR theVector;

NAMEVECTOR :: iterator theIterator;

....


//向我的向量添加数据集

theVector。 push_back(part_info(part_no,raw_qty,qty,ref));


}


当我把数据添加到我的vector我希望能够

按part_no排序。

我已经看过了覆盖运算符<然后使用std :: sort,

但是我正在编译错误,而我理解了基本的

发生了什么,我必须遗漏一些东西。


有人可以帮我这个吗?


谢谢

Venthor



typedef vector< part_infoNAMEVECTOR;



我们如何在没有错误消息和源代码的情况下为您提供帮助?


5月10日下午3:38,Venthor< vent ... @ gmail.comwrote:


你好,

我在绕着我需要做的事情时遇到了一些问题。

我正在使用向量存储特定类对象的数组

数据。


class part_info

{

public:

string part_no;

string raw_qty;

int qty;

int ref;


part_info() : PART_NO(QUOT; "),raw_qty(""),qty(0),ref(0){}

part_info(string NewPartNo,string NewRawQty,int NewQty,int

NewRef):part_no(NewPartNo),raw_qty(NewRawQty),qty(NewQty),

ref(NewRef){}


};


....


无效设置{

NAMEVECTOR theVector;

NAMEVECTOR :: iterator the itterator;

....


//向我的向量添加数据集

theVector.push_back(part_info(part_no) ,raw_qty,qty,ref));


}


当我把数据添加到我的矢量中时,我想成为能够

按part_no排序。

我已经看过覆盖运营商<然后使用std :: sort,

但是我正在编译错误,而我理解了基本的

发生了什么,我必须遗漏一些东西。


有人可以帮我这个吗?


谢谢

Venthor


typedef vector< part_infoNAMEVECTOR;



重载< partinfo类中的运算符(比较部分号)。然后

排序成员应该为你排序向量。


你得到了什么确切的编译错误?


在消息< 11 ********************** @ n59g2000hsh.googlegroups .com> ;,

Venthor< ve ***** @ gmail.comwrites


>你好,
我在绕着什么绕过什么问题时遇到了什么问题我需要这样做。
我正在使用一个向量来存储一个特定类对象数组的数据。

class part_info
{
public:

string part_no;

string raw_qty;

int qty;

int ref;


part_info():part_no(""),raw_qty(""),qty(0),ref(0){}

part_info (字符串NewPartNo,字符串NewRawQty,int NewQty,int
NewRef):part_no(NewPartNo),raw_qty(NewRawQty),qty(NewQty),
ref(NewRef){}

>};

....

d setup {
NAMEVECTOR theVector;

NAMEVECTOR :: iterator theIterator;
....
//将数据集添加到我的vector
theVector.push_back(part_info(part_no,raw_qty,qty,ref));

}

当我把我的数据添加到我的矢量时我想要能够按part_no对它进行排序。
我已经看过了覆盖运算符<然后使用std :: sort,
但我正在编译错误,虽然我理解了基本的
发生了什么,但我必须遗漏一些东西。

有人可以帮我这个吗?



当然。你对被覆盖的运营商的定义在哪里?()?

-

Richard Herring


Hello,
I''m having some problems wrapping my head around what I need to do.
I''m using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

.....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
.....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty, ref));

}

When I''m finished adding my data to my vector I want to be able to
sort it by part_no.
I''ve looked at overriding the operator < and then using the std::sort,
but I''m getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Thanks
Venthor


typedef vector<part_infoNAMEVECTOR;

解决方案

Venthor wrote:

Hello,
I''m having some problems wrapping my head around what I need to do.
I''m using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty, ref));

}

When I''m finished adding my data to my vector I want to be able to
sort it by part_no.
I''ve looked at overriding the operator < and then using the std::sort,
but I''m getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Thanks
Venthor


typedef vector<part_infoNAMEVECTOR;

How can we help you without the error messages and the source code?


On May 10, 3:38 pm, Venthor <vent...@gmail.comwrote:

Hello,
I''m having some problems wrapping my head around what I need to do.
I''m using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty, ref));

}

When I''m finished adding my data to my vector I want to be able to
sort it by part_no.
I''ve looked at overriding the operator < and then using the std::sort,
but I''m getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Thanks
Venthor

typedef vector<part_infoNAMEVECTOR;

Overload < operator in the partinfo class (compare on part num). The
sort member should then sort the vector for you.

What exact compile errors are you getting?


In message <11**********************@n59g2000hsh.googlegroups .com>,
Venthor <ve*****@gmail.comwrites

>Hello,
I''m having some problems wrapping my head around what I need to do.
I''m using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty ,ref));

}

When I''m finished adding my data to my vector I want to be able to
sort it by part_no.
I''ve looked at overriding the operator < and then using the std::sort,
but I''m getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Sure. Where''s your definition for the overridden operator<() ?

--
Richard Herring


这篇关于矢量类字符串排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆