且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何比较两个向量的内容

更新时间:2021-09-30 16:32:29

***的方法是将整个输入数据存储在树形结构中。如果你熟悉树形结构,这可能是最明显的解决方案。



因为你没有选择那条路,我认为你不熟悉建筑C ++中的一棵树。所以这里有一种更简单的方法来解决你的问题,只是基于你已经拥有的两个向量:



1.创建第三个向量,一个std ::矢量< INT&GT;并将其命名为level。我们的想法是将员工的级别存储在该向量中的公司层次结构中。老板被指定为0级,他的直接下属级别为1,等等。



2.最初填充值为-1的水平向量作为指示级别已经分配。



3.循环所有员工并尝试评估他们的级别。这在递归函数中最容易完成,我们称之为EvaluateLevel,其运算方式如下:



3a:如果该员工的经理为空(无),则分配等级0.



3b:否则搜索经理名称的名称向量并查找他的等级。如果尚未分配(即-1),则递归调用管理器索引上的EvaluateLevel函数。将一个添加到经理的级别并将其输入到员工的级别字段中。



4.最后循环遍历级别向量并找到最高值(或者您这样做只记住曾经分配过的***别的EvaluateLevel函数的副作用。



我没有在C ++代码中拼写出来,因为我想留下作为锻炼给你。如果您对实现有任何特殊问题,请告诉我。



[已添加]

递归函数将是这样的:

The best approach would be to store your entire input data in a tree structure. If you are familiar with tree-structures, this will probably the most obvious solution.

As you have not chosen that path, I assume that you are not familiar with building a tree in C++. So here is an easier way to solve your problem almost as efficiently, just based on the two vectors that you already have:

1. Create a third vector, a std::vector<int> and name it level. The idea is to store the employee's level in the company hierarchy in that vector. The boss is assigned level 0, his immediate subordinates level 1, and so forth.

2. Initially fill the level vector with the value of -1 as indicator that no level has been assigned yet.

3. Loop over all employees and try to evaluate their level. This is easiest done in a recursive function, lets call it EvaluateLevel, which operates like this:

3a: if the manager of that employee is null (none), assign level 0.

3b: else search the name vector for the managers name and look up his level. If it is not assigned yet (i.e. -1) recursivly call the EvaluateLevel function on the manager's index. Add one to the manager's level and enter it into the employee's level field.

4. Finally loop over the level vector and find the highest value (or you do that as a side effect of the EvaluateLevel function by just remembering the highest level ever assigned).

I haven't spelled that out in C++ code, as I want to leave that as an exercise to you. Just let me know if you have any particular problem with the implementation.

[ADDED]
The recursive function would be something like this:
using namespace std;

int EvaluateLevel (int idx,
    const vector<string>& names, 
    const vector<string>& managers, 
    vector<int>& levels)
{
    if (managers[idx].empty())
    {
        levels[idx] = 0;
        return 0;
    }
    if (levels[idx] != -1)
        return levels[idx];
    
    // find the index of the manager
    string manager = managers[idx];
    int managersIdx = -1;
    for (int i = 0; i < names.size(); ++i)
        if (names[i] == manager)
        {
            managersIdx = i;
            break;
        }
    // or better use std::find instead of the above loop if you are familiar
    // enough with STL

    // assign the manager's level + 1
    if (managersIdx = -1)
        return -1; // not found; error in input data
    int managersLevel = EvaluateLevel (managersIdx, names, managers, levels);
    if (managersLevel < 0)
        return -1; // recursive call returned an error
    levels[idx] = managersLevel + 1;
    return levels[idx];
}





在下一步中,尝试用描述a的类的单个向量替换三个并行向量person,即



In the next step, try to replace the three parallel vectors by a single vector of a class that describes a person, i.e.

class Person
{
public:
    string name;
    string manager;
    int    level;

    //...
};

vector <Person> 


hi,

简单比较2个向量你可以查看 [ ^ ]

希望它有所帮助!

for simply comparing 2 vectors you can check this out [^]
hope it helps !