std::pair<uint32_t, std::vector<float32>> myPair
suppose,
myPair = { {1221, [234.1, 1235.6]},
{5579, [56.9, 5780.0]},
{ 467, [69579.8, 7974.55]} }
the above-declared myPair
is the object of the pair type container. So I need to retrieve only the keys from the above pair i.e., 1221, 5579, 467....etc
CodePudding user response:
You need something like this. You can change data types as per your need in my answer. And in for loop you can compare those key values as per your need
#include <iostream>
#include <vector>
int main() {
std::vector<std::pair<int, std::vector<float>>> myPairs =
{ {1221, { 234.1F, 1235.6F}},
{5579, { 56.9F, 5780.0F}},
{ 467, {69579.8F, 7974.55F}} };
for (const auto& p : myPairs) {
std::cout << p.first << "\n";
}
return 0;
}
CodePudding user response:
You could also leverage the STL to do this rather easily with a lambda and std::transform
.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<pair<int, vector<float>>> myPairs =
{ {1221, { 234.1, 1235.6 }},
{5579, { 56.9, 5780.0 }},
{ 467, {69579.8, 7974.55}} };
vector<int> keys;
transform(myPairs.cbegin(), myPairs.cend(), back_inserter(keys),
[](const auto &pair) { return pair.first; });
for (const auto &k : keys)
cout << k << endl;
}