问:STL向量 [英] Q: STL vector

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

问题描述




我有一个存储在矢量中的值列表,例如


std :: vector< int> x;


x [0] = 1;

x [1] = 4;

x [2] = 2;

x [3] = 4;

x [4] = 6;

x [5] = 1;

x [6 ] = 4;


任何人都可以提出一种有效的方法来迭代向量,这样

向量只包含唯一值,例如向量中只有一个4的实例出现在

中。也就是说,新的矢量一旦被处理,将是:


{1,4,2,6}


提前致谢


David

Hi

I have a list of values stored in a vector e.g.

std::vector<int> x;

x[0] = 1;
x[1] = 4;
x[2] = 2;
x[3] = 4;
x[4] = 6;
x[5] = 1;
x[6] = 4;

Can anybody suggest an efficient way to iterate through the vector so that
the vector only contains unique values e.g. only one instance of 4 occurs in
the vector. That is, the new vector, once processed, would be:

{1,4,2,6}

Thanks in advance

David

推荐答案

" David Jones" <哒** @ ymchwil.NODAMNSPAM.com>在消息中写道

news:3f ********************* @ news.dial.pipex.com ..。
"David Jones" <da**@ymchwil.NODAMNSPAM.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .


我有一个存储在矢量中的值列表,例如

std :: vector< int> x;

x [0] = 1;
x [1] = 4;
x [2] = 2;
x [3] = 4;
x [4] = 6;
x [5] = 1;
x [6] = 4;

任何人都可以提出一种有效的迭代方法向量使得向量只包含唯一值,例如向量中只有一个4的实例出现
。也就是说,新的矢量一旦被处理,将是:

{1,4,2,6}
Hi

I have a list of values stored in a vector e.g.

std::vector<int> x;

x[0] = 1;
x[1] = 4;
x[2] = 2;
x[3] = 4;
x[4] = 6;
x[5] = 1;
x[6] = 4;

Can anybody suggest an efficient way to iterate through the vector so that
the vector only contains unique values e.g. only one instance of 4 occurs in the vector. That is, the new vector, once processed, would be:

{1,4,2,6}




查找标准: :STL引用中的sort和std :: unique。示例:


std :: sort(x.begin(),x.end());

std :: unique(x.begin() ,x.end());


您需要包含< algorithm>首先。


HTH,


Paul



Look up std::sort and std::unique in an STL reference. Example:

std::sort(x.begin(), x.end());
std::unique(x.begin(), x.end());

You''ll need to include <algorithm> first.

HTH,

Paul


非常感谢您的快速回复保罗。非常有用!


为了简化问题,我已经指定了一个整数向量。然而,对于生活中的大部分事情而言,我正在努力解决的真正问题是稍微复杂一些。我所拥有的向量不是整数向量,而是一个向量

的对象,其中有一个成员标识一个名称,即每个对象都有一个

char成员。向量的元素将此成员设置为A,B

等。我想从向量中删除具有相同名称的元素

即例如,只有一个向量将具有名称C。


我猜这是同一类型的问题,因此我的初始帖子,但是一个

稍微复杂一点。


我们非常感谢任何建议。


David
Many thanks for your prompt reply Paul. Very useful!

To simplify the problem I''ve specified a vector of integers. However, as
with most things in life, the true problem I''m trying to solve is a little
bit more complex. The vector I have is not a vector of integers but a vector
of objects which have a member identifying a name i.e. each object has a
char member. The elements of the vector have this member set as "A", "B"
etc. I want to remove elements from the vector which have identical "names"
i.e. only one vector will, for example, have name "C".

I guess that this is the same type of problem, hence my initial post, but a
little more complex.

Any advice will be most appreciated.

David




" Paul Thompson" < PA ** @ msdmagic.co.uk>在消息中写道

news:bf ********** @ hercules.btinternet.com ...

"Paul Thompson" <pa**@msdmagic.co.uk> wrote in message
news:bf**********@hercules.btinternet.com...
" David Jones" <哒** @ ymchwil.NODAMNSPAM.com>在消息中写道
新闻:3f ********************* @ news.dial.pipex.com ..。
"David Jones" <da**@ymchwil.NODAMNSPAM.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .


我有一个存储在矢量中的值列表,例如

std :: vector< int> x;

x [0] = 1;
x [1] = 4;
x [2] = 2;
x [3] = 4;
x [4] = 6;
x [5] = 1;
x [6] = 4;

任何人都可以提出一种有效的迭代方法向量所以
,向量只包含唯一值,例如在
Hi

I have a list of values stored in a vector e.g.

std::vector<int> x;

x[0] = 1;
x[1] = 4;
x[2] = 2;
x[3] = 4;
x[4] = 6;
x[5] = 1;
x[6] = 4;

Can anybody suggest an efficient way to iterate through the vector so that the vector only contains unique values e.g. only one instance of 4
向量中只出现一个4


的实例。也就是说,新的向量一旦被处理,将是:

{1,4,2,6}

the vector. That is, the new vector, once processed, would be:

{1,4,2,6}



查找std :: sort和std :: STL参考中的唯一。示例:

std :: sort(x.begin(),x.end());
std :: unique(x.begin(),x.end());

你需要包含< algorithm>首先。

HTH,

保罗



Look up std::sort and std::unique in an STL reference. Example:

std::sort(x.begin(), x.end());
std::unique(x.begin(), x.end());

You''ll need to include <algorithm> first.

HTH,

Paul




除了当然std :: unique,就像任何其他STL算法一样,实际上不会从向量中删除
。你需要做


ox.erase(std :: unique(ox.begin(),ox.end()),ox.end());





john



Except of course std::unique, like any other STL algorithm, will not
actually erase the items from the vector. You need to do

ox.erase(std::unique(ox.begin(), ox.end()), ox.end());

for that.

john


这篇关于问:STL向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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