For example, I store 3 elements simultaneously like:
vector< pair<int, pair<int, int> > > myvec;
myvec.push_back(make_pair(1, make_pair(2, 3)));
How can I check if {1,2,3} exist (as a pair) in myvec?
You can use find() from algorithm
.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector< pair<int, pair<int, int> > > myvec;
auto p = make_pair(1, make_pair(2, 3));
myvec.push_back(p);
if ( std::find(myvec.begin(), myvec.end(), p) != myvec.end() )
cout << "Found";
else
cout << "Not Found";
return 0;
}
sure, you can do this:
for (auto mval: myvec) {
if (mval.first == 1 && mval.second.first == 2 && mval.second.second == 3)
cout << "Found" << endl;
}
or this:
typedef pair<int,int> IntPair;
typedef pair<int, IntPair> MyPair;
...
for (auto mval: myvec) {
if (mval == MyPair(1, IntPair(2, 3)))
cout << "Found" << endl;
}
or a few other ways.
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.