简体   繁体   中英

From vector of pairs, how can I find key in C++?

I am creating vector of pairs and want to check whether given key is present or not within vector. I am trying to use std::find_if but seems it is not working. Can someone help me if I am doing something wrong in my code or any other approach to do find element for a given key?

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

In above code, I want to find pair whose key is 1. and I am getting exception.

Exception:

$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

It appears to be a simple typo. You declared your vector as std::vector<std::pair<int, char>> but the lambda expression you used takes const std::pair<std::string, char>& . Just change the std::string to int and it should be fine.

Edit: Also the = 1 should be == 1 as Borgleader noted.

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

Elements of your vector are std::pair < int, char > .

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

Your lambda accepts references to std::pair < std::string, int > . Such a reference cannot bind to an object of type std::pair < int, char > . That's what the compiler says here:

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

To fix this, use an argument type that matches with the type of the element.


Furthermore, your lambda tries to modify the object through the const reference. This is ill-formed. You may have intended to use the comparison operator instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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