std :: find在地图上运行不正常并迭代地图的键和值

时间:2018-05-06 02:27:31

标签: c++ vector maps

我目前正在尝试访问我的地图的特定值元素并遍历地图以使用其特定元素打印我的所有地图键,但在我的IDE(Eclipse)上调试时遇到错误关于如何克服这场斗争的任何想法?过去几天一直想找到答案。

在调用std :: find时,它说,

  

'没有匹配的会员功能可以拨打'查找'

当通过map使用迭代器到迭代器时,它说,

  

二进制表达式的无效操作数(' 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;
}

1 个答案:

答案 0 :(得分: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;
}

更优雅的版本,带有ranged 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函数提供了一个int。如果你真的想要一个带矢量的地图作为键,你需要给这个函数赋一个向量,如:

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

这构造了一个包含四个数字的向量,并将其赋予std::map::find