如何迭代并从map <pair <string,int>,pair <string,Array >>中查找?

时间:2019-12-27 08:28:46

标签: c++ algorithm c++11 data-structures stdmap

这是我的地图

map<pair<string, int>, pair<string, Array> > matchMap;

这是功能:

void Schedule::studentSchedule() 
{
    string s, c;
    cout << "Enter the student and course name to create schedule" << endl;
    cin >> s >> c;

    list<string>::iterator studentLoc;
    map<pair<string, int>, pair<string, Array> >::iterator courseL;

    studentLoc = find(getStudentList().begin(), getStudentList().end(), s);
    courseL = find(getMatchMap().begin(), getMatchMap().end(), c);

    if (studentLoc != getStudentList().end() && courseL != getMatchMap().end())
    {}
}

我在这里找不到字符串,因为出现错误:

courseL = find(getMatchMap().begin(),getMatchMap().end(),c);

如何找到想要的元素?这是错误:

In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:71,
                 from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:61,
                 from C:\Users\Fatih\Desktop\clion\SchoolProject1\Schedule.cpp:4:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Value = const std::__cxx11::basic_string<char>]':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:104:42:   required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const std::__cxx11::basic_string<char> >]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:161:23:   required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const std::__cxx11::basic_string<char> >]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:3905:28:   required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Tp = std::__cxx11::basic_string<char>]'
C:\Users\Fatih\Desktop\clion\SchoolProject1\Schedule.cpp:24:63:   required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h:241:17: error: no match for 'operator==' (operand types are 'std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > >' and 'const std::__cxx11::basic_string<char>')
  { return *__it == _M_value; }
           ~~~~~~^~~~~~~~~~~
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:67,
                 from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:61,
                 from C:\Users\Fatih\Desktop\clion\SchoolProject1\Schedule.cpp:4:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h:860:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)'
     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,

2 个答案:

答案 0 :(得分:3)

您是否尝试过使用find

courseL = matchMap.find(pair<string, int>{c, 1});

地图的密钥类型为pair<string, int>,因此要使用find,您需要提供有效的密钥。

答案 1 :(得分:2)

首先,看到此错误,

no match for 'operator=='
(operand types are 'std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, 
std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > >'
and 'const std::__cxx11::basic_string<char>')

地图的键(即getMatchMap())是std::pair<std::string, int>,而不仅仅是std::string,并且您只传递了std::string

您需要

std::find(getMatchMap().begin(), getMatchMap().end(), std::make_pair(s, /*some integer*/));
//                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

或更合适的方法是使用std::map::find成员函数。

const auto iter = getMatchMap().find(std::make_pair(s, /*some integer*/));