从向量对中,如何找到C ++中的密钥?

时间:2019-02-09 15:15:53

标签: c++

我正在创建向量对,并想检查向量中是否存在给定密钥。我正在尝试使用std :: find_if,但似乎无法正常工作。如果我在代码中做错了事情,或者可以通过其他任何方法找到给定键的元素,有人可以帮我吗?

std::vector < std::pair < int, char >> myVec;
pair < int, char > p1 = make_pair(1, 'a');
pair < int, char > p2 = make_pair(2, 'b');
pair < int, char > p3 = make_pair(3, 'c');
myVec.push_back(p1);
myVec.push_back(p2);
myVec.push_back(p3);

auto it = std::find_if(myVec.begin(), myVec.end(), [](const std::pair < std::string, int > & element) {
    return element.first = 1;
});
if (it == myVec.end()) {
    cout << "not found\n";
}

在上面的代码中,我想找到密钥为1的货币对,并且正在获取异常。

例外:

$g++ -o main *.cpp
main.cpp: In lambda function:
main.cpp:19:123: error: passing ‘const std::__cxx11::basic_string<char>’ as ‘this’ argument discards qualifiers [-fpermissive]
     auto it = std::find_if(myVec.begin(),myVec.end(),[](const std::pair<std::string, int>& element){ return element.first=1;} );
                                                                                                                           ^
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from main.cpp:1:
/usr/include/c++/7/bits/basic_string.h:677:7: note:   in call to ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       operator=(_CharT __c)
       ^~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/7/bits/char_traits.h:39,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from main.cpp:1:
/usr/include/c++/7/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<std::pair<int, char>*, std::vector<std::pair<int, char> > >; _Predicate = main()::<lambda(const std::pair<std::__cxx11::basic_string<char>, int>&)>]’:
/usr/include/c++/7/bits/stl_algo.h:120:14:   required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<int, char>*, std::vector<std::pair<int, char> > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<main()::<lambda(const std::pair<std::__cxx11::basic_string<char>, int>&)> >]’
/usr/include/c++/7/bits/stl_algo.h:161:23:   required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<std::pair<int, char>*, std::vector<std::pair<int, char> > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<main()::<lambda(const std::pair<std::__cxx11::basic_string<char>, int>&)> >]’
/usr/include/c++/7/bits/stl_algo.h:3932:28:   required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<std::pair<int, char>*, std::vector<std::pair<int, char> > >; _Predicate = main()::<lambda(const std::pair<std::__cxx11::basic_string<char>, int>&)>]’
main.cpp:19:127:   required from here
/usr/include/c++/7/bits/predefined_ops.h:283:11: error: no match for call to ‘(main()::<lambda(const std::pair<std::__cxx11::basic_string<char>, int>&)>) (std::pair<int, char>&)’
  { return bool(_M_pred(*__it)); }
           ^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/predefined_ops.h:283:11: note: candidate: std::__cxx11::basic_string<char> (*)(const std::pair<std::__cxx11::basic_string<char>, int>&) <conversion>
/usr/include/c++/7/bits/predefined_ops.h:283:11: note:   candidate expects 2 arguments, 2 provided
main.cpp:19:99: note: candidate: main()::<lambda(const std::pair<std::__cxx11::basic_string<char>, int>&)>
     auto it = std::find_if(myVec.begin(),myVec.end(),[](const std::pair<std::string, int>& element){ return element.first=1;} );
                                                                                                   ^
main.cpp:19:99: note:   no known conversion for argument 1 from ‘std::pair<int, char>’ to ‘const std::pair<std::__cxx11::basic_string<char>, int>&’

main.cpp

2 个答案:

答案 0 :(得分:2)

这似乎是一个简单的错字。您将向量声明为std::vector<std::pair<int, char>>,但您使用的lambda表达式采用const std::pair<std::string, char>&。只需将std::string更改为int,就可以了。

编辑: 同样,= 1也应为== 1,如Borgleader所述。

答案 1 :(得分:1)

std::vector < std::pair < int, char >> myVec;
              ^^^^^^^^^^^^^^^^^^^^^^^

向量的元素为std::pair < int, char >

auto it = std::find_if(myVec.begin(), myVec.end(), [](const std::pair < std::string, int > & element) {
                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

您的lambda接受对std::pair < std::string, int >的引用。这样的引用不能绑定到类型std::pair < int, char >的对象。那就是编译器在这里说的:

no known conversion for argument 1 from ‘std::pair<int, char>’ to ‘const std::pair<std::__cxx11::basic_string<char>, int>&’

要解决此问题,请使用与元素类型匹配的参数类型。


此外,您的lambda尝试通过const引用修改对象。这是不正确的。您可能打算改用比较运算符。