如何比较两个向量的内容 [英] How do I compare the content of two Vectors

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

问题描述

我正在尝试用C ++编写一个程序,它应该输入一个字符串数组,字符串应该包含Employee Name Salary,它的管理器名称用逗号分隔的分隔符



输入将按以下格式提供:

XX先生,5200000,无

YY先生,52000,XX先生

ZZ先生,34500 ,YY先生



有一位员工的经理是无人的,这意味着他是公司的老板。现在我想要员工字符串数组列表中距离公司老板最远的员工的姓名。对于上面的输入,输出应该是ZZ先生



我已经在字符串数组中输入并运行整个数组的循环。在循环中,我使用逗号分隔在子字符串中拆分字符串以获取员工姓名和经理名称,并将结果存储在两个向量中。现在我正在比较这些向量的结果,但是我无法编写可以产生所需输出的高效算法。任何方向都会很有帮助





PS:

复制以下问题和代码来自评论如下:






当我在EvaluateLevel中传递我的矢量时,我的代码崩溃了。



这是我的代码:

I am trying to write a program in C++ which should input a string array, and string should contain Employee Name Salary and Its Manager Name with comma seperated delimeter

The input will be provided following format:
Mr XX, 5200000, NONE
Mr YY,52000, Mr. XX
Mr ZZ, 34500, Mr. YY

There is a Employee whose Manager is NONE that means he is the boss of the company. Now I wanted the name of the employee who is farthest from the boss of the company in the list of employee string array. For above input the out put should be Mr. ZZ

I have taken input in string array and run the loop for the whole array. In the loop I have splitted the string in the substring with comma seperation to get the employee name and manager name and stored the result in two vectors. Now I am comparing the result of these vectors but am not able to write the efficient alogorithm which can result the desired output. Any direction would be great help


P.S.:
copied the following issue and code from comments below:



When am passing my vectors in the EvaluateLevel my code is crashing.

Here's my code:

int EvaluateLevel (int idx,
    const vector<string>& names,
    const vector<string>& managers,
    vector<int>& levels)
{
    if (managers[idx]=="NONE")
    {
        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];
}
int main()
{
const int size =2;
string text[size] ;
vector<std::string> EmpName;
vector<std::string> MgrName;
vector<int> level;
cout << "Enput the Strings" << endl;
 
    for(int m =0 ;m<size;m++) { cin>> text[m];
        }
 
for(int n =0;n<size;n++) { std::vector<std::string> words;
    std::string::size_type beg = 0, end;
    do
    {
        end = text[n].find(',', beg);
        if (end == std::string::npos)
        {
            end = text[n].size();
        }
        words.emplace_back(text[n].substr(beg, end - beg));
        beg = end + 1;
    } while (beg < text[n].size());
 

 
    EmpName.push_back(words[0]);
    MgrName.push_back(words[2]);

	EvaluateLevel(size,EmpName,MgrName,level);
 

}
 

 
  
 
   cin.get();
 
}<pre lang="c++">

推荐答案

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



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



1.创建第三个向量,一个std ::矢量< INT>并将其命名为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 !


这篇关于如何比较两个向量的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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