简体   繁体   English

如何将对向量中的对的第一个元素(字符串)与另一个字符串进行比较?

[英]How do I compare the first element (string) of a pair in a pair vector with another string?

I'm trying to implement what I've learned about std::vector to solve a problem.我正在尝试实施我对std::vector了解来解决问题。 User is going to input a list of animal names (not specified how many) and I need to record the animal names and their occurrences in the user input.用户将输入一个动物名称列表(未指定数量),我需要在用户输入中记录动物名称及其出现次数。 At first, I tried using an array, but as it's static, I turned to std::vector .起初,我尝试使用数组,但由于它是静态的,我转向std::vector Again, at first, I tried using two std::vector , one with type int and the other with type string to store the animal names and the amount of occurrences.同样,一开始,我尝试使用两个std::vector ,一个是int类型,另一个是string类型来存储动物名称和出现次数。 However, it seems a bit hard to sort both vectors later and a std::vector with a pair type sounds better in my mind.但是,稍后对两个向量进行排序似乎有点困难,而且在我看来,带有pair类型的std::vector听起来更好。 And now, I'm stuck with this code below with errors I don't quite understand:现在,我被下面的代码困住了,但我不太明白:

#include <bits/stdc++.h>
#include <sstream>

using namespace std;

int position(vector< pair<string, int> > myList, string animalName) {
    int pos;
    for (int i = 0; i < myList.size(); i++) if (animalName.compare(myList[i].first) == 0) pos = i;
    return pos;
}

int main() {
    int Q;
    cin >> Q;

    vector< pair<string, int> > zooPair;
    string animal;

    for (int i = 0; i < Q; i++){
        cin >> animal;
        if (find_if(zooPair.begin(), zooPair.end(), animal.compare(zooPair.first) == 0) == zooPair.end())
            zooPair.emplace_back(animal, 1);
        else
            zooPair[position(zooPair, animal)].second += 1;
    }

    sort(zooPair.begin(), zooPair.end());

    for (vector< pair<string, int> >::iterator it = zooList.begin(); it != zooList.end(); it++)
        cout << *it;

    return 0;
}

You simply should use std::map as the container type, because it already sorts, have a easy to use access interface with operator[].您应该简单地使用std::map作为容器类型,因为它已经排序,并且具有带有 operator[] 的易于使用的访问接口。 Here we create a std::map with your animal and the number of them in your zoo.在这里,我们使用您的动物和动物园中的数量创建了一个std::map

Example:例子:

int main() {
    int Q;
    std::cout << "Enter number of entries" << std::endl;
    std::cin >> Q;

    std::map<std::string, int> zoo;

    std::string animal;

    for (int i = 0; i < Q; i++){
        std::cout << "Enter animal" << std::endl;
        std::cin >> animal;
        zoo[animal]++;
    }

    for ( auto& it: zoo )
    {
        std::cout << it.first << " " << it.second << std::endl;
    }


    return 0; 
}

As you see, there is no need for an additional sort as a map is always sorted for the first part, named the "key", of every entry.如您所见,不需要额外的排序,因为映射总是针对每个条目的第一部分(名为“键”)进行排序。

The same with std::vector .std::vector相同。 Remark that you have to give operators for sort and find!请注意,您必须为排序和查找提供运算符!

Full example:完整示例:

struct SortableElements: public std::pair< std::string, int >
{
    // forward construction 
    using std::pair<std::string, int>::pair;

    // use for sort: 
    bool operator < (const SortableElements& e2 ) const
    {
        return first < e2.first;
    }

    // use for find: 
    bool operator == ( const std::string& e2 ) const
    {
        return first == e2;
    }
};



int main() 
{   
    std::vector< SortableElements > zoo;

    int Q;
    std::cout << "Enter number of entries" << std::endl;
    std::cin >> Q;

    std::string animal;

    for (int i = 0; i < Q; i++){
        std::cout << "Enter animal" << std::endl;
        std::cin >> animal;

        auto it = std::find( zoo.begin(), zoo.end(), animal);
        if ( it != zoo.end())
        {
            it->second++;
        }
        else
        {
            zoo.emplace_back( animal, 1 );
        }
    }

    // sort:
    std::sort(zoo.begin(), zoo.end());

    for ( auto& it: zoo )
    {
        std::cout << it.first << " " << it.second << std::endl;
    }

    return 0;
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换矢量<pair<string, t> &gt; 到矢量<pair<string,string> &gt; </pair<string,string></pair<string,> - How to convert vector<pair<string, T>> to vector<pair<string,string>> 如何复制向量中的字符串<pair<string, int> &gt; 到矢量<string> ? </string></pair<string,> - How do I copy the strings in a vector<pair<string, int>> to vector<string>? 如何从两个 arrays 中创建一个对向量,然后使用 CUDA/Thrust 按对的第一个元素进行排序? - How do you make a pair vector out of two arrays and then sort by the first element of the pair using CUDA/Thrust? 如何根据成对中的第一个和第二个元素对成对向量进行排序? - How do I sort a vector of pairs based on both first and second element in a pair? 将一对值与字符串进行比较 - compare a value of a pair with a string 如何对vector &lt;pair &lt;int,pair进行排序 <int , pair<string , pair<int , int > &gt;&gt;&gt;&gt;? - How to sort vector< pair< int , pair<int , pair<string , pair<int , int > > > > >? 如何根据对的第二个元素对对的向量进行排序? - How do I sort a vector of pairs based on the second element of the pair? 如何对矢量进行排序 <pair<string , pair<int , int> &gt;&gt;? - How to sort a vector<pair<string , pair<int , int>>>? 向量对的第一个元素的范围 - Range of the first element of a vector pair 我如何进入一对双 <pair<string,string> ,double&gt;在C ++ 11中 - How do I access the double of pair<pair<string,string>,double> in C++11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM