简体   繁体   English

在地图上找到std :: find不能正常工作,并通过地图的键和值进行迭代

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

I am currently trying to access specific value elements of my map and iterate through the map to print all of my map's keys with its specific elements but am running through errors when I am debugging on my IDE (Eclipse) Any ideas on how I can overcome this struggle? 我目前正在尝试访问地图的特定值元素,并遍历该地图以打印其特定元素的所有地图键,但是在我在IDE(Eclipse)上进行调试时遇到了错误。这场斗争? Been trying to find answer for the past few days. 过去几天一直在努力寻找答案。

when calling for std::find, it says, 当调用std :: find时,它说,

'no matching member function for call to 'find' '没有匹配的成员函数来调用'find'

when using iterator to iterator through map it says, 当使用迭代器遍历地图时,它说,

invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'const std::__1::vector >') 对二进制表达式无效的操作数(“ 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;
}

First of all, I'm curious to know what you're program is supposed to do that requires a map that maps a vector of ints to a vector of chars. 首先,我很好奇要知道您的程序应该执行的操作,这需要将int向量映射为chars向量的映射。 The map stores pairs of int vectors and char vectors like: 该地图存储成对的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

Perhaps you want an std::map<int, char> which maps an int to a char like: 也许您想要一个std::map<int, char> ,它将一个int映射到一个char上,例如:

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

If you do want to print out the contents of an std::map<std::vector<int>, std::vector<char>> , you need to iterate over the key-value pairs like you're already doing, and then print out the key vector and the value vector separately with loops. 如果您确实要打印出std::map<std::vector<int>, std::vector<char>> ,则需要像已经做的那样遍历键值对,然后通过循环分别打印出键向量和值向量。

Example: 例:

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

More elegant version with ranged for loops: 具有范围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;
}

Although the best way would be to use functions from the <algorithm> header such as std::for_each and lambdas to do all the looping for you. 尽管最好的方法是使用<algorithm>标头中的函数,例如std::for_each和lambdas为您完成所有循环。 I'm not good at that stuff though. 我虽然不擅长这些东西。

For your find operation, You have a map with std::vector<int> as the key, but you give a single int to the std::map::find function. 对于您的查找操作,您有一个以std::vector<int>作为键的映射,但是您为std::map::find函数提供了一个整数。 If you really intend to have a map with vectors as keys, you need to give a vector to the function like: 如果您确实打算使用向量作为键的映射,则需要为函数提供向量,例如:

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

This constructs a vector containing four numbers and gives it to std::map::find . 这将构造一个包含四个数字的向量,并将其赋予std::map::find

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM