簡體   English   中英

在地圖上找到std :: find不能正常工作,並通過地圖的鍵和值進行迭代

[英]std::find on map not functioning properly and iterating through map's key and values

我目前正在嘗試訪問地圖的特定值元素,並遍歷該地圖以打印其特定元素的所有地圖鍵,但是在我在IDE(Eclipse)上進行調試時遇到了錯誤。這場斗爭? 過去幾天一直在努力尋找答案。

當調用std :: find時,它說,

'沒有匹配的成員函數來調用'find'

當使用迭代器遍歷地圖時,它說,

對二進制表達式無效的操作數(“ ostream”(又名“ basic_ostream”)和“ const std :: __ 1 :: vector>”)

#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
    srand(time(NULL));

    cout << "Enter # of rows: ";
    int row;
    cin >> row;
    cout << "Enter # of columns: ";
    int column;
    cin >> column;

    vector<vector<int> > numberBoard;

    int startingIndex = 1;
    for(int i = 0; i < row; i++){
        vector<int> temp;
        for(int j = 0; j < column; j++){
            temp.push_back(startingIndex);
            startingIndex++;
        }
        numberBoard.push_back(temp);
    }

    cout <<"Number Board:" << endl;
    for(int i = 0; i < numberBoard.size(); i++){
        for(int j = 0; j < numberBoard[i].size(); j++){
            cout.width(5);
            cout << numberBoard[i][j];
        }
        cout << endl;
    }

        vector<vector<char> > hiddenBoard(row, vector<char>(column));

        // looping through outer vector vec
        for (int i = 0; i < row; i++) {
          // looping through inner vector vec[i]
          for (int j = 0; j < column; j++) {
              int random = rand()% 32 + 65;
            (hiddenBoard[i])[j] = char(random);
            //i*n + j;
          }
        }

        cout << "\nBoard:" << endl;
        for(int i = 0; i < hiddenBoard.size(); i++){
            for(int j = 0; j < hiddenBoard[i].size(); j++){
                cout.width(5);
                cout << hiddenBoard[i][j];
            }
            cout << endl;
        }
        map<vector<int>, vector<char> > boardMap;
        for(int i = 0; i < 20; i++){
                boardMap[numberBoard[i]] = hiddenBoard[i];
        }

        //using std::find
        int slotNum = 3;
        map<vector<int>, vector<char> >::iterator it = boardMap.find(slotNum);

        //trying to iterate through the map to print its corresponding key and value.
        for(map<vector<int>, vector<char> >::iterator it = boardMap.begin(); it != boardMap.end(); it++){
                cout << it->first << " => " << it->second << endl;
        }

    return 0;
}

首先,我很好奇要知道您的程序應該執行的操作,這需要將int向量映射為chars向量的映射。 該地圖存儲成對的int向量和char向量,例如:

3 8 5 7 => c j i e
5 7 2 0 => l o x w
1 4 3 8 => k r u a

也許您想要一個std::map<int, char> ,它將一個int映射到一個char上,例如:

5 => m
2 => c
0 => a

如果您確實要打印出std::map<std::vector<int>, std::vector<char>> ,則需要像已經做的那樣遍歷鍵值對,然后通過循環分別打印出鍵向量和值向量。

例:

#include <iostream>
#include <map>
#include <vector>

int main()
{
    std::map<std::vector<int>, std::vector<char>> boardMap; //declare and insert some values for testing
    boardMap.insert(std::make_pair(std::vector<int>({ 8, 6, 2, 7 }), std::vector<char>({ 'o', 'd', 'a' })));
    boardMap.insert(std::make_pair(std::vector<int>({ 4, 1, 0, 4 }), std::vector<char>({ 's', 'd', 'l' })));

    for (auto it = boardMap.begin(); it != boardMap.end(); ++it)
    { //using auto is much better than writing std::map<std::vector<int>, std::vector<char>>::iterator every time
        for (auto vecIt = it->first.begin(); vecIt != it->first.end(); ++vecIt) //output key vector
        {
            std::cout << *vecIt << ' ';
        }
        std::cout << "=>";
        for (auto vecIt = it->second.begin(); vecIt != it->second.end(); ++vecIt) //output value vector
        {
            std::cout << ' ' << *vecIt;
        }
        std::cout << '\n';
    }
    return 0;
}

具有范圍for循環的更優雅的版本:

#include <iostream>
#include <map>
#include <vector>

int main()
{
    std::map<std::vector<int>, std::vector<char>> boardMap; //declare and insert some values for testing
    boardMap.insert(std::make_pair(std::vector<int>({ 8, 6, 2, 7 }), std::vector<char>({ 'o', 'd', 'a' })));
    boardMap.insert(std::make_pair(std::vector<int>({ 4, 1, 0, 4 }), std::vector<char>({ 's', 'd', 'l' })));

    for (auto &keyValuePair : boardMap)
    { //using auto is much better than writing std::map<std::vector<int>, std::vector<char>>::iterator every time
        for (auto &keyNum : keyValuePair.first) //output key vector
        {
            std::cout << keyNum << ' ';
        }
        std::cout << "=>";
        for (auto &valueNum : keyValuePair.second) //output value vector
        {
            std::cout << ' ' << valueNum;
        }
        std::cout << '\n';
    }
    return 0;
}

盡管最好的方法是使用<algorithm>標頭中的函數,例如std::for_each和lambdas為您完成所有循環。 我雖然不擅長這些東西。

對於您的查找操作,您有一個以std::vector<int>作為鍵的映射,但是您為std::map::find函數提供了一個整數。 如果您確實打算使用向量作為鍵的映射,則需要為函數提供向量,例如:

auto findResult = boardMap.find(std::vector<int>({8, 5, 2, 6}));

這將構造一個包含四個數字的向量,並將其賦予std::map::find

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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