using tuple and lambda

I am using tuple in lambda as below:
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>


using namespace std;



int main()
{
int key = 10;
vector<tuple<int, int>> v;
v.reserve(10);
v[0] = std::make_tuple(10,11);
v[1] = std::make_tuple(20,21);

if(v.end() != std::find(v.begin(), v.end(),[key](std::tuple<int,int> t) {return t.get<0> == key;}))
{
cout<<"FOUND\n";
}
else
{
cout<<"NOT FOUND\n";
}


}

I am getting error:

g++ -std=c++0x d.cpp -o d
d.cpp: In function ‘int main()’:
d.cpp:19: error: expected primary-expression before ‘[’ token
d.cpp:19: error: expected primary-expression before ‘t’
A tiny syntax error: you didn't call get<0>():
[key](std::tuple<int,int> t) {return t.get<0>() == key;}
Last edited on
v.reserve(10); is wrong here, see:

http://www.cplusplus.com/reference/vector/vector/reserve/

Use v.resize(10); instead, see:

http://www.cplusplus.com/reference/vector/vector/resize/


tuple does not have a member get. Use the free function get<0>(t) instead. See:

http://www.cplusplus.com/reference/tuple/tuple/?kw=tuple
I'm trying to wrap the std::get<0>(t) call around various combinations of parentheses but keeping getting this error:
 
 no match for 'operator==' (operand types are 'std::tuple<int, int>' and 'const main()::<lambda(const std::tuple<int, int>&)>')|
I forgot to mention that find_if(...) needs to be use instead of find(...), see:

http://www.cplusplus.com/reference/algorithm/find_if/?kw=find_if


This compiles (using C++11) and returns the expected result:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>


using namespace std;



int main()
{
int key = 10;
vector<tuple<int, int>> v;
v.resize(10);
v[0] = std::make_tuple(10,11);
v[1] = std::make_tuple(20,21);

if(v.end() != std::find_if(v.begin(), v.end(),[key](std::tuple<int,int> t) {return get<0>(t) == key;}))
{
cout<<"FOUND\n";
}
else
{
cout<<"NOT FOUND\n";
}


}
coder777: yes, of course, you're spot on!
Topic archived. No new replies allowed.