簡體   English   中英

如何將對向量中的對的第一個元素(字符串)與另一個字符串進行比較?

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

我正在嘗試實施我對std::vector了解來解決問題。 用戶將輸入一個動物名稱列表(未指定數量),我需要在用戶輸入中記錄動物名稱及其出現次數。 起初,我嘗試使用數組,但由於它是靜態的,我轉向std::vector 同樣,一開始,我嘗試使用兩個std::vector ,一個是int類型,另一個是string類型來存儲動物名稱和出現次數。 但是,稍后對兩個向量進行排序似乎有點困難,而且在我看來,帶有pair類型的std::vector聽起來更好。 現在,我被下面的代碼困住了,但我不太明白:

#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;
}

您應該簡單地使用std::map作為容器類型,因為它已經排序,並且具有帶有 operator[] 的易於使用的訪問接口。 在這里,我們使用您的動物和動物園中的數量創建了一個std::map

例子:

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; 
}

如您所見,不需要額外的排序,因為映射總是針對每個條目的第一部分(名為“鍵”)進行排序。

std::vector相同。 請注意,您必須為排序和查找提供運算符!

完整示例:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM