维护向量的顺序(maintaining the order of vectors)

编程入门 行业动态 更新时间:2024-10-24 18:27:05
维护向量的顺序(maintaining the order of vectors)

可以说我有1个矢量名称和另一个电话号码矢量。 首先,用户将输入姓名(不排序,意味着他们不是从a到z),然后,用户将输入相应的电话号码。

在填充两个向量之后,程序然后在名称向量(向量1)中执行排序机制。 问题现在是矢量2(因为没有采用机制将其映射到矢量1)。

例:

vector name | vector telephone f 232132 a 34242342 b 997345 the result will be vector name | vector telephone a 232132 b 34242342 f 997345

正如你所看到的,矢量电话尚未调整。 我们该如何调整呢? 谢谢

lets say I have 1 vector of names and another vector for the telephone numbers. First, the user will enter names (not sorted, meaning they are not organized from a to z), then, the user will enter the corresponding telephone number.

After filling out both vectors, the program then executes sorting mechanism in the name vector(vector 1). The problem is now the vector 2, (since there is no adopting mechanism to map it to vector 1).

Example:

vector name | vector telephone f 232132 a 34242342 b 997345 the result will be vector name | vector telephone a 232132 b 34242342 f 997345

as you can see, the vector telephone hasnt been adjusted. how can we adjust this?? thanks

最满意答案

创建一个包含名称字符串和电话号码字符串/ int的结构体。 线性查看并记录名称信息。 再次浏览并记录手机号码信息。 然后排序。

如果你不想创建一个类,你可以使用一个pair对象。

vector<pair<string,int> > nameAndNumber;

编辑:修正了一个错误,谢谢smocking

Create a struct that holds a string for the name and a string/int for the phone number. Go through it linearly and record the name information. Go through it again and record the phone # information. Then sort.

If you do not wish to create a class, you can use a pair object.

vector<pair<string,int> > nameAndNumber;

Edit: fixed a bug, thanks smocking

更多推荐

本文发布于:2023-08-03 08:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1384975.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   顺序   maintaining   order   vectors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!